Linux systemctl指令详解

262 阅读2分钟

systemctl是用于控制和管理systemd系统和服务管理器的命令行工具。它可以启动、停止、重启服务,查看服务状态等操作。以下是一些常见的systemctl命令及其用法:

1. 查看服务状态

systemctl status <service_name>

查看指定服务的当前状态。 例如,查看 nginx 服务的状态:

systemctl status nginx

2. 启动服务

systemctl start <service_name>

启动指定的服务。 例如,启动 nginx 服务:

systemctl start nginx

3. 停止服务

systemctl stop <service_name>

停止指定的服务。 例如,停止 nginx 服务:

systemctl stop nginx

4. 重启服务

systemctl restart <service_name>

重启指定的服务。 例如,重启 nginx 服务:

systemctl restart nginx

5. 重新加载服务配置

systemctl reload <service_name>

重新加载服务配置文件,而不停止服务。 例如,重新加载 nginx 配置:

systemctl reload nginx

6. 启用服务(开机启动)

systemctl enable <service_name>

设置服务在系统启动时自动启动。 例如,设置 nginx 服务开机自启动:

systemctl enable nginx

7. 禁用服务(关闭开机启动)

systemctl disable <service_name>

禁用服务在系统启动时自动启动。 例如,禁用 nginx 服务的开机自启动:

systemctl disable nginx

8. 查看所有服务的状态

systemctl list-units --type=service

列出当前所有服务的状态。

9. 查看系统状态

systemctl status

查看系统的总体状态。

10. 查看服务的日志

journalctl -u <service_name>

查看指定服务的日志。 例如,查看 nginx 的日志:

journalctl -u nginx

11. 列出所有启用的服务

systemctl list-unit-files --state=enabled

列出所有设置为启用(开机启动)的服务。

12. 检查服务是否正在运行

systemctl is-active <service_name>

检查服务是否处于激活状态(即运行中)。 例如,检查 nginx 是否正在运行:

systemctl is-active nginx

13. 查看所有已加载的单元

systemctl list-units

列出所有已加载的单元(服务、设备、挂载点等)。

14. 停止所有正在运行的服务

systemctl stop *

停止所有正在运行的服务。

这些是一些常见的 systemctl 使用方法,适用于大多数基于 systemd 的 Linux 发行版(如 CentOS 7+、Ubuntu 16.04+、Debian 8+ 等)。