1. systemctl命令简介
systemd is a software suite that provides an array of system components for Linux operating systems. Its main aim is to unify service configuration and behavior across Linux distributions; Its primary component is a “system and service manager”—an init system used to bootstrap user space and manage user processes. It also provides replacements for various daemons and utilities, including device management, login management, network connection management, and event logging.
systemd 是一个软件套件,它为 Linux 操作系统提供一系列系统组件。它的主要目标是统一 Linux 发行版中的服务配置和行为;它的主要组件是“系统和服务管理器”——一个用于引导用户空间和管理用户进程的 init 系统。它还提供各种守护程序和实用程序的替代品,包括设备管理、登录管理、网络连接管理和事件日志记录。
systemd’s core components include the following:
- systemd is a system and service manager for Linux operating systems.
- systemctl is a command to introspect and control the state of the systemd system and service manager. Not to be confused with sysctl.
- systemd-analyze may be used to determine system boot-up performance statistics and retrieve other state and tracing information from the system and service manager.
如wiki所说,systemctl是systemd的一个核心组件,作用是作为命令客户端控制systemd。
参考文档:
2. systemctl常用命令
2.1. 启停服务
以启动docker服务为例
1 | systemctl start docker |
以停止docker服务为例
1 | systemctl stop docker |
以重启docker服务为例
1 | systemctl restart docker |
2.2. 重新加载单元配置
1 | systemctl daemon-reload |
经常和启停服务配合使用。
停止服务 -> 修改单元配置 -> 重新加载单元配置 -> 启动服务
修改单元配置 -> 重新加载单元配置 -> 重启服务
2.3. 开机自启动
1 | # 开机自启动 |
2.4. 查看服务状态
以查看docker服务状态为例
1 | systemctl status docker |
查看 Drop-In
字段,就能找到配置文件位置。
从这个配置文件中,又可以查看到其他配置文件的位置。
查看docker服务状态,日志不截断
1 | systemctl status docker -l |
2.5. 查看服务参数配置
以查看docker服务的MountFlags配置为例
1 | sudo systemctl show --property=MountFlags docker.service |
2.6. 重新加载配置
以重新加载sshd服务配置为例
1 | systemctl reload sshd |
2.7. 查看所有活跃的单元
1 | systemctl list-units |
2.8. 查看所有活跃的服务
1 | systemctl list-units -t service |