Using Runlevels

Fedora can be booted into different runlevels, each of which starts a specific collection of software for a particular purpose. The most commonly used are runlevel 3, which starts the system with a character-based user interface, and runlevel 5, which starts the system with a graphical user interface. Table 4-11 lists the standard runlevels.

Table 4-11. Standard runlevels

RunlevelDescriptionPurpose
s (or S)Single-user maintenance modeEmergency system recovery work
0
HaltStops the system
1
Single-user modeSystem administration
2
(Multiuser without networking)(Not normally used)
3
Multiuser, character-modeNormal system operation without graphical login; useful for servers
4
(Not defined)(Not normally used)
5
GraphicalNormal system operation with graphical login.
6
RebootRestarts the system
7, 8, 9,
a, b, c
(Not defined)Available for custom purposes

The ability to choose the runlevel lets you save system resources (for example, by not running the graphical user interface when it isn’t needed) or start the system in a minimal configuration so that you can fix problems.

How Do I Do That?

You can change the runlevel on the fly, or configure your system to start in a different runlevel.

Choosing the runlevel at boot time

The Fedora boot menu can be used to specify the runlevel:

  1. Press a key (such as the spacebar) when the Fedora Core boot display appears. This will reveal the boot menu.

  2. Select the Fedora Core boot option you wish to use using the arrow keys.

  3. Press the letter a (Append). An edit display will appear that allows you to append information to the boot command line.

  4. Add a space and then the runlevel to the end of the list of boot options (for example, press space then 3 to select runlevel 3).

  5. Press Enter to boot into the runlevel that you’ve specified.

Tip

If you have configured a GRUB password, you will be prompted to enter it before changing the boot options.

Changing the runlevel after booting

Take the following steps to change the runlevel after booting:

  1. Obtain a root prompt using the su command:

  2. $ su
    Password: 
                            
                               rootPassword
                            
    #
  3. Use the init command to change to the runlevel of your choice:

  4. # init 
                            
                               3
                            

Changing the default runlevel

System administrators often configure servers to start in runlevel 3, freeing up memory to increase the server’s performance.

The default runlevel is controlled by a line in the file /etc/inittab; to change the default runlevel, edit that file using the vi editor:

  1. Obtain a root prompt using su.

  2. Start vi with the /etc/inittab file:

  3. # vi /etc/inittab
  4. Find this line in the file:

  5. id:5:initdefault:
  6. Change the second field to the default runlevel of your choice; in this case, I’ve used 3:

  7. id:3:initdefault:
  8. Save the file and exit vi. The change will take effect next time you boot the system.

Creating entries for different runlevels on the boot menu

The boot menu is configured using the file /boot/grub/grub.conf. You can edit this file so that options for various runlevels appear on the boot menu:

  1. Obtain a root prompt.

  2. Start vi with the /boot/grub/grub.conf:

  3. # vi /boot/grub/grub.conf
  4. The file will look something like this:

  5. # grub.conf generated by anaconda
    #
    # Note that you do not have to rerun grub after making changes to this file
    # NOTICE:  You have a /boot partition.  This means that
    #          all kernel and initrd paths are relative to /boot/, eg.
    #          root (hd0,1)
    #          kernel /vmlinuz-version ro root=/dev/Main/root
    #          initrd /initrd-version.img
    #boot=/dev/hdc
    default=0
    timeout=5
    splashimage=(hd0,1)/grub/splash.xpm.gz
    hiddenmenu
    title Fedora Core (2.6.17-1.2517.fc6)
            root (hd0,1)
            kernel /vmlinuz-2.6.17-1.2517.fc6 ro root=/dev/Main/root rhgb quiet
            initrd /initrd-2.6.17-1.2517.fc6.img
    title Windows XP
            rootnoverify (hd0,0)
            chainloader +1
  6. This example shows two Fedora Core entries for two different kernel versions. There may be additional entries for other operating systems (such as Windows) or additional kernels.

  7. Find a Fedora Core entry (the bold lines in the example above)—usually, the one with the latest kernel. Make an identical copy of it immediately after the original location in the file:

  8. title Fedora Core (2.6.17-1.2517.fc6)
            root (hd0,1)
            kernel /vmlinuz-2.6.17-1.2517.fc6 ro root=/dev/Main/root rhgb quiet
            initrd /initrd-2.6.17-1.2517.fc6.img
    title Fedora Core (2.6.17-1.2517.fc6)
            root (hd0,1)
            kernel /vmlinuz-2.6.17-1.2517.fc6 ro root=/dev/Main/root rhgb quiet
            initrd /initrd-2.6.17-1.2517.fc6.img
  9. Change the description of the copied section to indicate the runlevel that will be used:

  10. title Fedora Core (2.6.17-1.2517_fc6) - Runlevel 3 - Character mode
  11. On the kernel line, append the runlevel that you wish to use (this will override the default runlevel in /etc/inittab):

    kernel /vmlinuz-2.6.17-1.2517.fc6 ro root=/dev/Main/root rhgb quiet 3
  1. Optionally, change the default, timeout, or hiddenmenu options to suit your tastes.

  2. The default option specifies which of the menu entries is booted by default; the menu entries are numbered starting at 0, so you could set this line to 1 to boot the second item on the menu automatically:

  3. default=1
  4. The timeout option sets the number of seconds that the menu will be displayed before the default option is automatically chosen. To give the user 30 seconds to decide which boot option to use, change the timeout line to read:

  5. timeout=30
  6. hiddenmenu hides the menu until the user presses a key; remove the hiddenmenu line to automatically reveal the menu every time the system is booted.

  7. Save the file and exit vi. The new menu option will appear the next time you boot the system.

How Does It Work?

Once the kernel has fully started up, it runs just one program: init. All other software is started directly or indirectly by init.

If a runlevel is specified in the kernel boot options, init uses that value for the runlevel; otherwise, it obtains a runlevel from the initdefault line in /etc/inittab.

init then looks for a sysinit entry in /etc/inittab and executes the command specified:

si::sysinit:/etc/rc.d/rc.sysinit

This executes the /etc/rc.d/rc.sysinit script, which performs some basic system setup common to all runlevels.

Next, init examines the /etc/inittab file, looking for entries that contain the current runlevel in the second field and wait or respawn in the third field. For runlevel 3, it will find these lines:

l3:3:wait:/etc/rc.d/rc 3
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
6:2345:respawn:/sbin/mingetty tty6

The first line starts the script /etc/rc.d/rc with the argument 3. This in turn sequentially executes every script in /etc/rc.d/rc3.d that starts with the letter S (for start); this is how runlevel-specific software and services get started. Scripts in that same directory that start with K (for Kill) are used to stop software when switching from the runlevel.

The remaining lines listed start character-mode logins on virtual terminals 1 through 6; the respawn keyword indicates that init must restart those programs when they terminate, enabling another user to log in.

What About...

...booting without an /etc/inittab file?

If the file /etc/inittab doesn’t exist, init cannot start the system normally. Runlevel S was created specifically for this purpose; it’s the only runlevel that doesn’t require /etc/inittab, so it can be a lifesaver if that file is missing or messed up. In fact, init doesn’t even ask for a password in runlevel S; it takes you directly to a root command prompt. This is useful if you’ve forgotten the root password, but presents a huge security risk.

To protect against the unauthorized use of runlevel S, it’s a good idea to add a password entry to the boot menu. If you didn’t do this during the installation, you can add the password at any time by following these steps:

  1. Generate an encrypted password with the grub-md5-crypt command:

  2. $ grub-md5-crypt
    Password: 
                               bigsecret
                            
    Retype password: 
                               bigsecret
                            
                            $1$f1z061$j/UEYyBn0e0996w0gjq4k/
  3. The previous line in bold is the encrypted (scrambled) version of the password.

  4. Next, edit the /boot/grub/grub.conf file and add this line at the top, substituting the password generated in step 1:

  5. password --md5 $1$f1z061$j/UEYyBn0e0996w0gjq4k/
  6. When you boot the system, you will still be able to select a boot menu entry, but to perform any advanced operations (such as appending runlevel information to a boot entry) you will need to enter the password.

...using the GUI in runlevel 3?

Just because runlevel 3 doesn’t offer a graphical login screen doesn’t mean that you can’t use a graphical user interface. If you log in on a character-mode display, you can start the GUI with this command:

$ startx

To have the GUI start each time you log in, add this command to your ~/.bash_profile:

exec startx

On a server, this gives you the best of both worlds: the GUI doesn’t consume any resources when it’s not in use, but it can be started quickly any time you need it—useful when you need to look up documentation on a web site, for example.

Where Can I Learn More?

  • The manpages for init and inittab

Get Fedora Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.