Guide | Systemctl is a tool used by systemd to manage systems and management services. Many modern Linux distributions, such as Ubuntu, Debian, Fedora, Linux Mint, OpenSuSE, Redhat, use systemd as the default init system. |
Using systemctl, you can start, stop, reload, restart services, list service units, check service status, enable/disable services, manage run levels, and power management. In this article, I will show how to use systemctl commands to manage systemd services in Linux .
Use the systemctl command Start/Stop/Restart/Reload service
When using systemctl start the service, the command format: systemctl start [service-name]
. For example, start the firewalld service:
[root@localhost ~]# systemctl start firewalld
Contrary to the previous service
commands in the old version of linux , the systemctl start command does not output anything.
To stop the service, please use systemctl stop [service-name]
. For example, to stop the firewalld service:
[root@localhost ~]# systemctl stop firewalld
To restart the service, use systemctl restart [service-name]
, for example:
[root@localhost ~]# systemctl restart firewalld
To reload the configuration of a service (such as ssh) without restarting it, use systemctl reload [service-name]
, for example:
[root@localhost ~]# systemctl reload sshd
systemctl check service status
In order to check whether the service is running, we can use systemctl status [service-name]
to check.
[root@localhost ~]# systemctl status firewalld
Check whether the service is set to start on boot
To enable the service at boot time, use systemctl enable [service-name]
, for example:
[root@localhost ~]# systemctl enable httpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
Similarly, when disable, cancel the service at boot time:
[root@localhost ~]# systemctl disable httpd.service
You can use the is-enabled option to check whether the service is started on boot, please run:
[root@localhost ~]# systemctl is-enabled httpd.service
The output content enabled
indicates that the service disabled
is started at boot time , which means that the service is not started at boot time.
systemctl lists units
To list all active units, use list-units
options.
[root@localhost ~]# systemctl list-units
To list all active services, run:
[root@localhost ~]# systemctl list-units -t service
Use systemctl to restart and shut down the system
Like poweroff
, shutdown
command the same, systemctl command to shut down the system or reboot into hibernation.
Shut down:
[root@localhost ~]# systemctl poweroff
Restart:
[root@localhost ~]# systemctl reboot
System hibernation:
[root@localhost ~]# systemctl hibernate
Use systemclt to manage remote systems
Generally, all the above systemctl commands can be used to manage remote hosts through the systemctl command itself. This will use ssh to communicate with the remote host. As follows:
[root@localhost ~]# systemctl status httpd -H [email protected]
-H
Option, specify the username and password of the remote host.
Manage Targets
Systemd has the concept of Targets, and the purpose of these Targets is similar to the run level in the sysVinit system. The run levels in sysVinit are mainly numbers (0,1,2,-6). The following are the run levels in sysVinit and their corresponding targets in systemd:
0 runlevel0.target, poweroff.target
1 runlevel1.target, rescue.target
2,3,4 runlevel2.target, runlevel3.target,runlevel4.target, multi-user.target
5 runlevel5.target, graphical.target
6 runlevel6.target, reboot.target
If you want to view the current run level, you can use the following command:
[root@localhost ~]# systemctl get-default
multi-user.target
To set the default run level to graphical, the command is as follows:
[root@localhost ~]# systemctl set-default graphical.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.
To list all activated targets, you can use the following command:
[root@localhost ~]# systemctl list-units -t target
Other commands of the systemd tool
journalctl log collection
systemd has its own logging system, called journald. It replaces syslogd in sysVinit.
[root@localhost ~]# journalctl
To view all boot messages, run the commandjournalctl -b
[root@localhost ~]# journalctl -b
The following command tracks the system log in real time (similar to tail -f):
[root@localhost ~]# journalctl -f
Query the duration of the system startup process
[root@localhost ~]# systemd-analyze
Startup finished in 497ms (kernel) + 1.836s (initrd) + 6.567s (userspace) = 8.901s
Finally, the system startup time is 8.901 seconds.
View the start time of the service:
[root@localhost ~]# systemd-analyze blame
hostnamectl command
View host name:
[root@localhost ~]# hostnamectl
to sum up
In this article, I learned the systemctl command to manage system services in Linux distributions. Linux should be learned like this