systemctl 命令详解
systemctl 是 Linux 系统服务管理器,它将 service 和 chkconfig 命令的功能组合在一起,是 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 systemctl set-default graphical.target
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
| 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. 环境变量
为服务设置环境变量:
- 创建环境变量文件:
/etc/systemd/system/service-name.service.d/environment.conf
- 添加以下内容:
1 2
| [Service] Environment=VAR1=value1 VAR2=value2
|
- 重新加载配置并重启服务
六、常见问题及解决方案
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
|
七、实例演示
示例 1:管理 NFS 服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| 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 服务器服务:
创建服务文件 /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
|
启用并启动服务:
1 2 3
| systemctl daemon-reload systemctl enable simple-web.service systemctl start simple-web.service
|
查看服务状态:
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 系统。