linux系统命令systemctl详解,管理系统服务的工具-控制systemd、管理unit、启动或禁止服务

本文介绍了Linux系统中systemctl命令,该命令用于管理系统服务,在CentOS 7及以上版本可用。文中阐述了其基本语法、常用选项和操作,还介绍了获取帮助的方式,给出查看系统状态、管理单元等应用举例,以及服务管理、定时器管理等应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

一、systemctl命令介绍

二、命令语法

1、基本语法

2、常用选项

3、常用操作

三、获取帮助

1、通过help获取

2、通过man获取

四、应用举例

1. 查看系统状态和日志:

2. 管理系统单元:

3. 设置单元的启动频率限制和重启策略:

4. 依赖管理:

5. 使用通配符进行批量操作:

五、systemctl的应用场景

 1. 服务管理

 2. 定时器管理

 3. 查看系统状态

 4. 单元依赖管理

 5. 管理用户服务

 6. 管理套接字和挂载点

 7. 隔离目标

 8. 临时覆盖单元设置


一、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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值