When a Linux system boots, it enters its default runlevel and runs the startup scripts associated with that runlevel. You can also switch between runlevels – for example, there’s a runlevel designed for recovery and maintenance operations.
Traditionally, Linux used System V-style init scripts – while new init systems will eventually obsolete traditional runlevels, they haven’t yet. For example, Ubuntu’s Upstart system still uses traditional System V-style scripts.
What’s a Runlevel?When a Linux system boots, it launches the init processes. init is responsible for launching the other processes on the system. For example, when you start your Linux computer, the kernel starts init, and init executes the startup scripts to initialize your hardware, bring up networking, start your graphical desktop.
However, there isn’t just one single set of startup scripts init executes. There are multiple run levels with their own startup scripts – for example, one runlevel may bring up networking and launch the graphical desktop, while another runlevel may leave networking disabled and skip the graphical desktop. This means you can drop from “graphical desktop mode” to “text console mode without networking” with a single command, without manually starting and stopping different services.
More specifically, init runs the scripts located in a specific directory that corresponds to the runlevel. For example, when you enter runlevel 3 on Ubuntu, init runs the scripts located in the /etc/rc3.d directory.
The RunlevelsSome runlevels are standard between Linux distributions, while some runlevels vary from distribution to distribution.
The following runlevels are standard:
- 0 – Halt (Shuts down the system.)
- 1 – Single User Mode (The system boots into superuser mode without starting daemons or networking. Ideal for booting into a recovery or diagnostics environment.)
- 6 – Reboot
Switching to a Different RunlevelTo switch to a different runlevel while the system is already running, use the following command:
sudo telinit #Replace # with the number of the runlevel you want to switch to. Omit sudo and run the command as root if you’re running a distribution that doesn’t use sudo.
Booting Directly to a Specific RunlevelYou can select a runlevel to boot into from the boot loader – Grub, for example. At the start of the boot process, press a key to access Grub, select your boot entry, and press e to edit it.
While Ubuntu’s Upstart daemon still emulates the SystemV init system, much of this information will change in the future. For example, Upstart is event-based – it can stop and start services when events occur (for example, a service could start when a hardware device is connected to the system and stop when the device is removed.) Fedora also has its own successor to init, systemd.