# Change runlevels / boot targets and shutdown or reboot system

> Move between runlevels and systemd targets, pick the default, and shut down or reboot without surprising anyone.

Source: https://aminbytes.dev/bytes/system-architecture/runlevels-and-boot-targets/


Sometimes you need a machine in a different state: text mode instead of a desktop to save memory on a server, a minimal single-user mode to repair a filesystem, or a clean reboot after a kernel update. Doing that well also means warning the people logged in and letting programs close properly, so nobody loses work.

Linux has two vocabularies for these states. Older **SysVinit** systems use numbered **runlevels**. **systemd** uses named **targets**. The exam asks about both, and systemd still understands the old commands, so it pays to know how they map.

> [!NOTE]
> You need a real Linux machine or VM for this byte, with root or `sudo`. A Docker container has no systemd as PID 1, so `systemctl` answers with `System has not been booted with systemd as init system (PID 1). Can't operate.`

## Runlevels (SysVinit)

A runlevel is a number from 0 to 6. Each one defines which services run.

| Runlevel | Red Hat style (RHEL 6 and older) | Debian (before systemd) |
| --- | --- | --- |
| 0 | halt (power off) | halt |
| 1 (or `S`) | single-user mode: root only, no network | single-user |
| 2 | multi-user, no network file sharing (NFS) | full multi-user, the default |
| 3 | full multi-user, text mode | same as 2 |
| 4 | unused, free for your own setup | same as 2 |
| 5 | multi-user with graphical login | same as 2 |
| 6 | reboot | reboot |

0, 1 and 6 mean the same everywhere. 2 to 5 depended on the distribution, which was one of the annoyances systemd cleaned up.

Check where you are with `runlevel` (previous and current; `N` means "none") or `who -r`:

```bash
runlevel
who -r
```

```console
N 5
         run-level 5  2026-12-01 09:14
```

### /etc/inittab

On SysVinit, `/sbin/init` reads `/etc/inittab`. Each line has the format `id:runlevels:action:process`. A typical Red Hat style file:

```text {title="/etc/inittab"}
# default runlevel
id:3:initdefault:

# system initialization, runs once at boot
si::sysinit:/etc/rc.d/rc.sysinit

# run the scripts for each runlevel
l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l3:3:wait:/etc/rc.d/rc 3
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6

# what Ctrl+Alt+Del does
ca::ctrlaltdel:/sbin/shutdown -t3 -r now

# login prompts on the text consoles, restarted when they exit
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
```

The **default runlevel** is the `initdefault` line. Change `3` to `5` there and the next boot starts graphically. Debian's version looks the same but uses `/etc/init.d/rcS` and `/etc/init.d/rc` and `getty` instead of `mingetty`.

### /etc/init.d/ and the rc directories

Each service has a script in `/etc/init.d/` that understands `start`, `stop`, `restart` and `status`:

```bash
sudo /etc/init.d/ssh restart
sudo service ssh restart     # the same, through a wrapper
```

Which scripts run in which runlevel is decided by symbolic links in `/etc/rc0.d/` to `/etc/rc6.d/` (on Red Hat: `/etc/rc.d/rc3.d/` and so on):

```bash
ls /etc/rc3.d/
```

```console
K01apache-htcacheclean  S01rsyslog  S02cron  S02ssh  S03apache2
```

When you enter a runlevel, the `K` (kill) links run with `stop`, then the `S` (start) links run with `start`. The number sets the order. You manage these links with `update-rc.d` on Debian or `chkconfig` on Red Hat, not by hand.

### Changing runlevel: init and telinit

```bash
sudo telinit 1     # go to single-user mode
sudo telinit 3     # back to multi-user text mode
sudo init 6        # reboot (init with a number does the same as telinit)
sudo telinit q     # re-read /etc/inittab after editing it
```

`telinit` tells the running init to switch. You'll see it in older docs as `init 0` for power off and `init 6` for reboot.

## Targets (systemd)

systemd groups units into **targets**. A target is a unit that just pulls in other units, so `multi-user.target` means "everything needed for a working text-mode multi-user system". Targets can depend on each other: `graphical.target` includes all of `multi-user.target` plus a display manager.

| systemd target | Old runlevel | What you get |
| --- | --- | --- |
| `poweroff.target` | 0 | power off |
| `rescue.target` | 1, `S`, single | root shell, local filesystems mounted, no network services |
| `emergency.target` | (none) | root shell, root filesystem read-only, nearly nothing else |
| `multi-user.target` | 2, 3, 4 | full system, text logins, network services |
| `graphical.target` | 5 | multi-user plus a graphical login |
| `reboot.target` | 6 | reboot |

For compatibility, systemd ships `runlevel0.target` to `runlevel6.target` as links to these. That's why `telinit 3`, `init 6` and `runlevel` still work on systemd machines.

### The default target

```bash
systemctl get-default
sudo systemctl set-default multi-user.target
```

```console
graphical.target
Removed "/etc/systemd/system/default.target".
Created symlink /etc/systemd/system/default.target → /usr/lib/systemd/system/multi-user.target.
```

The default is just a symbolic link called `default.target`. `set-default` replaces it. This is the systemd version of editing `initdefault` in `/etc/inittab`. (Debian 12 prints `/lib/systemd/system/...`, which is the same directory.)

### Changing target now

```bash
sudo systemctl isolate multi-user.target   # stop the desktop, stay in text mode
sudo systemctl isolate graphical.target    # bring the desktop back
sudo systemctl rescue                      # shortcut for isolate rescue.target
sudo systemctl emergency                   # shortcut for isolate emergency.target
systemctl list-units --type=target         # targets that are active right now
```

`isolate` starts the target and everything it needs, and **stops everything else**. Only targets that allow it (`AllowIsolate=yes`) can be isolated, which is true for the ones in the table.

### Single-user mode at boot

To get to rescue mode when the system won't boot normally, edit the `linux` line in the GRUB menu (press `e`) and add one of these at the end, then press `Ctrl+X`:

- `systemd.unit=rescue.target`, or simply `single`, `1` or `S`
- `systemd.unit=emergency.target`, or `emergency`, if even rescue mode fails

Both modes ask for the root password. (Side note: Ubuntu locks the root account by default, so rescue mode answers "Cannot open access to console, the root account is locked." Use the `init=/bin/bash` trick from the previous byte, or the recovery entry in Ubuntu's GRUB menu.)

### Where systemd keeps its files

| Path | What's there |
| --- | --- |
| `/usr/lib/systemd/` | shipped by packages: the `systemd` program itself, helpers, and the vendor unit files in `/usr/lib/systemd/system/`. Don't edit these: updates overwrite them. |
| `/etc/systemd/` | the admin's place: `system.conf`, `logind.conf`, `journald.conf`, and your own units, links and overrides in `/etc/systemd/system/` |
| `/run/systemd/` | runtime state, gone after reboot |

A unit in `/etc/systemd/system/` wins over one with the same name in `/usr/lib/systemd/system/`. That's how `default.target` in `/etc` overrides the vendor default. `systemctl edit ssh` creates a small override file in `/etc/systemd/system/ssh.service.d/` for you. On Debian and Ubuntu, `/lib/systemd/` is the same directory as `/usr/lib/systemd/`.

## Shutting down and rebooting

### shutdown

`shutdown` is the polite way: it warns users, blocks new logins, and lets the system stop everything in order.

```bash
sudo shutdown -h now                         # power off now
sudo shutdown -r now                         # reboot now
sudo shutdown -r +10 "Kernel update, back in 5 minutes"
sudo shutdown -h 22:30 "Maintenance tonight"
sudo shutdown -c                             # cancel a planned shutdown
sudo shutdown -k +5 "Test: just a warning"   # only send the warning, don't shut down
```

| Option | Meaning |
| --- | --- |
| `-h` | halt; on systemd this means power off |
| `-P` | power off |
| `-H` | halt without powering off |
| `-r` | reboot |
| `-c` | cancel a scheduled shutdown |
| `-k` | send the warning only, don't actually go down |
| `--no-wall` | don't send a message |

The time is `now`, `+minutes`, or `hh:mm`. With no time at all, systemd's `shutdown` waits one minute. Anything after the time is the message sent to every terminal:

```console
Reboot scheduled for Mon 2026-12-01 10:35:00 CET, use 'shutdown -c' to cancel.
```

Five minutes before the shutdown, it creates a `nologin` file (`/run/nologin` on systemd, `/etc/nologin` on SysVinit) so that only root can still log in.

### poweroff, reboot, halt and systemctl

```bash
sudo poweroff      # same as systemctl poweroff
sudo reboot        # same as systemctl reboot
sudo halt          # stop the system, but leave the power on
sudo systemctl poweroff
sudo systemctl reboot
sudo systemctl suspend      # sleep to RAM
sudo systemctl hibernate    # sleep to disk
```

These act right away, with no delay and no warning to other users. On a shared server, prefer `shutdown` with a time and a message. And `init 0` and `init 6` still work as SysVinit-style shortcuts for power off and reboot.

### Warning users with wall

`wall` ("write all") sends a message to the terminal of every logged-in user. `shutdown` uses it for you, but you can call it yourself before any disruptive change:

```bash
sudo wall "Restarting the database at 14:00. Please save your work."
echo "Disk cleanup in 10 minutes" | sudo wall
```

```console
Broadcast message from root@web01 (pts/0) (Mon Dec  1 13:50:02 2026):

Restarting the database at 14:00. Please save your work.
```

Users can refuse messages with `mesg n`, but messages from root still get through.

## Ending processes properly

A clean shutdown doesn't just cut the power. Programs get the chance to write their data and close files:

1. Services are stopped in order. systemd stops units in reverse dependency order. SysVinit runs the `K` scripts in `/etc/rc0.d/` or `/etc/rc6.d/`.
2. Every remaining process gets **SIGTERM** (signal 15): "please finish and exit".
3. Processes that are still alive after a grace period get **SIGKILL** (signal 9): killed by the kernel, no cleanup possible. systemd waits 90 seconds by default (`DefaultTimeoutStopSec` in `/etc/systemd/system.conf`).
4. Filesystems are unmounted or remounted read-only, then the system powers off or reboots.

Do the same when you stop something by hand: try `systemctl stop name` or `kill PID` (SIGTERM) first, and only use `kill -9 PID` if it refuses to go. (Signals and `kill` are covered in 103.5.)

> [!WARNING]
> Pulling the power or holding the power button skips all of this. Programs lose unsaved data and filesystems need a check at the next boot.

## acpid and the power button

**ACPI** (Advanced Configuration and Power Interface) is how the firmware tells the operating system about power events: the power button was pressed, the laptop lid closed, the battery is low. **acpid** is a classic daemon that listens for these events and runs scripts for them. Its rules live in `/etc/acpi/events/`:

```text {title="/etc/acpi/events/powerbtn"}
event=button/power
action=/etc/acpi/powerbtn.sh
```

`acpi_listen` shows events as they arrive, so you can see what to match on.

On current systemd distributions you usually don't need acpid: **systemd-logind** handles the power key, the suspend key and the lid switch itself, configured in `/etc/systemd/logind.conf` (`HandlePowerKey=poweroff`, `HandleLidSwitch=suspend`). Ctrl+Alt+Del is handled by `ctrl-alt-del.target`, which points to `reboot.target`. Run `sudo systemctl mask ctrl-alt-del.target` to disable it on a server.

## Exam checklist

- Runlevels: 0 halt, 1/S single-user, 2-5 multi-user (Red Hat: 3 text, 5 graphical), 6 reboot. `runlevel` shows previous and current.
- SysVinit default: `id:N:initdefault:` in `/etc/inittab`. Switch with `telinit N` or `init N`. Re-read inittab with `telinit q`.
- `/etc/init.d/` holds the service scripts. `/etc/rcN.d/` holds `S` and `K` links for each runlevel.
- systemd targets: `poweroff`, `rescue`, `multi-user`, `graphical`, `reboot`, plus `emergency`. `runlevelN.target` links exist for compatibility.
- `systemctl get-default` and `set-default` for the boot target (a `default.target` link in `/etc/systemd/system/`). `systemctl isolate` to switch now. `systemctl rescue` for single-user.
- Vendor units in `/usr/lib/systemd/system/`, admin units and overrides in `/etc/systemd/system/`. `/etc` wins.
- `shutdown -h|-r|-P|-H time "message"`, `shutdown -c` to cancel, `shutdown -k` to only warn. A nologin file blocks logins 5 minutes before.
- `wall` broadcasts a message to all logged-in terminals.
- Clean stop: SIGTERM first, SIGKILL only after a timeout.
- acpid runs actions for ACPI events (power button, lid). On systemd, logind usually handles them.


## Byte check

1. Which command makes a systemd machine boot into text mode (no graphical login) from now on?
   - `systemctl isolate multi-user.target`
   - **`systemctl set-default multi-user.target`** (correct)
   - `telinit 3`
   `set-default` changes the `default.target` link, which is what the next boot uses. `isolate` and `telinit 3` only switch the running system.

2. On a SysVinit system, which line in `/etc/inittab` sets the default runlevel to 3?
   - **`id:3:initdefault:`** (correct)
   - `default:3:respawn:`
   - `l3:3:wait:/etc/rc.d/rc 3`
   The `initdefault` action sets the default runlevel, and its runlevel field holds the number. The `l3` line only says which script runs when you enter runlevel 3.

3. You run `shutdown -r +15 "Kernel update"`. What happens?
   - The system reboots now and shows the message after boot
   - **Logged-in users are warned, new logins are blocked shortly before, and the system reboots in 15 minutes** (correct)
   - Only users in the wheel group are told, then the system halts
   `shutdown` broadcasts the message to all terminals, creates a nologin file 5 minutes before the time, and reboots at +15. `shutdown -c` cancels it.


