Managing Runlevels and Boot Targets with SysVinit and Systemd #
In this tutorial, we’ll cover the key aspects of changing runlevels, boot targets, shutting down, and rebooting a system for the LPIC-1 exam. We’ll explore both SysVinit and systemd, including setting default runlevels/boot targets, alerting users, and properly terminating processes.
1. Setting the Default Runlevel or Boot Target #
SysVinit #
SysVinit uses the /etc/inittab
file to set the default runlevel.
Open
/etc/inittab
for editing:sudo nano /etc/inittab
Set the default runlevel:
Look for the line that starts withid:
and set the desired runlevel. For example, to set the default runlevel to 3:id:3:initdefault:
Systemd #
Systemd uses targets instead of runlevels. The default target is typically a symbolic link to a target file.
View the current default target:
systemctl get-default
Set the default target:
sudo systemctl set-default multi-user.target
2. Changing Runlevels/Boot Targets #
SysVinit #
Change to a different runlevel:
Use theinit
ortelinit
command followed by the runlevel number. For example, to switch to runlevel 1 (single-user mode):sudo init 1
Systemd #
Change to a different target:
Use thesystemctl isolate
command followed by the target name. For example, to switch to rescue mode (single-user mode):sudo systemctl isolate rescue.target
3. Shutting Down and Rebooting #
SysVinit #
Shutdown the system:
sudo shutdown -h now
Reboot the system:
sudo shutdown -r now
Systemd #
Shutdown the system:
sudo systemctl poweroff
Reboot the system:
sudo systemctl reboot
4. Alerting Users Before Switching Runlevels/Boot Targets #
Send a message to all logged-in users using
wall
:sudo wall "System will switch to single-user mode in 5 minutes. Please save your work."
5. Properly Terminating Processes #
SysVinit #
Gracefully terminate processes before changing runlevels:
This is typically handled by the scripts in/etc/init.d/
. For example, stopping the Apache service:sudo /etc/init.d/apache2 stop
Systemd #
Stop a service before changing targets:
Usesystemctl stop
followed by the service name. For example, stopping the Apache service:sudo systemctl stop apache2
6. Awareness of acpid
#
acpid
(Advanced Configuration and Power Interface Daemon) handles power-related events, such as closing a laptop lid or pressing a power button.
Check if
acpid
is installed and running:systemctl status acpid
Install
acpid
if necessary:sudo apt-get install acpid
Start
acpid
:sudo systemctl start acpid
Key Files, Terms, and Utilities #
/etc/inittab
: Configuration file for SysVinit to set default runlevel.shutdown
: Command to shutdown or reboot the system.init
andtelinit
: Commands to change runlevels in SysVinit./etc/init.d/
: Directory containing init scripts for SysVinit.systemd
andsystemctl
: Tools for managing systemd services and targets./etc/systemd/
and/usr/lib/systemd/
: Directories containing systemd configuration and service files.wall
: Command to send messages to all logged-in users.
By mastering these concepts and commands, you’ll be well-prepared to manage runlevels and boot targets effectively, a crucial skill for passing the LPIC-1 exam.