101.3: Change runlevels || boot targets and shutdown || reboot system

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.

  1. Open /etc/inittab for editing:

    sudo nano /etc/inittab
    
  2. Set the default runlevel:
    Look for the line that starts with id: 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.

  1. View the current default target:

    systemctl get-default
    
  2. Set the default target:

    sudo systemctl set-default multi-user.target
    

2. Changing Runlevels/Boot Targets #

SysVinit #

  1. Change to a different runlevel:
    Use the init or telinit command followed by the runlevel number. For example, to switch to runlevel 1 (single-user mode):

    sudo init 1
    

Systemd #

  1. Change to a different target:
    Use the systemctl 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 #

  1. Shutdown the system:

    sudo shutdown -h now
    
  2. Reboot the system:

    sudo shutdown -r now
    

Systemd #

  1. Shutdown the system:

    sudo systemctl poweroff
    
  2. Reboot the system:

    sudo systemctl reboot
    

4. Alerting Users Before Switching Runlevels/Boot Targets #

  1. 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 #

  1. 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 #

  1. Stop a service before changing targets:
    Use systemctl 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.

  1. Check if acpid is installed and running:

    systemctl status acpid
    
  2. Install acpid if necessary:

    sudo apt-get install acpid
    
  3. 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 and telinit: Commands to change runlevels in SysVinit.
  • /etc/init.d/: Directory containing init scripts for SysVinit.
  • systemd and systemctl: 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.