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.
systemctl start dnsmasq
As opposed to service command, systemctl start command does not give any output.
To stop a service, use systemctl stop service-name. For example:
systemctl stop dnsmasq
To restart a service, use systemctl restart service-name.
systemctl restart dnsmasq
To reload the configuration of service (say ssh), without restarting it, use systemctl reload service-name.
systemctl reload sshd
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
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.
systemctl enable dnsmasq.service
Similarly, the services can be disabled at boot, run:
systemctl disable dnsmasq.service
In order to check if a service is enabled on boot or not, run:
systemctl is-enabled dnsmasq.service
4. Systemctl List Units
To list all the running units, run systemctl command without any option. The list-units option also does the same.
The failed units can be listed with --failed option.
To list all the active services, run:
systemctl list-units -t service
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.
The following command will shutdown the system but won't poweroff the machine. This will send a notification to all logined users.
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 email@example.com
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
If you want to see what target you are in, you need to list all the corresponding units.
systemctl list-units --type=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
To list the default target, type:
The default target can be set with set-default command, type:
systemctl set-default graphical.target
Other useful systemd Command
Logging in systemd
The systemd has its own logging system called journald. It replaces the syslog daemon from sysVinit.
To see all boot messages, run the command "journalctl -b".
The following command follows the system logs in real-time (similar to tail -f).
To check logs specific to a particular service or executable, use journalctl as:
Find boot process duration
To find the systemd boot process duration with the following command:
The systemd-analyze time also shows the same information.
To print a list of all running units ordered by the time taken to initialize, use systemd-analyze blame.
To show and change hostname use hostnamectl command.
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.