How to Change/Switch Runlevel of a Linux Server

How to Change Runlevel of a Linux Server – How to Switch Runlevel of a Linux system/PC

Linux Runlevels Explained

During booting init command will check the default runlevel you have specified in the /etc/inittab file. By default your linux system will boot in either runlevel 3 or 5.

Before changing the runlevel you must have basic knowledge of different runlevels and what it does. There are 7 runlevels on a Linux Server, starting from runlevel 0 to runlevel 6 (0 – 6). Different Linux Runlevel and their details is pasted below :
 


Runlevel 0 = Halt or Shutdown. This will shutdown the Linux system
Runlevel 1 = This is the Single User Mode
Runlevel 2 = Multi User Mode without NFS
Runlevel 3 = Multiuser mode with networking
Runlevel 4 : Runlevel 4 is user-definable
Runlevel 5 : Runlevel 5 is multiuser mode with graphical user interface
Runlevel 6 : Reboot. This reboots the server.

Each runlevel have different configuration. init is the parent process with PID 1 and it will check the inittab file and load the server to that runlevel. All the services (like httpd, mysql, nginx etc) in that runlevel will be loaded.


 
Most of the linux PCs that we use in our home will be in runlevel 5 (with GUI – graphical user interface) and most of the linux servers for hosting websites/data will be in runlevel 3. It is possible to change the runlevel of linux PCs and server.

Before changing the runlevel first check the current runlevel you are on.

Command 1 to check the current runlevel : runlevel
Command 2 to check the current runlevel : who -r
Command 3 to check the current runlevel : grep -i initdefault /etc/inittab

You can use any of the above command to see the current runlevel you are on.

Current runlevel of Linux Unix server.

 

-sh-4.1$ grep -i initdefault /etc/inittab
id:3:initdefault:

-sh-4.1$ who -r
run-level 3 2015-12-03 06:45

-sh-4.1$ runlevel
N 3

 

The output of the above command shows that my Linux server is in runlevel 3 (without GUI)
 


 
How to Change/Switch the runlevel of your Linux Server Permanently

The default runlevel of your linux system is specified in the file /etc/inittab file. It is possible to edit this file using your favorite text editor.

Open the file /etc/inittab using vi editor like show below

vi /etc/inittab

id:2:initdefault — > This Server is in runlevel 2 (Current runlevel is 2)

change above line to “id:3:initdefault” to change the server to runlevel 3 after reboot.

Save the above file and exit.

Reboot the server after changing the default runlevel in inittab file. After reboot init command will check the runlevel you have entered in the /etc/inittab and your linux PC/Server will be loaded in that runlevel.

On production servers with websites hosted runlevel 3 (text mode – multi user mode + networking) is commonly used because graphical mode will consume more server resources compared to runlevel 5. Linux PCs we use in home runs on runlevel 5 because it is more easy to use compared to runlevel 3.

Do not set runlevel 0 (shutdown) or 6 (reboot) in your /etc/initab file.
 


 
How to Change/Switch the runlevel of your Linux Server Temporarily

If you just type “init 0” command on your linux server, it will be shutdown immediately.

If you type “init 6” command on your linux server, it will be rebooted immediately.

If you just type “init 1” command on your linux server, the server will be shutdown and single user mode will be loaded.

This is not a permanent change, after server reboot you will be back in your default runlevel. Edit the /etc/inittab file if you want to change the runlevel permanently.

It is also possible to change the runlevel during server boot time.