Learn to shut down or restart your Linux system, warn users that the system is shutting down, and switch to a somewhat restrictive run level. This process is unfamiliar to many Linux enthusiasts. Next, Ian Shields, a senior programmer from IBM, will give a detailed introduction to the run level, shutdown and restart of Linux.
In this article, learn to shut down or restart your Linux system and warn users that the system is shutting down, please switch to single-user mode or a somewhat restrictive run level. Learn:
- Set the default run level
- Changes between run levels
- Change to single user mode
- Shut down or restart the system from the command line
- Warn users of major system events, including switching to another run level
- Terminate the process appropriately
Unless otherwise specified, the examples in this article use Fedora 8 with a 2.6.26 kernel. The upstar example uses Fedora 13 with a 2.6.34 kernel, or Ubuntu 10.10 with a 2.6.35 kernel . Your results may be different on other systems.
In order to make the most of the articles in this series, you should have a basic knowledge of Linux and need to prepare a Linux system for practicing the commands described in this article. Sometimes different versions of the program have different output formats, so the results you get may not always be the same as the list and diagram shown here. In particular, recent systems have changed a lot of content familiar to users of the traditional System V init process (see Init vs. Upstart for more details). This article is aimed at the traditional System V init process, and only gives a rough overview of the difference between upstar.
The run level defines the tasks that can be completed in the current state (or run level) of the Linux system. Each Linux system supports 3 basic run levels, plus one or more run levels for normal operation. The basic operating levels are shown in Table 1.
|0||Shut down (or terminate) the system|
|1||Single user mode: usually also called s or S|
|6||Restart the system|
In addition to these basic run levels, the use of run levels varies from release to release. A command usage set is shown in Table 2.
|2||Multi-user mode without network|
|3||Multi-user mode with network|
|5||Multi-user mode with network and X Window System|
The Slackware distribution uses run level 4 instead of 5 because the entire system is running the X Window system. Debian and its derivatives, such as Ubuntu , use a single run level for any multi-user mode, usually run level 2. Be sure to check your version of the documentation.
Default run level
When a Linux system starts, the default run level is determined by the id: entry under /etc/inittab. Listing 1 illustrates a typical entry for a system (such as Fedora 8 or openSUSE 11.2), both of which use run level 5 for the X Window System.
Listing 1. Default runlevel in /etc/inittab
[root@pinguino ~]# grep "^id:" /etc/inittab id:5:initdefault:
If you want your system to start in a different run level, for example run level 3, edit this value.
Change run level
There are several ways to change the run level. In order to make a permanent change, you can edit /etc/inittab and change the default runlevel, as you just saw above.
If you only need to promote your system to a different run level in one boot, you can do so. For example, suppose you have just installed a new kernel, and you need to build some kernel modules after restarting the system with the new kernel, but before starting the X Window System. You may want to upgrade your run level to level 3 to accomplish this task. You can do this at boot time by editing the kernel line (GRUB) or adding a parameter after the selected system name (LILO). Use a single digit to indicate the desired run level (in this case, 3). We will introduce the specific process through a GRUB example. Suppose your /boot/grub/menu.lst file contains the fragment shown in Listing 2.
Listing 2. A typical GRUB snippet to boot Fedora 8
title Fedora (220.127.116.11-57.fc8) root (hd0,5) kernel /boot/vmlinuz-18.104.22.168-57.fc8 ro root=LABEL=FEDORA8 rhgb quiet initrd /boot/initrd-22.214.171.124-57.fc8.img
To upgrade the system to run level 3, wait until the boot entry appears, select the entry and enter’e’ to edit the entry. Depending on your GRUB options, you may need to press a key to display the boot entry, and then also enter a’p’ and a password to unlock the editing. The GRUB screen on Fedora 8 system looks like Figure 1.
Figure 1. Choosing a boot option in GRUB
Listing 3. Verify and change the run level
[root@pinguino ~]# runlevel N 3 [root@pinguino ~]# telinit 5
After entering telinit 5, you will see several messages flash by, and your interface will switch to the configured graphical login interface. Open a terminal window and verify that the run level has been modified, as shown in Listing 4.
Listing 4. Confirm the new runlevel
[root@pinguino ~]# runlevel 3 5
If you use the ls command to display a long list of telinit commands, you will see that it is actually a symbolic link to the init command. We will illustrate this in Listing 5.
Listing 5. telinit is just a symbolic link to init
[root@pinguino ~]# ls -l $(which telinit) lrwxrwxrwx 1 root root 4 2008-04-01 07:50 /sbin/telinit -> init
The executable file init knows whether it is called init or telinit, and then adjusts accordingly. Because init runs at boot time as PID 1, it is also very smart, knowing when you will use init to call it later instead of using telinit. If you do, it will assume that you want it to behave as if telinit has been called. For example, you might use init 5 instead of telinit 5 to switch to run level 5.
Single user mode
Compared to personal computer operating systems, such as DOS or Windows , Linux itself is a multi-user system. However, sometimes this may become a problem, for example, when you need to restore an important file system or database, or install and test a new software. Run level 1, single-user mode is your best answer in these situations. The actual implementation varies according to different versions, but you usually only use a small system to simply start. Usually there is no networking here, and no (or very few) daemons are running. On some systems, you must log in for authentication, but on other systems you can directly start the shell prompt as the root user. Single-user mode may be a lifebuoy, or it may destroy your system, so you should be careful when using root user rights at any time. Upon completion, immediately restart to a normal multi-user mode.
Like switching to the normal multi-user run level, you can also use telinit 1 to switch to single-user mode. As described in Table 1,’s’ and’S’ are aliases for run level 1, so you can use telinit s instead.