记录:342
场景:在CentOS 7.9操作系统上,使用systemctl命令启动、停止、重启服务;使用systemctl命令设置开机启动、开机关闭服务等功能。使用systemctl命令查看系统上安装的服务信息。
版本:
操作系统:CentOS 7.9
1.命令应用
(1)查看系统上已安装的服务
命令:systemctl list-unit-files
解析:查看系统上已经安装的全部服务service。服务状态包括static、enabled、enabled等。
命令:systemctl list-unit-files | grep firewalld
解析:使用grep命令,搜索服务名包含firewalld关键字的服务。
(2)查看开机禁用的服务
命令:systemctl list-unit-files | grep disabled
解析:查看所有开机禁用的服务,只要服务是disabled,开机不会自动启动服务,如需使用,需手动启动服务。
(3)查看开机启用的服务
命令:systemctl list-unit-files | grep enabled
解析:查看所有开机启动的服务,只要服务是enabled,开机会自动启动服务。
(4)查看服务状态
查看状态:systemctl status 服务名称
查看状态:systemctl status firewalld
查看服务状态:systemctl status firewalld.service
解析:以firewalld.service为例,查看防火墙状态,如果是Active: active (running),则已经开启防火墙。如果是Active: inactive (dead),则已关闭防火墙。使用时可以不用加上.service后缀。
(5)开启服务
开启服务:systemctl start 服务名称
开启防火墙:systemctl start firewalld
解析:以firewalld.service为例,开启防火墙服务。
(6)停止服务
关闭服务:systemctl stop 服务名称
关闭防火墙:systemctl stop firewalld
解析:以firewalld.service为例,关闭防火墙服务。
(7)重启服务
重启服务:systemctl stop 服务名称
重启防火墙:systemctl restart firewalld
解析:以firewalld.service为例,重启防火墙服务。
(8)设置服务开机启动
设置服务开机启动:systemctl enable 服务名称
开机启用防火墙:systemctl enable firewalld
解析:以firewalld.service为例,开机启用防火墙。
(9)设置服务开机禁用
设置服务开机禁用:systemctl disable 服务名称
设置开机禁用防火墙:systemctl disable firewalld
解析:以firewalld.service为例,设置开机禁用防火墙,主机启动时,就会关闭防火墙。
(10)查看服务单元信息
命令:systemctl list-units
解析:查看所有服务信息、包括UNIT、LOAD、ACTIVE、SUB、DESCRIPTION信息。
(11)查看服务的依赖关系
命令:systemctl list-dependencies sshd
解析:查看服务依赖关系,比如查看sshd服务关联的服务。
(12)查看全部服务信息
命令:systemctl -a
解析:查看全部服务信息。
systemctl list-dependencies sshd
2.命令帮助手册
命令:systemctl --help
解析:查看systemctl支持全部命令和选项,在实际工作中,查看这个手册应该是必备之选。
systemctl [OPTIONS...] {COMMAND} ...
Query or send control commands to the systemd manager.
-h --help Show this help
--version Show package version
--system Connect to system manager
-H --host=[USER@]HOST
Operate on remote host
-M --machine=CONTAINER
Operate on local container
-t --type=TYPE List units of a particular type
--state=STATE List units with particular LOAD or SUB or ACTIVE state
-p --property=NAME Show only properties by this name
-a --all Show all loaded units/properties, including dead/empty
ones. To list all units installed on the system, use
the 'list-unit-files' command instead.
-l --full Don't ellipsize unit names on output
-r --recursive Show unit list of host and local containers
--reverse Show reverse dependencies with 'list-dependencies'
--job-mode=MODE Specify how to deal with already queued jobs, when
queueing a new job
--show-types When showing sockets, explicitly show their type
-i --ignore-inhibitors
When shutting down or sleeping, ignore inhibitors
--kill-who=WHO Who to send signal to
-s --signal=SIGNAL Which signal to send
--now Start or stop unit in addition to enabling or disabling it
-q --quiet Suppress output
--no-block Do not wait until operation finished
--no-wall Don't send wall message before halt/power-off/reboot
--no-reload Don't reload daemon after en-/dis-abling unit files
--no-legend Do not print a legend (column headers and hints)
--no-pager Do not pipe output into a pager
--no-ask-password
Do not ask for system passwords
--global Enable/disable unit files globally
--runtime Enable unit files only temporarily until next reboot
-f --force When enabling unit files, override existing symlinks
When shutting down, execute action immediately
--preset-mode= Apply only enable, only disable, or all presets
--root=PATH Enable unit files in the specified root directory
-n --lines=INTEGER Number of journal entries to show
-o --output=STRING Change journal output mode (short, short-iso,
short-precise, short-monotonic, verbose,
export, json, json-pretty, json-sse, cat)
--plain Print unit dependencies as a list instead of a tree
Unit Commands:
list-units [PATTERN...] List loaded units
list-sockets [PATTERN...] List loaded sockets ordered by address
list-timers [PATTERN...] List loaded timers ordered by next elapse
start NAME... Start (activate) one or more units
stop NAME... Stop (deactivate) one or more units
reload NAME... Reload one or more units
restart NAME... Start or restart one or more units
try-restart NAME... Restart one or more units if active
reload-or-restart NAME... Reload one or more units if possible,
otherwise start or restart
reload-or-try-restart NAME... Reload one or more units if possible,
otherwise restart if active
isolate NAME Start one unit and stop all others
kill NAME... Send signal to processes of a unit
is-active PATTERN... Check whether units are active
is-failed PATTERN... Check whether units are failed
status [PATTERN...|PID...] Show runtime status of one or more units
show [PATTERN...|JOB...] Show properties of one or more
units/jobs or the manager
cat PATTERN... Show files and drop-ins of one or more units
set-property NAME ASSIGNMENT... Sets one or more properties of a unit
help PATTERN...|PID... Show manual for one or more units
reset-failed [PATTERN...] Reset failed state for all, one, or more
units
list-dependencies [NAME] Recursively show units which are required
or wanted by this unit or by which this
unit is required or wanted
Unit File Commands:
list-unit-files [PATTERN...] List installed unit files
enable NAME... Enable one or more unit files
disable NAME... Disable one or more unit files
reenable NAME... Reenable one or more unit files
preset NAME... Enable/disable one or more unit files
based on preset configuration
preset-all Enable/disable all unit files based on
preset configuration
is-enabled NAME... Check whether unit files are enabled
mask NAME... Mask one or more units
unmask NAME... Unmask one or more units
link PATH... Link one or more units files into
the search path
add-wants TARGET NAME... Add 'Wants' dependency for the target
on specified one or more units
add-requires TARGET NAME... Add 'Requires' dependency for the target
on specified one or more units
edit NAME... Edit one or more unit files
get-default Get the name of the default target
set-default NAME Set the default target
Machine Commands:
list-machines [PATTERN...] List local containers and host
Job Commands:
list-jobs [PATTERN...] List jobs
cancel [JOB...] Cancel all, one, or more jobs
Snapshot Commands:
snapshot [NAME] Create a snapshot
delete NAME... Remove one or more snapshots
Environment Commands:
show-environment Dump environment
set-environment NAME=VALUE... Set one or more environment variables
unset-environment NAME... Unset one or more environment variables
import-environment [NAME...] Import all or some environment variables
Manager Lifecycle Commands:
daemon-reload Reload systemd manager configuration
daemon-reexec Reexecute systemd manager
System Commands:
is-system-running Check whether system is fully running
default Enter system default mode
rescue Enter system rescue mode
emergency Enter system emergency mode
halt Shut down and halt the system
poweroff Shut down and power-off the system
reboot [ARG] Shut down and reboot the system
kexec Shut down and reboot the system with kexec
exit Request user instance exit
switch-root ROOT [INIT] Change to a different root file system
suspend Suspend the system
hibernate Hibernate the system
hybrid-sleep Hibernate and suspend the system
以上,感谢。
2022年11月27日