Use Systemctl commands to manage system services

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 servicecommands in the old version of linux , the systemctl start command does not output anything.
Use Systemctl command to manage system services Use Systemctl command to manage system services
To stop the service, please use systemctl stop [service-name]. For example, to stop the firewalld service:

[root@localhost ~]# systemctl stop firewalld

Use Systemctl command to manage system services Use Systemctl command to manage system services
To restart the service, use systemctl restart [service-name], for example:

[root@localhost ~]# systemctl restart firewalld

Use Systemctl command to manage system services Use Systemctl command to manage system services
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

Use Systemctl command to manage system services Use Systemctl command to manage system services

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

Use Systemctl command to manage system services Use Systemctl command to manage system services

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.

Use Systemctl command to manage system services Use Systemctl command to manage system services
Similarly, when disable, cancel the service at boot time:

[root@localhost ~]# systemctl disable httpd.service 

Use Systemctl command to manage system services Use Systemctl command to manage system services
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 

Use Systemctl command to manage system services Use Systemctl command to manage system services
The output content enabledindicates that the service disabledis 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-unitsoptions.

[root@localhost ~]# systemctl list-units

Use Systemctl command to manage system services Use Systemctl command to manage system services
To list all active services, run:

[root@localhost ~]# systemctl list-units -t service 

Use Systemctl command to manage system services Use Systemctl command to manage system services

Use systemctl to restart and shut down the system

Like poweroff, shutdowncommand 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]

Use Systemctl command to manage system services Use Systemctl command to manage system services
-HOption, 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

Use Systemctl command to manage system services Use Systemctl command to manage system services
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.

Use Systemctl command to manage system services Use Systemctl command to manage system services
To list all activated targets, you can use the following command:

[root@localhost ~]# systemctl list-units -t target

Use Systemctl command to manage system services Use Systemctl command to manage system services

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 

Use Systemctl command to manage system services Use Systemctl command to manage system services
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

Use Systemctl command to manage system services Use Systemctl command to manage system services

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

Use Systemctl command to manage system services Use Systemctl command to manage system services
Finally, the system startup time is 8.901 seconds.

View the start time of the service:

[root@localhost ~]# systemd-analyze blame

Use Systemctl command to manage system services Use Systemctl command to manage system services

hostnamectl command

View host name:

[root@localhost ~]# hostnamectl 

Use Systemctl command to manage system services Use Systemctl command to manage system services

to sum up

In this article, I learned the systemctl command to manage system services in Linux distributions. Linux should be learned like this

Guess you like

Origin blog.csdn.net/Linuxprobe18/article/details/111941958