目录
一、systemctl命令介绍
systemctl命令是Linux操作系统中用于管理系统服务的命令行工具,尤其在RHEL 7及更高版本上,它替代了之前的service和chkconfig命令,将两者的功能整合在一起。systemctl的主要功能包括查询或发送相应的控制命令给systemd、管理unit以及启动或禁止相应的服务等。
只有在CentOS 7及以上版本中才能使用systemctl命令来管理系统和服务。在早期的CentOS版本中,如CentOS 6及以下版本,使用的是SysV init系统,管理系统和服务的命令是service和chkconfig。
二、命令语法
1、基本语法
systemctl命令的基本语法为:
systemctl [OPTIONS...] COMMAND [UNIT]
其中OPTIONS是可选参数,用于指定systemctl的行为;COMMAND是必需参数,用于执行特定的systemd命令,例如启动、停止或重新启动服务;UNIT是可选参数,用于指定要操作的服务或单元。
2、常用选项
systemctl命令的一些常用选项包括:
`-H,--host`:指定要在远程主机上运行的systemd实例的主机名或IP地址。
`-a,--all`: 显示所有已加载的单位,而不仅仅是当前活动的单位。
`-t,--type`:根据指定的单元类型筛选单元列表,支持的单元类型包括service、socket、mount等。
`-u,--user`:执行与当前用户相关联的用户级服务。
3、常用操作
systemctl命令的一些常用操作包括:
(1)停止服务:使用`systemctl stop`命令,后跟服务名称,如`systemctl stop nginx.service`,这将停止名为nginx.service的服务。
(2)查看服务状态:使用`systemctl status`命令,后跟服务名称,如`systemctl status nginx.service`,这将显示服务的详细状态信息,包括是否正在运行、最近一次启动时间、进程ID等。
(3)重启服务:使用`systemctl restart`命令,后跟服务名称,以重启服务。
三、获取帮助
systemctl命令是一个强大且灵活的工具,可以帮助Linux系统管理员有效地管理系统服务。如需了解更多关于systemctl命令的详细信息和用法,建议查阅相关的Linux系统文档或手册。如下是两种便捷获取帮助的渠道:
1、通过help获取
systemctl --help
实验如下:
[root@ecs-52a1 vidisit]#
[root@ecs-52a1 vidisit]# systemctl --help
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
--user Connect to user service 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 properties/all units currently in memory,
including dead/empty ones. To list all units installed on
the system, use the 'list-unit-files' command instead.
--failed Same as --state=failed
-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
--value When showing properties, only print the value
-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
--dry-run Only print what would be done
-q --quiet Suppress output
--wait For (re)start, wait until service stopped again
--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/mask unit files globally
--runtime Enable/disable/mask unit files 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/disable/mask 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-precise,
short-iso, short-iso-precise, short-full,
short-monotonic, short-unix,
verbose, export, json, json-pretty, json-sse, cat)
--firmware-setup Tell the firmware to show the setup men