Here's How: Linux runlevels

When a Linux system is booted it passes through several distinct stages, known as runlevels, before the system has completed booting and is usable. Likewise, when a Linux system is shut down it passes through another set of runlevels before the system is ready to be turned off. In this column, we are going to examine runlevels and show you some tricks that can be used to avoid unnecessary reboots of your Linux system.

A runlevel could be referred to as a system state. They can also be considered as similar to the several modes into which Microsoft Windows is able to boot: Normal, Safe-mode and Command prompt only. Each runlevel has a collection of associated services that are initialised or stopped when the system enters the runlevel. These services are represented by initialisation scripts located in the directory corresponding to each runlevel: /etc/rc.d/rc?.d or /etc/rc?.d, where ? is the number corresponding to the runlevel.

There are generally eight runlevels used by most Linux distributions, though some may vary this organisation slightly.

Runlevel System State
0 Halt the system
1 Single user mode
2 Basic multi user mode
3 Multi user mode
5 Multi user mode with GUI
6 Reboot the system
S, s Single user mode

Most desktop Linux systems by default boot into runlevel 5 and greet the user with a graphical login prompt. Most server systems by default boot into runlevel 3 and greet the user with a text-mode login prompt. Runlevels 1and 2 are rarely used outside of debugging purposes. Runlevels S and s are not intended for direct use by users and are used to prepare the system for Single User mode.

The advantage Linux runlevels have over Windows’ boot modes is that Linux runlevels can be changed on the fly using the ‘init’ tool. You already unknowingly change runlevels on the fly whenever you shut down or reboot your Linux system. Part of the shutdown process involves calling init with the corresponding runlevel (0 or 6) to stop all running processes.

Displaying the runlevel

To begin, you may be curious to know which runlevel your system was previously and is currently in. The ‘runlevel’ command can be used to find out this information:

$ /sbin/runlevel 3 5

The output of the runlevel command tells me my system was previously in runlevel 3 and is currently in runlevel 5.

Switching runlevels

To switch runlevels use the ‘telinit’ command to control the ‘init’ process (as root):

$ /sbin/telinit

A common reason to switch runlevels is to restart certain applications such as XFree86. Switching from runlevel 5 to runlevel 3 will stop XFree86 and its associated programs. XFree86 can then be restarted cleanly by switching back to runlevel 5. Before trying this, be sure to save any work underway in XFree86, as all GUI applications will be stopped. To try restarting XFree86 using init, type (as root):

$ /sbin/telinit 3 $ /sbin/telinit 5

After typing each command your screen will likely fill with messages as each service is started or stopped.

Adding services to a runlevel

To add a service to be started at a certain runlevel, first create a startup script for the service and place it in the directory /etc/rc.d/init.d or /etc/init.d (depending on how your distribution lays out these directories). To associate the script with a runlevel, create a symbolic link in the directory for the runlevel you wish it to be started under. The filename of this symbolic link should be prefixed with SXX where XX is a number. The purpose of the number is to control the order in which scripts are executed within each runlevel. The scripts are executed in ascending numerical order. An example of adding an initialisation script to a runlevel is:

$ cp myservice /etc/init.d/ $ ln -s /etc/init.d/myservice /etc/rc3.d/S99myservice

The next time runlevel 3 is entered, myservice would be started.

Booting into a runlevel

You can change the runlevel Linux boots into by specifying it at boot time. If your system uses LILO, append the runlevel to the boot command as shown:

LILO: linux 5

If your system uses GRUB, you can change the boot runlevel by pressing the ‘e’ key to edit the boot configuration. From here, append the runlevel to the end of the boot command as shown:

kernel /vmlinuz ro root=/dev/hda1 5

Join the PC World newsletter!

Error: Please check your email address.

Our Back to Business guide highlights the best products for you to boost your productivity at home, on the road, at the office, or in the classroom.

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Alastair Cousins

PC World
Show Comments

Essentials

Lexar® JumpDrive® S57 USB 3.0 flash drive

Learn more >

Microsoft L5V-00027 Sculpt Ergonomic Keyboard Desktop

Learn more >

Mobile

Lexar® JumpDrive® S45 USB 3.0 flash drive 

Learn more >

Exec

Lexar® JumpDrive® C20c USB Type-C flash drive 

Learn more >

HD Pan/Tilt Wi-Fi Camera with Night Vision NC450

Learn more >

Lexar® Professional 1800x microSDHC™/microSDXC™ UHS-II cards 

Learn more >

Audio-Technica ATH-ANC70 Noise Cancelling Headphones

Learn more >

Budget

Back To Business Guide

Click for more ›

Most Popular Reviews

Latest News Articles

Resources

PCW Evaluation Team

Azadeh Williams

HP OfficeJet Pro 8730

A smarter way to print for busy small business owners, combining speedy printing with scanning and copying, making it easier to produce high quality documents and images at a touch of a button.

Andrew Grant

HP OfficeJet Pro 8730

I've had a multifunction printer in the office going on 10 years now. It was a neat bit of kit back in the day -- print, copy, scan, fax -- when printing over WiFi felt a bit like magic. It’s seen better days though and an upgrade’s well overdue. This HP OfficeJet Pro 8730 looks like it ticks all the same boxes: print, copy, scan, and fax. (Really? Does anyone fax anything any more? I guess it's good to know the facility’s there, just in case.) Printing over WiFi is more-or- less standard these days.

Ed Dawson

HP OfficeJet Pro 8730

As a freelance writer who is always on the go, I like my technology to be both efficient and effective so I can do my job well. The HP OfficeJet Pro 8730 Inkjet Printer ticks all the boxes in terms of form factor, performance and user interface.

Michael Hargreaves

Windows 10 for Business / Dell XPS 13

I’d happily recommend this touchscreen laptop and Windows 10 as a great way to get serious work done at a desk or on the road.

Aysha Strobbe

Windows 10 / HP Spectre x360

Ultimately, I think the Windows 10 environment is excellent for me as it caters for so many different uses. The inclusion of the Xbox app is also great for when you need some downtime too!

Mark Escubio

Windows 10 / Lenovo Yoga 910

For me, the Xbox Play Anywhere is a great new feature as it allows you to play your current Xbox games with higher resolutions and better graphics without forking out extra cash for another copy. Although available titles are still scarce, but I’m sure it will grow in time.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?