Labels

Showing posts with label ubuntu. Show all posts
Showing posts with label ubuntu. Show all posts

Wednesday, 24 May 2017

How to change default runlevel in ubuntu 16.04? How to find default runlevel?

How to find default Runlevel?

The conventional way to check the runlevel in linux distos using runlevel command.

$ runlevel
N 5
The output shows/explains about two things 
1. N -> Indicates the previous runlevel used after reboot.
2. 5 -> Runlevel number 

How to check runlevel in ubuntu 16.04?

Ubuntu 16.04 uses systemd as a init daemon program. so lets have a brief understanding about systemd.

What is Systemd?

systemd is a replacement to the older traditional "System V init" system . systemd stands for system daemon. systemd was designed to allow for better handling of dependencies and have the ability to handle more work in parallel at system startup. systemd supports snapshotting of your system and the restoring of your systems state, keeps track of processes stored in what is known as a "cgroup" as opposed to the conventional "PID" method. systemd is now shipping by default with many popular Linux distributions such as Fedora, Mandriva, Mageia, Arch Linux, CentOS 7, RHEL 7.0 (Red Hat Enterprise Linux) and Oracle Linux 7.0. systemd refers to runlevels as targets.

In the following examples, we will show you how to display and work with different runlevels (targets). The system used to demonstrate the following commands is a RHEL 7.0 Standard Desktop configuration.

Controlling Runlevels

To display the current runlevel of your system, you will need to issue the following command: systemctl -get-default


[root@rhel07a ~]# systemctl get-default
graphical.target

The reply back from the system is "graphical.target". Basically the runlevel "graphical.target" is the equivalent to the traditional runlevel 5, Full user access with Graphical Display and networking.

You can display the new runlevels/targets by issuing the following command:

ls -al /lib/systemd/system/runlevel*


[root@rhel07a /]# ls -al /lib/systemd/system/runlevel*
lrwxrwxrwx. 1 root root 15 Apr 25 10:31 /lib/systemd/system/runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 Apr 25 10:31 /lib/systemd/system/runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 Apr 25 10:31 /lib/systemd/system/runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 Apr 25 10:31 /lib/systemd/system/runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 Apr 25 10:31 /lib/systemd/system/runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 Apr 25 10:31 /lib/systemd/system/runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 Apr 25 10:31 /lib/systemd/system/runlevel6.target -> reboot.target

From the above we can see that we still have seven different runlevels ranging from system poweroff to system reboot. 

RunlevelSystemd Description
0poweroff.target
1rescue.target
2multi-user.target
3multi-user.target
4multi-user.target
5graphical.target
6reboot.target


Traditionally the default runlevel was contained within the "/etc/inittab" file and could be displayed with the following command:
cat /etc/inittab | grep initdefault. This would typically report back with an entry similar to: id:5:initdefault:.

Now if you try to display the "/etc/inittab" file on a system using systemd, you will see a message similar to the following:


[root@rhel07a /]# cat /etc/inittab
# inittab is no longer used when using systemd.
#
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# Ctrl-Alt-Delete is handled by /etc/systemd/system/ctrl-alt-del.target
#
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
#
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
#
# To set a default target, run:
#
# ln -sf /lib/systemd/system/<target name>.target /etc/systemd/system/default.target

Setting a new Default Runlevel

In the following example we are going to change the runlevel from "graphical.target" to "multi-user.target". (Runlevel 5 to Runlevel 3).

To do this we simply issue the following commands:

rm /etc/systemd/system/default.target
ln -s /lib/systemd/system/runlevel3.target /etc/systemd/system/default.target

Alternatively you could issue the link command with the "-f" parameter indicating that the destination file is to be removed:

ln -sf /lib/systemd/system/runlevel3.target /etc/systemd/system/default.target

Here we are first deleting the existing "default.target" and then replacing with our link command. Our new "target.default" will be that of "runlevel3.target".


[root@rhel07a /]# rm /etc/systemd/system/default.target
rm: remove symbolic link ‘/etc/systemd/system/default.target’? y
[root@rhel07a /]# ln -s  /lib/systemd/system/runlevel3.target  /etc/systemd/system/default.target
[root@rhel07a /]# systemctl get-default 
runlevel3.target

Now if we were to reboot the system, it would start in "runlevel 3 - multi-user.target".
To revert back to the original runlevel "runlevel 5 - graphical.target" we would simply issue the following commands:


[root@rhel07a ~]# systemctl get-default 
runlevel3.target
[root@rhel07a ~]# rm /etc/systemd/system/default.target
rm: remove symbolic link ‘/etc/systemd/system/default.target’? y
[root@rhel07a ~]# ln -s  /lib/systemd/system/runlevel5.target  /etc/systemd/system/default.target
[root@rhel07a ~]# systemctl get-default 
runlevel5.target

For the system to switch to the new runlevel, you would need to reboot your system or issue the "init" command followed by the relevant runlevel.

Another Approach:

Ubuntu 16.04 uses systemd instead of init and hence the concept of runlevels is replaced by the term targets. So there is indeed a mapping between init-based runlevels and systemd-based targets:
   Mapping between runlevels and systemd targets
   ┌─────────┬───────────────────┐
   │Runlevel │ Target            │
   ├─────────┼───────────────────┤
   │0        │ poweroff.target   │
   ├─────────┼───────────────────┤
   │1        │ rescue.target     │
   ├─────────┼───────────────────┤
   │2, 3, 4  │ multi-user.target │
   ├─────────┼───────────────────┤
   │5        │ graphical.target  │
   ├─────────┼───────────────────┤
   │6        │ reboot.target     │
   └─────────┴───────────────────┘
Now, to just change the "runlevels" in 16.04, you can use for eg:
sudo systemctl isolate multi-user.target
To make this the default "runlevel", you can use:
sudo systemctl enable multi-user.target
sudo systemctl set-default multi-user.target
The command sudo systemctl set-default multi-user.target does the same operation mentioned in the starting. Which will create the symbolic link . The only difference is "the first method user is creating link default.target manually", whereas in second method "systemd is creating link default.target".

How to run a script at reboot with systemd service?

Run a Script at Reboot Using Systemd service

NOTE: Works on latest linux distos like ubuntu 16.04, which uses systemd as a init service. You can check it by using below command
ls -l /sbin/init

output looks like below
lrwxrwxrwx 1 root root 20 Jan 19 03:34 /sbin/init -> /lib/systemd/systemd
Run the service unit as a normal service - have a look at the [Install] section. So everything has to be thought reverse, dependencies too. Because the shutdown order is the reverse startup order. That's why the script has to be placed in ExecStop=.
The following solution is working for me:
[Unit]
Description=...

[Service]
Type=oneshot
RemainAfterExit=true
ExecStop=<your script/program>

[Install]
WantedBy=multi-user.target
RemainAfterExit=true is needed when you don't have an ExecStart action. 
After creating the file, make sure to systemctl daemon-reload and systemctl enable yourservice.
Note: At ubuntu 16.04 you must have a ExecStart=/bin/true --> which means under [service] add the ExecStart=/bin/true, before ExecStop.

Case study:

You can verify the service working or not based on the message given in the Description under [Unit] section.
If Description=My Magic Script, then after issuing reboot we can see message from the log like "Stopping My Magic Script..." and another print like "Stopped My Magic Script". At startup log shows like "Starting My Magic Script..." and which will execute /bin/true binary and exits.
Important thing to observe is binary gets exited whereas service wont die..why because there is property in service which makes it not to die and the property is "RemainAfterExit=true".
We can see the service status whether it actually died or not by running the below command
systemctl status <service_name>

To get full understanding of each stanza please follow the below systemd manual.
References:
https://www.freedesktop.org/software/systemd/man/systemd.service.html#
 

Monday, 14 December 2015

Date/time missing in the menu bar in ubuntu

Date and time missing in menu bar in ubuntu? here is fix!

If you have upgraded your Ubuntu machine to Ubuntu Saucy 13.10, one of the things that you might find missing is the date/time indicator in the menu bar. And if you visit the “System Settings -> Time and Date -> Clock” section, you might find that the option to add it to the menu bar is greyed out and disabled.

 

If you are having this problem, here’s the fix.

1. Reinstall indicator-datetime. It should be installed by default, but just in case you have removed it unknowingly, it is best to run the install command again.
sudo apt-get install indicator-datetime
2. Next, we are going to reconfigure the date time:
sudo dpkg-reconfigure --frontend noninteractive tzdata
3. Lastly, restart unity.
sudo killall unity-panel-service
 NOTE: This was worked fine!! tested on ubuntu 14.04LTS.


















Friday, 9 January 2015

How to install NGINX RTMP server in ubuntu

Installing Nginx and Nginx-RTMP

Install the tools required to compile Nginx and Nginx-RTMP from source.

sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev
 
Make a working directory and switch to it.

mkdir ~/working
cd ~/working
 
Download the Nginx and Nginx-RTMP source.

wget http://nginx.org/download/nginx-1.7.5.tar.gz
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
 
Install the Unzip package.

sudo apt-get install unzip
 
Extract the Nginx and Nginx-RTMP source.

tar -zxvf nginx-1.7.5.tar.gz
unzip master.zip
 
Switch to the Nginx directory.

cd nginx-1.7.5
 
Add modules that Nginx will be compiled with. Nginx-RTMP is included.

./configure --with-http_ssl_module --add-module=../nginx-rtmp-module-master
 
Compile and install Nginx with Nginx-RTMP.

make
sudo make install
 
Install the Nginx init scripts.

sudo wget https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx -O /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
sudo update-rc.d nginx defaults
 
Start and stop Nginx to generate configuration files.

sudo service nginx start
sudo service nginx stop

Installing FFmpeg

Add the FFmpeg PPA.

sudo apt-add-repository ppa:jon-severinsson/ffmpeg
 
Update the package lists.

sudo apt-get update
 
Install FFmpeg.

sudo apt-get install ffmpeg
Note: The apt-add-repository command may not be installed in some cases. To install it run sudo apt-get install software-properties-common.

Configuring Nginx-RTMP and FFmpeg

Open the Nginx configuration file.

sudo nano /usr/local/nginx/conf/nginx.conf
 
Append the following.

rtmp {
    server {
            listen 1935;
            chunk_size 4096;

            application live {
                    live on;
                    record off;
                    exec ffmpeg -i rtmp://localhost/live/$name -threads 1 -c:v libx264 -profile:v baseline -b:v 350K -s 640x360 -f flv -c:a aac -ac 1 -strict -2 -b:a 56k rtmp://localhost/live360p/$name;
            }
            application live360p {
                    live on;
                    record off;
        }
    }
}
After you've added the above, you can customize settings such a video bitrate, audio bitrate and resolution. These changes will only be applied to the lower quality stream. To add more qualities, copy and paste the exec ffmpeg line and change the settings. You'll also need to create a new application. You can do this by copying and pasting the live360 example that has been included. Don't forget to update the exec ffmpeg line with the address of the new application. You can do this by changing the final RTMP address in the exec ffmpeg line.
Note: Changing the value after -b:v will change the video bitrate. This is measured in kilobits per second. Changing the value after -b:a will change the audio bitrate. This is measured in kilobits per second. Changing the value after -s will change the resolution.
Save the file by pressing Control and X together. Restart Nginx.

sudo service nginx restart
Note: For best performance, each stream being converted should have its own CPU core. For example two qualities, 360P and 480P are being created from a 720P stream. A Vultr instance with at least two CPU cores should be used.

Security Note

If you're using a firewall, you'll need to make sure TCP 1935 is allowed.
The current configuartion allows anyone to stream to your server. We can fix this by only allowing certain IP addresses the publish permission. Open the Nginx configuration.

sudo nano /usr/local/nginx/conf/nginx.conf
 
Look for the following lines.

                live on;
                record off;
 
Add the following to each set of the above lines. Change 0.0.0.0 to your IP address.
                allow publish 127.0.0.1;
                allow publish 0.0.0.0;
                deny publish all;
 
The configuration should now look something like this.

rtmp {
    server {
            listen 1935;
            chunk_size 4096;

            application live {
                    live on;
                    record off;
                    allow publish 127.0.0.1;
                    allow publish 0.0.0.0;
                    deny publish all;
                    exec ffmpeg -i rtmp://localhost/live/$name -threads 1 -c:v libx264 -profile:v baseline -b:v 350K -s 640x360 -f flv -c:a aac -ac 1 -strict -2 -b:a 56k rtmp://localhost/live360p/$name;
            }
            application live360p {
                    live on;
                    record off;
                    allow publish 127.0.0.1;
                    allow publish 0.0.0.0;
                    deny publish all;
        }
    }
}
 
Save the file by pressing Control and X together. Restart Nginx.

sudo service nginx restart

Configuring Software to Work with Nginx-RTMP

Streaming applications typically have two fields for connection information. The first field is usually for the server information and the second field is usually for the stream name or key. The information that you should place into each field is listed. The stream name or key can be set to anything.

Field 1: rtmp://your.vultr.ip/live/
Field 2: stream-key-your-set
 
To view streams open the following links in a player supporting RTMP.

rtmp://your.vultr.ip/live/stream-key-you-set
rtmp://your.vultr.ip/live360p/stream-key-your-set
 
Setting up a player to display live video on a website is beyond the scope of this guide. Searching for the term 'RTMP web player' might assist you.