linux command – systemctl

systemctl parameter description

1. Use syntax

Usage: systemctl [OPTIONS…] {COMMAND}…

2, parameter description

parameter parameter description
start immediately start the following unit
stop immediately close the following unit
restart Close immediately and start the following unit, that is, execute stop and start again
reload Do not close and then follow In the case of the unit, reload the configuration file to make the setting take effect
enable Setting the next time the unit is turned on, the following unit will be disabled Start
disable Set the following unit will not be started when the next boot
status The status of the following unit will list information such as whether it is running, whether it is powered on, etc.
is-active Is it currently running
is-enable Is there a preset to enable this unit when booting?
kill Don’t be scared by the name kill, it is actually to run The process of the unit sends a signal
show Lists the configuration of the unit.
mask Log out the unit, you will not be able to start this unit after logout
unmask Cancel logout of unit
list-units List the currently activated units according to the unit. If you add –all, the ones that are not started will be listed. (equivalent to no parameters)
list-unit-files List all the units to install and their startup status (enabled, disabled, static, mask).
–type=TYPE is the unit type, mainly service, socket, target, etc.
get-default Get the current target
set-default The target after setting becomes the default operation mode
isolate Switch to the following mode

systemctl description

Task Old Instructions New Instructions
Make a service start automatically chkconfig –level 3 httpd on systemctl enable httpd.service
Make a service Do not start automatically chkconfig –level 3 httpd off systemctl disable httpd.service
Check service status service httpd status systemctl status httpd.service (service details) systemctl is-enabled httpd.service (only shows whether Active)
Show all started services chkconfig –list systemctl list-units –type=service
Start a service service httpd start systemctl start httpd.service
Stop a service service httpd stop systemctl stop httpd.service
Restart a service service httpd restart systemctl restart httpd.service
A service reloads the configuration file service httpd reload systemctl reload httpd.service

systemctl example

List all available units

systemctl list-unit-files |more

systemctl list-units --type=target

List all loaded units

 systemctl list-units |more

View all services that can be managed by systemctl

 systemctl list-units --type=service


There are many kinds of management units available for systemctl. In daily work, we only use them to manage services. The common types of units are:

Service unit: file extension .service, used to define system services;
Target unit: file extension .target, used to simulate the realization of “running level”;
Device unit: file extension .device, used to define the device recognized by the kernel;
Mount unit: file extension .mount, used to define the mount point of the file system;
Socket unit: file extension .socket, used to identify the socket file used for inter-process communication;
Snapshot unit: file extension .snapshot, used to manage system snapshots;
Swap unit: file extension .swap, used to identify the swap device;
Automount unit: file extension .automount, used to define the automatic point device of the file system;
Path unit: file extension .path, used to define a file or directory in the file system;

Logout service

After the service is deregistered, the service cannot be started and stopped through systemctl.

[root@s153 system]# systemctl mask firewalld
Created symlink from /etc/systemd/system/firewalld.service to /dev/null.
[root@s153 system]# systemctl start firewalld
Failed to start firewalld.service: Unit is masked.

Cancel logout service

[root@s153 system]# systemctl unmask firewalld
Removed symlink /etc/systemd/system/firewalld.service.
[root@s153 system]# systemctl start firewalld

Set the service to start automatically at boot

[root@s153 system]# systemctl enable xinetd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/xinetd.service to /usr/lib/systemd/system/xinetd.service.

Cancel the service from starting at boot

[root@s153 system]# systemctl disable xinetd.service
Removed symlink /etc/systemd/system/multi-user.target.wants/xinetd.service.

View machine information

[root@s153 system]# systemctl list-machines
NAME STATE FAILED JOBS
s153 (host) running 0 0

1 machines listed.

View system environment variables

[root@s153 system]# systemctl show-environment
LANG=zh_CN.UTF-8
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin

Reload unit file

If you manually modified the unit file, you can use this command to reload it.

[root@s153 system]# systemctl daemon-reload


Create a system snapshot

[root@s153 system]# systemctl snapshot wuhs
wuhs.snapshot

Delete the specified snapshot

[root@s153 system]# systemctl delete wuhs

Check whether the service starts automatically at boot

[root@s153 system]# systemctl is-enabled xinetd.service
enabled

Kill the service

[root@s153 system]# systemctl kill xinetd
[root@s153 system]# systemctl is-failed xinetd
inactive

Enter rescue mode

[root@s153 system]# systemctl rescue

Broadcast message from root@s153 on pts/1 (Wed 2022-07-20 13:08:30 CST):

The system is going down to rescue mode NOW!
#Switch to default mode in rescue mode
[root@s153 ~]# systemctl default

Shut down the system

[root@s153 ~]# systemctl poweroff


Restart the machine

[root@s153 ~]# systemctl reboot


System Sleep

suspend suspend mode, similar to the sleep mode of the window environment, will save the state data of the system to the memory, and then shut down most of the system hardware, of course, there is no actual shutdown. When the user presses the button to wake up the machine, the system data will be restored in the memory, and then the hardware that is mostly shut down will be re-driven, and it will start to work normally! Wake up faster.

[root@s153 ~]# systemctl suspend


View system startup mode

[root@s153 boot]# systemctl get-default
multi-user.target

Set the system to start with a graphical interface

[root@s153 system]# 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.