Professional Documents
Culture Documents
Chapter 04
Units
Units are systemd objects used for organizing boot and maintenance
tasks, such as hardware initialization, socket creation, file system
mounts, and service startups.
Units have a name and a type, and they are encoded in files with names
in the form unitname.type. Some examples are tmp.mount,
sshd.service, syslog.socket, and umount.target.
(1) systemunit files that are distributed with installed packages and
located in the /usr/lib/systemd/system directory, and
(2) user unit files that are user-defined and stored in the
/etc/systemd/user directory.
Tips:
Types of Units:
Systemd categories units according to the type of resource they
describe. The easiest way to determine the type of a unit is with its type
suffix, which is appended to the end of the resource name. The
following list describes the types of units available to systemd:
RHCSA EX200
Example:
[Service]
Type=notify
EnvironmentFile=-/etc/crypto-policies/back-ends/opensshserver.config
EnvironmentFile=-/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS $CRYPTO_POLICY
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
[user1@Server1 ~]$
RHCSA EX200
Targets
Targets are simply logical collections of units. They are a special
systemd unit type with the .target file extension. Targets are used to
execute a series of units. systemd includes several predefined targets
that are described below:
Target unit files contain all information under the [Unit] section, and it
comprises the description, documentation location, and dependency
and conflict information.
RHCSA EX200
Example:
[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
[user1@Server1 ~]$
The file shows four dependencies: Requires, Wants, Conflicts, and After.
It suggests that the system must have already accomplished the
rescue.service, rescue.target, multiuser. target, and display-
manager.service levels in order to be declared running in the graphical
target.
By default, the systemctl command lists only the active units. You can
use the --all option to include the inactive units too. To list all (--all) active
and inactive services of type (-t) service:
To list all units that failed (--failed) to start at the last system boot:
To list the hierarchy of all dependencies (required and wanted units) for
the current default target:
$ systemctl list-dependencies
RHCSA EX200
To list the hierarchy of all dependencies (required and wanted units) for
a specific unit such as sshd.service:
To check whether atd is set to auto start at the next system reboot:
[root@Server1 ~]# systemctl is-enabled sshd.service
enabled
To check whether the atd service is running:
[root@Server1 ~]# systemctl is-active sshd.service
active
To stop, start and restart atd, run either of the following:
[root@Server1 ~]# systemctl stop sshd.service; systemctl start
sshd.service
[root@Server1 ~]# systemctl restart sshd.service
To show the details of the sshd service:
# systemctl show sshd.service
RHCSA EX200
For each target unit, the above output returns the target unit’s name,
load state, high-level and low-level activation states, and a short
description. Add the --all option to the above to see all loaded targets in
either active or inactive state.
RHCSA EX200
[root@Server1 ~]#
[root@Server1 ~]# systemctl -t target | grep active
basic.target loaded active active Basic System
bluetooth.target loaded active active Bluetooth
cryptsetup.target loaded active active Local Encrypted Volumes
getty.target loaded active active Login Prompts
graphical.target loaded active active Graphical Interface
local-fs-pre.target loaded active active Local File Systems (Pre)
local-fs.target loaded active active Local File Systems
multi-user.target loaded active active Multi-User System
network-online.target loaded active active Network is Online
network-pre.target loaded active active Network (Pre)
network.target loaded active active Network
nfs-client.target loaded active active NFS client services
nss-user-lookup.target loaded active active User and Group Name
Lookups
paths.target loaded active active Paths
remote-fs-pre.target loaded active active Remote File Systems (Pre)
remote-fs.target loaded active active Remote File Systems
rpc_pipefs.target loaded active active rpc_pipefs.target
rpcbind.target loaded active active RPC Port Mapper
slices.target loaded active active Slices
sockets.target loaded active active Sockets
sound.target loaded active active Sound Card
sshd-keygen.target loaded active active sshd-keygen.target
swap.target loaded active active Swap
sysinit.target loaded active active System Initialization
timers.target loaded active active Timers
25 loaded units listed. Pass --all to see loaded but inactive units, too.
[root@Server1 ~]#
RHCSA EX200
System Logging
System logging (syslog for short) is one of the most rudimentary
elements of an operating system. Its purpose is to capture messages
generated by the kernel, daemons, commands, user activities,
applications, and other events, and forwarded them to various log files,
which store them for security auditing, service malfunctioning, system
troubleshooting, or informational purposes.
• Troubleshooting
• Service Level Agreement (SLA) audits
• Baselining
• Preventative maintenance
Configuration file
The actual rsyslog configuration is managed via a configuration file in the
/etc directory. Back up the original configuration file, and then open the
/etc/rsyslog.conf file with your favorite text editor.
RHCSA EX200
Notice that the format of the messages is similar to that of the events
logged to the /var/log/messages file.
Each line begins with a timestamp followed by the system hostname,
process name with or without a PID, and the actual message.
To display detailed output for each entry, use the -o verbose option:
# journalctl -o verbose
RHCSA EX200
To view all events since the last system reboot, use the -b options:
# journalctl -b