[one-liner]: Linux Runlevels

Background

Determining a systems runlevel can be useful when setting up a headless server or determining if a system is being used as a server or as a workstation. Thankfully it’s pretty easy, using a number of commands. But before we get started, a little background.

Runlevels are a concept from UNIX System V used by the init(8) daemon or other system initialization system to define modes of system operation. Eight runlevels are permitted, the first seven are numbered 0-6 and the eighth is named S or s (both are permitted). Services and other system components are said to exist in one or more runlevels. When switching from one runlevel to another, the services that should not exist in the new runlevel are stopped and the services that only exist in the new runlevel are started.

NOTE: Runlevels 0, 1 and 6 are reserved. Runlevel 0 is used to halt the system and 6 to reboot the system. Run level 1 is used to bring the system back down into single-user mode, after which the runlevel will be S.

Solution

To determine a system’s runlevel you can use one of the following methods.

#1 – who

The 1st one I’ll cover, and the least obvious – at least to me, is the command who. This is the reason that I’m writing this post!

1
2
$ who -r
         run-level 3  2013-12-02 21:45
#2 – runlevel

The much more direct method is to use the command, runlevel.

1
2
$ runlevel
N 3
Changing runlevels

To do this you can use one of 2 commands, init or telinit. Both take a number as an argument or a character such as “S” for single user mode aka. runlevel 1.

1
2
3
4
5
6
7
8
9
10
# Examples
$ init 1     # single user mode
$ telinit S  # single user mode
 
$ telinit 3
 
$ telinit 0  # halt
$ telinit 6  # reboot
 
$ telinit Q  # re-read /etc/inittab file
Rebooting & Halting

You can also use the shutdown command to perform a reboot and/or a halt, as well as the commands reboot, halt, & poweroff.

1
2
3
4
5
6
7
8
# Examples
$ shutdown -h now    # halt 
 
$ shutdown -r now    # reboot
 
$ reboot
$ halt
$ poweroff
Changing permanently

All the changing we’ve been doing above are temporary. To change a system’s runlevel permanently so that instead of say 5 it’s now 3, you’ll need to edit init‘s configuration file, /etc/inittab. A change like this to the file:

1
id:3:initdefault:

Save it and either tell init to re-read it using the command telinit Q or reboot the system.

References

links

NOTE: For further details regarding my one-liner blog posts, check out my one-liner style guide primer.

This entry was posted in linux, one-liner, Syndicated, sysadmin, tips & tricks. Bookmark the permalink.

Comments are closed.