Although this recipe uses services by their base name, they can also be addressed by their full filename. For example, sshd
can be substituted by sshd.service
.
The following steps need to be performed to successfully start or stop systemd
services:
systemd
services, as follows:~]# systemctl list-unit-files --type service UNIT FILE STATE atd.service enabled auditd.service enabled auth-rpcgss-module.service static [email protected] disabled avahi-daemon.service disabled blk-availability.service disabled brandbot.service static ... systemd-udev-trigger.service static systemd-udevd.service static systemd-update-utmp-runlevel.service static systemd-update-utmp.service static systemd-user-sessions.service static systemd-vconsole-setup.service static tcsd.service disabled [email protected] static tuned.service enabled wpa_supplicant.service disabled xinetd.service enabled 161 unit files listed.
This shows all service units available followed by information regarding whether the service is enabled or not.
systemd
services and their status, as follows:~]# systemctl list-units --type service --all UNIT LOAD ACTIVE SUB DESCRIPTION atd.service loaded active running Job spooling tools auditd.service loaded active running Security Auditing Service auth-rpcgss-module.service loaded inactive dead Kernel Module supporting RPC brandbot.service loaded inactive dead Flexible Branding Service cpupower.service loaded inactive dead Configure CPU power related crond.service loaded active running Command Scheduler cups.service loaded inactive dead CUPS Printing Service dbus.service loaded active running D-Bus System Message Bus ... systemd-...es-setup.service loaded active exited Create Volatile Files and Di systemd-...-trigger.service loaded active exited udev Coldplug all Devices systemd-udevd.service loaded active running udev Kernel Device Manager systemd-update-utmp.service loaded active exited Update UTMP about System Reb systemd-...sessions.service loaded active exited Permit User Sessions systemd-...le-setup.service loaded active exited Setup Virtual Console tuned.service loaded active running Dynamic System Tuning Daemon xinetd.service loaded active running Xinetd A Powerful Replacemen LOAD = Reflects whether the unit definition was properly loaded. ACTIVE = The high-level unit activation state, i.e. generalization of SUB. SUB = The low-level unit activation state, values depend on unit type. 103 loaded units listed. To show all installed unit files use 'systemctl list-unit-files'. ~]#
To get the status of a particular service, execute the following, substituting <service>
with the name of the service:
~]# systemctl status <service>
Here's an example:
~]# systemctl status sshd sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled) Active: active (running) since Fri 2015-07-17 09:13:55 CEST; 1 weeks 0 days ago Main PID: 11880 (sshd) CGroup: /system.slice/sshd.service +-11880 /usr/sbin/sshd -D Jul 22 12:07:31 rhel7.mydomain.lan sshd[10340]: Accepted publickey for root... Jul 22 12:12:29 rhel7.mydomain.lan sshd[10459]: Accepted publickey for root... Jul 22 12:13:33 rhel7.mydomain.lan sshd[10473]: Accepted publickey for root... Jul 24 21:27:24 rhel7.mydomain.lan sshd[28089]: Accepted publickey for root... Hint: Some lines were ellipsized, use -l to show in full. ~]#
systemd
services.To stop a systemd
service, execute the following, substituting <service>
with the name of the service:
~]# systemctl stop <service>
Here's an example:
~]# systemctl stop sshd
To start a systemd
service, execute the following, substituting <service>
with the name of the service:
~]# systemctl start <service>
~]# systemctl start sshd
systemd
services.To enable a systemd
service, execute the following, substituting <service>
with the name of the service:
~]# systemctl enable <service>
Here's an example:
~]# systemctl enable sshd ln -s '/usr/lib/systemd/system/sshd.service' '/etc/systemd/system/multi-user.target.wants/sshd.service' ~]#
To disable a systemd
service, execute the following, substituting <service>
with the name of the service:
~]# systemctl disable <service>
Here's an example:
~]# systemctl disable sshd rm '/etc/systemd/system/multi-user.target.wants/sshd.service' ~]#
Let's make the ntpd
service restart if it crashes after 1 minute.
/etc/systemd/system/ntpd.service.d
.~]# mkdir -p /etc/systemd/system/ntpd.service.d
restart.conf
and add the following to it:[Service] Restart=on-failure RestartSec=60s
~]# systemctl daemon-reload
ntpd
service by executing the following command:~]# systemctl restart ntpd
When requesting the status of a service, the most recent log entries are also shown when executed as root
.
The service status information can be seen in the following table:
In some (rare) cases, you want to prevent a service from being started, either manually or by another service; there is an option to mask the service, which is as follows:
~]# systemctl mask <service>
To unmask, execute the following:
~]# systemctl unmask <service>
When modifying service unit files (and this is not limited to services only), it is best practice to copy the original service file, which is located at /lib/systemd/system
to /etc/systemd/service
. Alternatively, you can create a directory in /etc/systemd/service
appended with .d
, in which you will create conf
files containing only the directives that you wish to add or change, as in the previous recipe. The advantage of the latter is that you don't need to keep up with changes in the original service file as it will be "updated" with whatever is located in the service.d
directory.
For more information about managing systemd
services, go to https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/sect-Managing_Services_with_systemd-Services.html.