ObjectiveLearning about systemd emergency and rescue targets and how to boot the system into them
- No special requirements
- # - requires given linux commands to be executed with root privileges either directly as a root user or by use of
- $ - requires given linux commands to be executed as a regular non-privileged user
IntroductionSystemd has nowadays become the de facto standard
init systemfor all the major Linux distributions.
Superseding SysV and upstart, it also replaced the classical way of defining system
runlevels, using system
targets, a special type of
In this tutorial we will see how to boot an Ubuntu 18.04 system into the
rescuesystemd targets, and what kind of environment they provide to the users.
Systemd targets vs classic runlevelsSystemd has introduced the concept of
targetswhich replaced the classic system runlevels.
As an example, what was know as
runlevel 0in SysV, which represents the
haltstate of the machine, is the equivalent of the systemd
single user modefinds its systemd equivalent in the
Finally, runlevels 5 and 6, used respectively for the graphical mode and to reboot the system, have now been replaced by the
rescueand the similar
emergencytargets, are what we will talk about in this tutorial: they are very useful to fix some critical situations.
The emergency targetThe
emergencytarget is the most minimal environment the system can be booted in.
Once this target is reached, an emergency shell is started on the main console.
Other than that, only
systemditself is available to the user: only the root filesystem is mounted (in read-only mode) and no services are started (this would also mean that you will have no access to the network).
This is the target we are dropped in when the boot process cannot be successfully completed (when a filesystem check fails, for example).
How the emergency target is definedTo check how the emergency target is defined, we must inspect the dedicated systemd
We can use the
systemctl catcommand to accomplish this task:
As we can see from the output above, the emergency.target requires the related emergency.service as a dependency. Let's take a look at it too:
$ systemctl cat emergency.target # /lib/systemd/system/emergency.target # This file is part of systemd. # # systemd is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. [Unit] Description=Emergency Mode Documentation=man:systemd.special(7) Requires=emergency.service After=emergency.service AllowIsolate=yes
The definition of the service gives us very clear information.
$ systemctl cat emergency.service # /lib/systemd/system/emergency.service # This file is part of systemd. # # systemd is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. [Unit] Description=Emergency Shell Documentation=man:sulogin(8) DefaultDependencies=no Conflicts=shutdown.target Conflicts=rescue.service Before=shutdown.target Before=rescue.service [Service] Environment=HOME=/root WorkingDirectory=-/root ExecStart=-/lib/systemd/systemd-sulogin-shell emergency Type=idle StandardInput=tty-force StandardOutput=inherit StandardError=inherit KillMode=process IgnoreSIGPIPE=no SendSIGHUP=yes
First of all, the
HOMEenvironment variable is defined through the
Environmentkeyword, and corresponds to the home directory of the root user.
This is also the working directory used by default when the emergency.target is reached.
When this service is started, the
/lib/systemd/systemd-sulogin-shellexecutable is called, which, in turn, invokes
/usr/sbin/sulogin, responsible for providing us a login for the single user shell.
How to access emergency.target at bootTo force the system to boot in to the emergency.target, we must modify the grub menu.
This is a very easy operation. When the grub menu appears, just select the first entry, and press
Once you press the
ekey, you will be able to modify the boot parameters and the kernel command line.
Search for the line starting with
At this point, hit
CTRL-eto reach the end of the line, delete
$vt_handoffand add the
systemd.unit=emergency.targetdirective (you can also just use
emergencyas an alias, for SysV compatibility), so that your line will look this way:
If you now press
F10, the system will boot in to emergency mode:
The rescue.targetThis is the systemd target that can be associated to the old single user mode.
Unlike what happens with the emergency.target, when this target is reached, the base system is pulled in: all filesystems are mounted and the most basic services are launched and made available to the user.
The rescue.target is defined in the /lib/systemd/system/rescue.target file:
The rescue.target, just like what happens for the emergency.target, requires the related
# /lib/systemd/system/rescue.target # This file is part of systemd. # # systemd is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. [Unit] Description=Rescue Mode Documentation=man:systemd.special(7) Requires=sysinit.target rescue.service After=sysinit.target rescue.service AllowIsolate=yes
rescue.service, plus the
The former, just like the emergency.service, basically provides the single user login, while the latter pulls in the services required for the system initialization (unlike the emergency.target, the rescue.target is more than a simple shell).
Boot into the rescue.targetThe procedure to boot the system into rescue.target is the same we followed to make it boot into the emergency target.
The only thing that needs to be changed is the argument added to the kernel command line: instead of systemd.unit=emergency.target, we will use
systemd.unit=rescue.target; again we can also use an alias for SysV compatibility, replacing the directive with just
Once started, the system will boot into the rescue.target, where we can administer the system in single user mode:
ConclusionsWe quickly examined what the systemd emergency and rescue targets are, in what they are different, and what kind of environment the provide to the user.
We also saw how to edit the grub menu to change the kernel command line and boot the system directly in to these targets.
It's important to say that systemd targets can also be reached, from an already running system, by "isolating", them, using systemctl.
For example, running:
# systemctl isolate rescue.targetwill bring the system to the rescue target.
For a more in depth knowledge of the systemd
special units, we can consult the related, very clear manpage (SYSTEMD.SPECIAL(7)).