How to Change Runlevels in Linux

Runlevel can be defined as a part of Linux OS which determines the processes that need to be started at system startup. While booting a Linux system, 'init' (but different in the case of systemd) is the first process that gets executed with PID 1, which then starts other processes. The processes that need to be started by 'init' at system bootup are determined by the default runlevel mentioned in '/etc/inittab' file.

There are 7 runlevels defined in any Linux machine, which are as follows:

0 System Halt
1 Single User Mode
2 Multi User Mode without networking
3 Multi User Mode with networking
4 Not Used/Special purpose
5 Multi User Mode with GUI
6 System Reboot

All runlevels are defined in a directory '/etc/rcX.d', where X corresponds to the runlevel.

For example, runlevel5 is available at, '/etc/rc5.d'. In this directory you can find many files with the following syntax:

S/K nnBinaryname

Where,

S => Stands for starting the process
K => Stands for kill/stop the processes
nn => These digits determines the order in which the programs need to be executed. The lowest digit programs will be executed first.

So, all programs that start with letter 'K' will be killed in that particular runlevel and all programs that start with 'S' will be started in that particular runlevel.

 Changing and Viewing the default runlevel

The default run level is defined in the file '/etc/inittab'. You can view it as follows.

# grep ^id /etc/inittab
id:5:initdefault:

As you can see from the above output, the default runlevel is 5. If you want to change this to 3, edit the /etc/inittab file with the following.

id:3:initdefault:

Listing the current runlevel

If you want to check the current runlevel in which your machine runs, you can use any of the following commands.

# runlevel
N 3

This shows that the current runlevel is 3. 'N' stands for none, meaning there has been no run level change since powering up.

# who –r
run-level 3 2012-08-10 04:30 last=S

This shows the current runlevel is 3 and the last runlevel was 'single user mode'.

Changing runlevel

You can change the runlevels using the command telinit (stands for telling init o change runlevel). This actually signals “init” process to change runlevel.

For example, if you want to change the runlevel to 5, execute the following command.

# telinit 5
or
# init 5
Bobbin Zachariah 10:17 pm

Comments

Your email address will not be published. Required fields are marked *

2 Comments

  1. I wonder what is equivalent to /etc/inittab in Ubuntu Server 18.04.04, I want to change the default runlevel 5 for ssh to 4 (with an option to change it to 3 after logging in). Some hint? :)

    1. Hi Maciek,

      The new version of Ubuntu uses target instead of runlevels. You can switch the target using 'systemctl isolate' command. For desktop systems the default taget is 'graphical.target' - run this command $ systemctl get-default.

      In your case run below command:

      $ sudo systemctl isolate multi-user.target

      To make this as the default

      $ sudo systemctl enable multi-user.target
      $ sudo systemctl set-default multi-user.target

      FYI

      Below is the mapping between runlevels and systemd targets:

      0 - poweroff.target

      1 - rescue.target

      2, 3, 4 - multi-user.target

      5 - graphical.target

      6 - reboot.target