Controlling The Boot Process: Selecting A Systemd Target

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Controlling the Boot Process

systemctl poweroff stops all running services, unmounts all file systems (or remounts them
read-only when they cannot be unmounted), and then powers down the system.
systemctl reboot stops all running services, unmounts all file systems, and then reboots the
system.
systemctl halt are also available to stop the system, but unlike poweroff, these commands do
not power off the system; they bring a system down to a point where it is safe to power it off
manually.

You can also use the shorter version of these commands, poweroff, reboot and halt which
are symbolic links to their systemctl equivalents.

Selecting a Systemd Target


A systemd target is a set of systemd units that the system should start to reach a desired
state. The following table lists the most important targets.
Table 10.1. Commonly Used Targets

Target Purpose
graphical.target System supports multiple users, graphical- and text-based logins.
multi-
user.target
System supports multiple users, text-based logins only.

rescue.target sulogin prompt, basic system initialization completed.


sulogin prompt, initramfs pivot complete, and system root
emergency.target
mounted on / read only.

A target can be a part of another target. For example, the graphical.target includes
multi-user.target, which in turn depends on basic.target and others. You can view
these dependencies with the following command.

[user@host ~]$ systemctl list-dependencies graphical.target | grep target


graphical.target
* └─multi-user.target
* ├─basic.target
* │ ├─paths.target
* │ ├─slices.target

To list the available targets, use the following command.

[user@host ~]$ systemctl list-units --type=target --all

Selecting a Target at Runtime


On a running system, administrators can switch to a different target using the systemctl
isolate command.

[root@host ~]# systemctl isolate multi-user.target

Not all targets can be isolated. You can only isolate targets that have AllowIsolate=yes set
in their unit files. For example, you can isolate the graphical target, but not the cryptsetup
target.

[user@host ~]$ systemctl cat graphical.target #


/usr/lib/systemd/system/graphical.target

...output omitted...

[Unit]

Description=Graphical Interface

Documentation=man:systemd.special(7)

Requires=multi-user.target

Wants=display-manager.service

Conflicts=rescue.service rescue.target

After=multi-user.target rescue.service rescue.target display-manager.service

AllowIsolate=yes

Setting a Default Target


When the system starts, systemd activates the default.target target. Normally the default
target in /etc/systemd/system/ is a symbolic link to either graphical.target or multi-
user.target. Instead of editing this symbolic link by hand, the systemctl command
provides two subcommands to manage this link: get-default and set-default.

[root@host ~]# systemctl get-default


multi-user.target
[root@host ~]# systemctl set-default graphical.target
Removed /etc/systemd/system/default.target.
Created symlink /etc/systemd/system/default.target ->
/usr/lib/systemd/system/graphical.target.
[root@host ~]# systemctl get-default
graphical.target

Selecting a Different Target at Boot Time


To select a different target at boot time, append the systemd.unit=target.target option to
the kernel command line from the boot loader.
For example, to boot the system into a rescue shell where you can change the system
configuration with almost no services running, append the following option to the kernel
command line from the boot loader.

systemd.unit=rescue.target

1. On workstation, open a terminal and confirm that the default target is


graphical.target.

[student@workstation ~]$ systemctl get-default


graphical.target

2. On workstation, switch to the multi-user target manually without rebooting. Use


the sudo command and if prompted, use student as the password.

[student@workstation ~]$ sudo systemctl isolate multi-user.target


[sudo] password for student: student
3. Configure workstation to automatically boot into the multi-user target, and then reboot
workstation to verify. When done, change the default systemd target back to the Reboot
workstation.
Use the systemctl set-default command to set the default target.

[root@workstation ~]# systemctl set-default multi-user.target

Reboot workstation.

[root@workstation ~]# systemctl reboot

Notice that after reboot the system presents a text-based console and not a graphical login
anymore.

Set the default systemd target back to the graphical target.

[root@workstation ~]# systemctl set-default graphical.target

In this second part of the exercise, you are practicing the rescue mode.
Access the boot loader by rebooting workstation again. From within the boot loader menu,
boot into the rescue target.
Initiate the reboot.

[root@workstation ~]# systemctl reboot

When the boot loader menu appears, press any key to interrupt the countdown (except
Enter, which would initiate a normal boot).
Use the cursor keys to highlight the default boot loader entry.
Press e to edit the current entry.
Using the cursor keys, navigate to the line that starts with linux.
Press End to move the cursor to the end of the line.
Append systemd.unit=rescue.target to the end of the line.
Press Ctrl+x to boot using the modified configuration.
Log in to rescue mode. The root password is redhat You may need to hit enter to
get a clean prompt.
Confirm that in rescue mode, the root file system is in read/write mode.

[root@workstation ~]# mount


...output omitted...
/dev/vda1 on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
...output omitted...

Press Ctrl+d to continue with the boot process.


The system presents a graphical login. Log in as student using student as the
password.

Resetting the Root Password from the Boot Loader

To access that root shell, follow these steps:


1. Reboot the system.
2. Interrupt the boot loader countdown by pressing any key, except Enter.
3. Move the cursor to the kernel entry to boot.
4. Press e to edit the selected entry.
5. Move the cursor to the kernel command line (the line that starts with linux).
6. Append rd.break. With that option, the system breaks just before the system hands
control from the initramfs to the actual system.
7. Press Ctrl+x to boot with the changes.
At this point, the system presents a root shell, with the actual root file system on the disk
mounted read-only on /sysroot. Because troubleshooting often requires modification to the
root file system, you need to change the root file system to read/write. The following step
shows how the remount,rw option to the mount command remounts the file system with the
new option (rw) set.

To reset the root password from this point, use the following procedure:
1. Remount /sysroot as read/write.

switch_root:/# mount -o remount,rw /sysroot

2. Switch into a chroot jail, where /sysroot is treated as the root of the file-system
tree.

switch_root:/# chroot /sysroot

3. Set a new root password.


sh-4.4# passwd root

4. Make sure that all unlabeled files, including /etc/shadow at this point, get relabeled
during boot.

sh-4.4# touch /.autorelabel

5. Type exit twice. The first command exits the chroot jail, and the second command
exits the initramfs debug shell.
At this point, the system continues booting, performs a full SELinux relabel, and then reboots
again.

Guided Exercise: Repairing File System


Issues at Boot
In this exercise, you will recover a system from a misconfiguration in /etc/fstab that
causes the boot process to fail.
You should be able to diagnose /etc/fstab issues and use emergency mode to
recover the system.

1.
1. When the boot-loader menu appears, press any key to interrupt the countdown,
except Enter.
2. Looking at the error from the previous boot, it appears that at least parts of the system
are still functioning. Because you know the root password, redhat, attempt an
emergency boot.
1. Use the cursor keys to highlight the default boot loader entry.
2. Press e to edit the current entry.
3. Use the cursor keys to navigate to the line that starts with linux.
4. Press End to move the cursor to the end of the line.
5. Append systemd.unit=emergency.target to the end of the line.
6. Press Ctrl+x to boot using the modified configuration.
3. Log in to emergency mode. The root password is redhat.

Give root password for maintenance


(or press Control-D to continue): redhat
[root@servera ~]#

4. Determine which file systems are currently mounted.


[root@servera ~]# mount
...output omitted...
/dev/vda1 on / type xfs (ro,relatime,seclabel,attr2,inode64,noquota)
...output omitted...

Notice that the root file system is mounted read-only.


5. Remount the root file system read/write.

[root@servera ~]# mount -o remount,rw /

6. Use the mount -a command to attempt to mount all the other file systems. With the
--all (-a) option, the command mounts all the file systems listed in /etc/fstab that
are not yet mounted.

[root@servera ~]# mount -a


mount: /RemoveMe: mount point does not exist.

7. Edit /etc/fstab to fix the issue.


Remove or comment out the incorrect line.

[root@servera ~]# vim /etc/fstab


...output omitted...
# /dev/sdz1 /RemoveMe xfs defaults 0 0

Update systemd for the system to register the new /etc/fstab configuration.

[root@servera ~]# systemctl daemon-reload


[root@servera ~]#

8. Verify that your /etc/fstab is now correct by attempting to mount all entries.

[root@servera ~]# mount -a


[root@servera ~]#

9. Reboot the system and wait for the boot to complete. The system should now boot
normally.

[root@servera ~]# systemctl reboot


Lab: Controlling the Boot Process
1) On serverb, reset the root password to redhat.
Locate the icon for the serverb console, as appropriate for your classroom environment.
Work from that console.
Send a Ctrl+Alt+Del to your system using the relevant button or menu entry.
When the boot-loader menu appears, press any key to interrupt the countdown, except
Enter.
Use the cursor keys to highlight the default boot loader entry.
Press e to edit the current entry.
Use the cursor keys to navigate to the line that starts with linux.
Press End to move the cursor to the end of the line.
Append rd.break to the end of the line.
Press Ctrl+x to boot using the modified configuration.
At the switch_root prompt, remount the /sysroot file system read/write, then use
chroot to go into a chroot jail at /sysroot.

switch_root:/# mount -o remount,rw /sysroot


switch_root:/# chroot /sysroot

Set the root password to redhat.

sh-4.4# passwd root


Changing password for user root.
New password: redhat
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: redhat
passwd: all authentication tokens updated successfully.

Configure the system to automatically perform a full SELinux relabel after boot.

sh-4.4# touch /.autorelabel

Type exit twice to continue booting your system. The system fails to boot because of
an issue you resolve in the next step.

2) The system fails to boot. A start job does not seem to complete. From the console, fix the
issue.
Boot the system into emergency mode. To do so, reboot serverb by sending a
Ctrl+Alt+Del to your system using the relevant button or menu entry.

When the boot-loader menu appears, press any key to interrupt the countdown, except
Enter.
Use the cursor keys to highlight the default boot loader entry.
Press e to edit the current entry.
Use the cursor keys to navigate to the line that starts with linux.
Press End to move the cursor to the end of the line.
Append systemd.unit=emergency.target to the end of the line.
Press Ctrl+x to boot using the modified configuration.
Log in to emergency mode. The root password is redhat.

Give root password for maintenance


(or press Control-D to continue): redhat
[root@serverb ~]#

Remount the / file system read/write.

[root@serverb ~]# mount -o remount,rw /

Use the mount -a command to attempt to mount all the other file systems.

[root@serverb ~]# mount -a


mount: /olddata: can't find UUID=4d5c85a5-8921-4a06-8aff-80567e9689bc.

Edit /etc/fstab to remove or comment out the incorrect line.

[root@serverb ~]# vim /etc/fstab


...output omitted...
#UUID=4d5c85a5-8921-4a06-8aff-80567e9689bc /olddata xfs defaults 0 0

Update systemd for the system to register the new /etc/fstab configuration.

[root@serverb ~]# systemctl daemon-reload


[root@serverb ~]#

Verify that your /etc/fstab is now correct by attempting to mount all entries.

[root@serverb ~]# mount -a


[root@serverb ~]#

Reboot the system and wait for the boot to complete. Because you created the
/.autorelabel file in the first step, after setting the root password, the system runs
an SELinux relabel, then reboots again by itself. The system should now boot
normally.

[root@serverb ~]# systemctl reboot

3) Change the default systemd target on serverb for the system to automatically start a
graphical interface when it boots.
No graphical interface is installed yet on serverb. For this exercise, only set the default
target and do not install the packages.
Log in to serverb as the root user. Use redhat as the password.
Use the systemctl set-default command to set graphical.target as the default
target.

[root@serverb ~]# systemctl set-default graphical.target

1. Use the systemctl get-default command to verify your work.


2. [root@serverb ~]# systemctl get-default

graphical.target

3. Log off from serverb.

[root@serverb ~]# exi

You might also like