systemctl 命令详解

systemctl 是 Linux 系统服务管理器,它将 servicechkconfig 命令的功能组合在一起,是 Systemd 系统的核心工具。Systemd 是大多数现代 Linux 发行版采用的初始化系统,用于管理系统服务、挂载点、设备等。

一、基本语法

1
systemctl [OPTIONS...] COMMAND [UNIT...]

二、服务管理命令

1. 启动和停止服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 启动服务
systemctl start service-name.service

# 停止服务
systemctl stop service-name.service

# 重启服务
systemctl restart service-name.service

# 重新加载服务配置(不重启服务)
systemctl reload service-name.service

# 启动服务并立即重新加载配置
systemctl reload-or-restart service-name.service

2. 查看服务状态

1
2
3
4
5
6
7
8
9
10
11
# 查看服务当前状态
systemctl status service-name.service

# 查看所有已启动的服务
systemctl list-units --type=service

# 查看所有服务(包括未启动的)
systemctl list-units --type=service --all

# 查看服务的详细信息
systemctl show service-name.service

3. 开机自启管理

1
2
3
4
5
6
7
8
9
10
11
# 设置服务开机自启
systemctl enable service-name.service

# 禁止服务开机自启
systemctl disable service-name.service

# 查看服务是否开机自启
systemctl is-enabled service-name.service

# 查看所有开机自启的服务
systemctl list-unit-files --type=service | grep enabled

4. 服务状态管理

1
2
3
4
5
6
7
8
9
10
11
# 查看服务是否运行中
systemctl is-active service-name.service

# 查看服务是否存在
systemctl list-unit-files --type=service | grep service-name

# 检查服务的依赖关系
systemctl list-dependencies service-name.service

# 检查哪些服务依赖于当前服务
systemctl list-dependencies --reverse service-name.service

三、系统状态管理

1. 系统关机和重启

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 关机
systemctl poweroff

# 重启
systemctl reboot

# 挂起
systemctl suspend

# 休眠
systemctl hibernate

# 混合休眠(同时休眠和挂起)
systemctl hybrid-sleep

2. 系统运行级别

Systemd 使用目标(target)来替代传统的运行级别:

1
2
3
4
5
6
7
8
9
# 查看当前运行级别
systemctl get-default

# 设置默认运行级别
systemctl set-default multi-user.target # 对应传统的 runlevel 3
systemctl set-default graphical.target # 对应传统的 runlevel 5

# 切换运行级别
systemctl isolate multi-user.target

四、服务文件管理

1. 服务文件位置

Systemd 服务文件通常位于以下目录:

  • /etc/systemd/system/:系统管理员创建的服务文件
  • /usr/lib/systemd/system/:软件包安装的服务文件

2. 创建自定义服务

创建一个简单的服务文件 /etc/systemd/system/my-service.service

1
2
3
4
5
6
7
8
9
10
11
12
[Unit]
Description=My Custom Service
After=network.target

[Service]
Type=simple
ExecStart=/path/to/your/script.sh
Restart=always
User=username

[Install]
WantedBy=multi-user.target

3. 重新加载服务配置

1
2
3
4
5
# 重新加载 systemd 配置
systemctl daemon-reload

# 重新加载后重启服务
systemctl restart my-service.service

五、高级功能

1. 定时器(Timers)

用于替代 cron 作业:

1
2
3
4
5
# 查看所有定时器
systemctl list-timers

# 查看定时器状态
systemctl status timer-name.timer

2. 日志管理

1
2
3
4
5
6
7
8
# 查看服务日志
journalctl -u service-name.service

# 实时查看服务日志
journalctl -u service-name.service -f

# 查看引导日志
journalctl -b

3. 环境变量

为服务设置环境变量:

  1. 创建环境变量文件:/etc/systemd/system/service-name.service.d/environment.conf
  2. 添加以下内容:
    1
    2
    [Service]
    Environment=VAR1=value1 VAR2=value2
  3. 重新加载配置并重启服务

六、常见问题及解决方案

1. 服务启动失败

1
2
3
4
5
6
# 查看详细的错误信息
systemctl status service-name.service
journalctl -u service-name.service

# 检查服务文件语法
systemd-analyze verify service-name.service

2. 服务依赖问题

1
2
3
4
5
# 查看服务依赖关系
systemctl list-dependencies service-name.service

# 检查依赖服务的状态
systemctl status $(systemctl list-dependencies service-name.service | grep -v "●")

3. 开机自启不生效

1
2
3
4
5
# 检查服务是否启用
systemctl is-enabled service-name.service

# 检查服务文件中的 [Install] 部分是否正确
# 确保 WantedBy 指向正确的 target

七、实例演示

示例 1:管理 NFS 服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 启动 NFS 服务
systemctl start nfs-server.service

# 设置开机自启
systemctl enable nfs-server.service

# 查看服务状态
systemctl status nfs-server.service

# 停止服务
systemctl stop nfs-server.service

# 禁止开机自启
systemctl disable nfs-server.service

示例 2:创建自定义服务

创建一个简单的 web 服务器服务:

  1. 创建服务文件 /etc/systemd/system/simple-web.service

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [Unit]
    Description=Simple Web Server
    After=network.target

    [Service]
    Type=simple
    ExecStart=/usr/bin/python3 -m http.server 8080
    WorkingDirectory=/var/www/html
    Restart=on-failure

    [Install]
    WantedBy=multi-user.target
  2. 启用并启动服务:

    1
    2
    3
    systemctl daemon-reload
    systemctl enable simple-web.service
    systemctl start simple-web.service
  3. 查看服务状态:

    1
    systemctl status simple-web.service

八、systemctl 与传统命令对比

任务 传统命令 systemctl 命令
启动服务 service service start systemctl start service.service
停止服务 service service stop systemctl stop service.service
重启服务 service service restart systemctl restart service.service
查看服务状态 service service status systemctl status service.service
设置开机自启 chkconfig service on systemctl enable service.service
禁止开机自启 chkconfig service off systemctl disable service.service
查看所有服务 chkconfig –list systemctl list-unit-files –type=service

九、总结

systemctl 是现代 Linux 系统中管理服务的核心工具,它提供了丰富的功能来控制服务的启动、停止、重启以及开机自启等。通过本文的介绍,你应该已经掌握了 systemctl 的基本用法和一些高级功能,可以更有效地管理 Linux 系统服务。

记住,systemctl 是 Systemd 的一部分,它不仅仅用于管理服务,还可以管理系统的其他方面,如挂载点、设备、定时器等。深入了解 systemctl 的使用,将有助于你更好地管理和维护 Linux 系统。