Systemctl Commands to Manage Systemd Service

Systemctl is a utility used by systemd for managing system and service manager. Many modern Linux distributions such as Ubuntu, Debian, Fedora, Linux Mint, OpenSuSE, Redhat has adopted systemd as their default init system.

Using systemctl you can start, stop, reload, restart service, list units, check service status, enable/disable service, manage targets (runlevels) and power management.

In this tutorial, I will show you how to use systemctl commands to manage systemd service in Linux.

1. Systemctl Start/Stop/Restart/Reload Service

To start a service in Linux, run systemctl followed by 'start' space service name.

Example:

systemctl start dnsmasq

As opposed to service command, systemctl start command does not give any output.

systemctl start service-name
Output - systemctl start service-name

To stop a service, use systemctl stop service-name. For example:

systemctl stop dnsmasq
systemctl stop service-name
Output of systemctl stop service-name

To restart a service, use systemctl restart service-name.

Example:

systemctl restart dnsmasq
systemctl restart service-name
Output of system restart service-name

To reload the configuration of service (say ssh), without restarting it, use systemctl reload service-name.

For example:

systemctl reload sshd
systemctl reload a service
Output of systemctl reload service

2. Systemctl check service status

In order to see whether a service is running or not, we can use the systemctl status check.

systemctl status dnsmasq
Service status
systemctl status service

3. Check and Enable/Disable service at boot

To enable a service at boot (this corresponds to 'chkconfig on' in sysvinit system), use systemctl enable service-name.

Example:

systemctl enable dnsmasq.service
Enable service at boot

Similarly, the services can be disabled at boot, run:

systemctl disable dnsmasq.service
Disable service at boot
Disable service at boot

In order to check if a service is enabled on boot or not, run:

systemctl is-enabled dnsmasq.service
check if a service is enabled
check if a service is enabled

4. Systemctl List Units

To list all the running units, run systemctl command without any option. The list-units option also does the same.

systemctl

or

systemctl list-units
systemctl list units

The failed units can be listed with --failed option.

systemctl --failed
systemctl failed units
systemctl failed units

To list all the active services, run:

systemctl list-units -t service
systemctl list all active services
systemctl list all active services

6. Systemctl reboot/shutdown commands

Like shutdown command,  systemctl command to put the system down, reboot or hibernate.

The following command will shutdown system and poweroff the machine and will send a notification to all logined users.

systemctl poweroff

The following command will shutdown the system but won't poweroff the machine. This will send a notification to all logined users.

systemctl halt

Poweroff the machine but won't send any notification to all logined users.

systemctl --no-wall poweroff

To display shutdown details run the below command.

journalctl -u systemd-shutdownd

7. Systemclt to managing Remote systems

Typically, all of the above systemctl commands can be used to manage a remote host with systemctl command itself. This will use ssh for communication with the remote host. All you need to do is add the user and host to systemctl command like this:

systemctl status sshd -H root@1.2.3.4
Manage remote system

8. Managing targets

Systemd has a concept of targets having a similar purpose to runlevels in sysVinit system.

The runlevels in sysVinit were mostly numeric (0,1,2,...). Here are the runlevels in sysVinit with their systemd counterparts:

0 runlevel0.target, poweroff.target
1, s, single runlevel1.target, rescue.target
2, 4 runlevel2.target, runlevel4.target, multi-user.target
3 runlevel3.target, multi-user.target
5 runlevel5.target, graphical.target
6 runlevel6.target, reboot.target
emergency emergency.target

To change the current target, type:

systemctl isolate graphical.target
Change target
Change target using systemctl

If you want to see what target you are in, you need to list all the corresponding units.

systemctl list-units --type=target
Current target
check current target

You can see "graphical.target" listed here. This is what we changed our target. Now let's change the runlevel again to multi-user.target and then analyze this output:

systemctl isolate multi-user.target
systemctl list-units --type=target
Multi-user target
change to multi-user target

To list the default target, type:

systemctl get-default
default target
List default target

The default target can be set with set-default command, type:

systemctl set-default graphical.target
Set default target
Set default target

Other useful systemd Command

Logging in systemd

The systemd has its own logging system called journald. It replaces the syslog daemon from sysVinit.

journalctl
Systemd logs
Systemd logs

To see all boot messages, run the command "journalctl -b".

journalctl -b
Boot messages
Boot messages

The following command follows the system logs in real-time (similar to tail -f).

journalctl -f
Follow logs
Follow logs

To check logs specific to a particular service or executable, use journalctl as:

journalctl /usr/sbin/dnsmasq
Service specific logs
Service specific logs

Find boot process duration

To find the systemd boot process duration with the following command:

systemd-analyze
systemd-analyze
systemd-analyze

The systemd-analyze time also shows the same information.

systemd-analyze time
systemd-analyze time
systemd-analyze time

To print a list of all running units ordered by the time taken to initialize, use systemd-analyze blame.

systemd-analyze blame
systemd-analyze blame
systemd-analyze blame

Hostnamectl command

To show and change hostname use hostnamectl command.

hostnamectl
hostnamectl
Output of hostnamectl

Conclusion

In this tutorial, we learned systemctl commands to manage systemd service in Linux distributions. I hope you enjoyed reading and please leave your suggestions in the below comment section.

Read Also:

Leave a Comment