Linux administration

Change Runlevels and Shutdown or Reboot System

Nguyen Hai Chau
Vietnam National University

Understanding Runlevels

  • A Linux system runs in one of several runlevels — modes providing different features and levels of functionality

  • Linux systems normally have seven runlevels, numbered from 0–6:

    • Three are mandatory (0 = halt, 6 = reboot, 1 = single-user)
    • Four are user-defined (2–5)
  • No consensus between administrators or distributions about how to organise the user-defined runlevels

    • Some rely (partly) on runlevels to define which major subsystems are running
    • Others prefer the flexibility of starting and stopping subsystems individually, without changing runlevel
    • In every common distribution, there is at least one user-defined runlevel which has the same services as another

Typical Runlevels

Run level Description
0 A 'transitional' run-level, used to tell the system to shut itself down safely. Once the system has shut down, it needs a manual reboot to reactivate.
1 Single-user mode, used for maintenance. Users may not log in, and many services (usually including all networking facilities) are disabled. There is only one terminal active, on which root is automatically logged in.
2-5 Multi-user modes. Some systems make all of these identical. Others disable networking (or NFS file-sharing) in runlevel 2, and/or enable a graphical login in runlevel 5 (but not in other runlevels).
6 A 'transitional' run-level, used to tell the system to reboot.

Single-User Mode and sulogin

  • Many Linux distributions use a program called sulogin to restrict access to single-user mode
  • sulogin is run when the system enters single-user mode
  • It requires the root password to be typed on the console before switching to single-user mode
    • If the password is not typed, sulogin returns the system to the normal runlevel
  • Why is sulogin needed?
    • Untrusted users may have access to the system's keyboard during bootup
    • In many configurations, this would enable them to boot the system up in single-user mode

Shutting Down and Restarting the System

  • To safely shut down the system, run the halt command as root. This is a safe shutdown: it stops all services, disables all network interfaces, and unmounts all filesystems

  • To safely reboot the system, run reboot as root. Most systems also let you hit Ctrl+Alt+Del on the console

  • Alternatively, the shutdown command allows you to schedule the power-down or reboot, to give users warning to save their work

  • Halt at 6pm:

# shutdown -h 18:00
  • Reboot thirty minutes from now:
# shutdown -r +30

Setting the Default Runlevel

  • The system's default runlevel on bootup is configured in /etc/inittab
  • To configure a default runlevel of 3, /etc/inittab should contain the line:
id:3:initdefault
  • There should be only one initdefault line in /etc/inittab

Selecting a Different Runlevel at Bootup

  • Most bootloaders, including GRUB, give you the ability to type in a kernel command line
  • Naming a runlevel on the kernel command line selects that runlevel for use on system startup
  • To start in single-user mode:
linux 1
  • To start in emergency mode:
linux -b

Emergency mode provides nothing beyond a shell to type into — useful for repairing serious system corruption

Determining the Current Runlevel

  • The runlevel command prints the system's previous and current runlevels:
$ /sbin/runlevel
N 3
  • If there is no previous runlevel (for example, if the runlevel hasn't been changed from the default), N is printed instead

Switching Runlevel

  • The system has a process named init, with pid 1, which is the ultimate ancestor of all other processes

  • init is responsible for controlling runlevels, so switching runlevels involves telling init to do something. As root, run

# telinit 1

to switch into a given runlevel

  • You can alternatively use init itself, with the same syntax:
# init 5
  • Obviously, changing runlevels should not be undertaken lightly. In particular, changing runlevel can terminate important system services, or affect users' ability to log in

Services in Each Runlevel: the init.d Directory

  • /etc contains an init.d directory, and an rcN.d directory for each runlevel N. Some distributions (notably Red Hat) put all these directories in /etc/rc.d, not directly under /etc

  • init.d contains an init script for each service that can be started

  • The rcN.d directories contain symbolic links to the init scripts

    • These symbolic links control which services are available in each runlevel

Symbolic Links in rcN.d

  • Symbolic links in the rcN.d directory are either start links or stop links

    • Start links are named SNNservice, where NN is a number and service is the name of a service
    • Stop links are named KNNservice
  • The start links for a runlevel directory indicate which services should be started when that runlevel is entered

  • Correspondingly, the stop links indicate which services should be stopped when the runlevel is entered

  • The rc shell script (usually /etc/rc.d/rc or /etc/init.d/rc) runs the relevant init script appropriately for start links and stop links

Starting or Stopping Individual Services

  • You can also start or stop an individual service without changing runlevel
  • An init script always takes an argument of start or stop to start or stop the relevant service
  • For example, if the MySQL database server has an init script /etc/init.d/mysql, you can start MySQL with
# /etc/init.d/mysql start

or stop it with

# /etc/init.d/mysql stop
  • Some init scripts also accept an argument of restart (stop and then re-start) or reload (reload the service's configuration file)

Exercise 1

  • a. Look in /etc/init.d or /etc/rc.d/init.d to see what services can be started by init.
  • b. Try running the script for crond, and use it to stop the cron service, and then start it up again.
  • c. Take a quick look at the program in a text editor (it's a small shell script) to get a rough idea of what it
  • d. Look in the rc3.d directory to see what services are killed and started when switching to runlevel 3.
  • e. Use telinit to change to single-user mode.
  • f. Once in single-user mode, use top to see what processes are left running.

Exercise 2

  • a. Reboot the machine by changing to runlevel 6.
  • b. When the GRUB prompt appears, type Tab to see a list of operating systems to boot. Type the name of the one you want followed by a space and the number 1, to indicate that you want to boot straight into single-user mode.
  • c. Change back to runlevel 3.