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.
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*
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.
Runlevel | Systemd Description |
---|---|
0 | poweroff.target |
1 | rescue.target |
2 | multi-user.target |
3 | multi-user.target |
4 | multi-user.target |
5 | graphical.target |
6 | reboot.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:
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".
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:
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".