systemctl
systemctl
是systemd系统和服务管理器的主要工具,用于控制systemd服务、检查状态、管理系统状态等。
基本语法
systemctl [选项] 命令 [单元名称...]
主要命令
服务控制命令
start
- 启动服务stop
- 停止服务restart
- 重启服务reload
- 重新加载服务配置reload-or-restart
- 重新加载或重启服务enable
- 设置服务开机自启disable
- 禁用服务开机自启mask
- 屏蔽服务(禁止启动)unmask
- 取消屏蔽服务
状态查询命令
status
- 显示服务状态is-active
- 检查服务是否活动is-enabled
- 检查服务是否开机自启is-failed
- 检查服务是否失败
列表命令
list-units
- 列出所有单元list-unit-files
- 列出所有单元文件list-dependencies
- 显示单元依赖关系list-jobs
- 显示活动作业
系统控制命令
reboot
- 重启系统poweroff
- 关闭系统suspend
- 挂起系统hibernate
- 休眠系统rescue
- 进入救援模式emergency
- 进入紧急模式
常用选项
-t, --type=TYPE
- 指定单元类型-a, --all
- 显示所有单元--failed
- 只显示失败的单元--no-pager
- 不使用分页器--no-legend
- 不显示标题行-l, --full
- 显示完整信息--now
- 立即执行操作-f, --force
- 强制执行-q, --quiet
- 静默模式
使用示例
基本服务管理
# 启动服务
systemctl start nginx
# 停止服务
systemctl stop nginx
# 重启服务
systemctl restart nginx
# 重新加载配置
systemctl reload nginx
# 查看服务状态
systemctl status nginx
# 检查服务是否运行
systemctl is-active nginx
# 检查服务是否开机自启
systemctl is-enabled nginx
开机自启管理
# 设置服务开机自启
systemctl enable nginx
# 禁用服务开机自启
systemctl disable nginx
# 启用服务并立即启动
systemctl enable --now nginx
# 禁用服务并立即停止
systemctl disable --now nginx
# 屏蔽服务(完全禁止启动)
systemctl mask nginx
# 取消屏蔽
systemctl unmask nginx
查看系统状态
# 显示系统状态概览
systemctl status
# 列出所有运行的服务
systemctl list-units --type=service
# 列出所有服务(包括未运行的)
systemctl list-units --type=service --all
# 列出失败的服务
systemctl --failed
# 列出所有开机自启的服务
systemctl list-unit-files --type=service --state=enabled
依赖关系管理
# 查看服务依赖
systemctl list-dependencies nginx
# 查看反向依赖
systemctl list-dependencies --reverse nginx
# 显示服务依赖树
systemctl list-dependencies --all nginx
# 查看服务被什么需要
systemctl list-dependencies --reverse --all nginx
实际应用场景
Web服务管理
# 管理Apache服务
systemctl start httpd
systemctl enable httpd
systemctl status httpd
# 管理Nginx服务
systemctl restart nginx
systemctl reload nginx
systemctl status nginx
# 管理PHP-FPM
systemctl start php-fpm
systemctl enable php-fpm
数据库服务管理
# 管理MySQL/MariaDB
systemctl start mysqld
systemctl enable mysqld
systemctl status mysqld
# 管理PostgreSQL
systemctl start postgresql
systemctl enable postgresql
systemctl status postgresql
# 管理Redis
systemctl start redis
systemctl enable redis
系统服务管理
# 管理SSH服务
systemctl status sshd
systemctl restart sshd
# 管理防火墙
systemctl start firewalld
systemctl enable firewalld
systemctl status firewalld
# 管理网络服务
systemctl restart network
systemctl status NetworkManager
监控和故障排查
# 查看所有失败的服务
systemctl --failed
# 重置失败状态
systemctl reset-failed service_name
# 查看服务的日志
journalctl -u service_name
# 实时查看服务日志
journalctl -u service_name -f
# 查看服务的详细信息
systemctl show service_name
高级用法
编辑服务配置
# 编辑服务文件
systemctl edit nginx
# 编辑完整的服务文件
systemctl edit --full nginx
# 重新加载systemd配置
systemctl daemon-reload
# 查看服务文件内容
systemctl cat nginx
系统状态管理
# 检查系统启动时间
systemctl status | head -3
# 查看系统运行级别
systemctl get-default
# 设置默认运行级别
systemctl set-default multi-user.target
# 切换到图形界面
systemctl isolate graphical.target
# 切换到命令行界面
systemctl isolate multi-user.target
作业管理
# 查看当前作业
systemctl list-jobs
# 取消作业
systemctl cancel job_id
# 查看作业详情
systemctl show-job job_id
服务文件管理
服务文件位置
# 系统服务文件目录
/usr/lib/systemd/system/
# 本地服务文件目录
/etc/systemd/system/
# 运行时服务文件目录
/run/systemd/system/
# 查看服务文件路径
systemctl show -p FragmentPath service_name
自定义服务
# 创建自定义服务文件
sudo vim /etc/systemd/system/myapp.service
# 基本服务文件示例
[Unit]
Description=My Application
After=network.target
[Service]
Type=simple
User=myuser
ExecStart=/path/to/myapp
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
# 重新加载配置
systemctl daemon-reload
# 启用新服务
systemctl enable myapp.service
systemctl start myapp.service
定时器(Timer)管理
# 列出所有定时器
systemctl list-timers
# 查看定时器状态
systemctl status timer_name
# 启动/停止定时器
systemctl start timer_name
systemctl stop timer_name
# 查看定时器详情
systemctl show timer_name
系统电源管理
# 重启系统
systemctl reboot
# 关机
systemctl poweroff
# 挂起系统
systemctl suspend
# 休眠系统
systemctl hibernate
# 混合睡眠
systemctl hybrid-sleep
# 进入救援模式
systemctl rescue
# 进入紧急模式
systemctl emergency
环境和配置
环境变量管理
# 查看服务环境变量
systemctl show-environment
# 设置环境变量
systemctl set-environment VAR=value
# 取消环境变量
systemctl unset-environment VAR
# 导入环境变量文件
systemctl import-environment
配置重载
# 重新加载systemd配置
systemctl daemon-reload
# 重新执行生成器
systemctl daemon-reexec
故障排查
常见问题诊断
# 查看服务失败原因
systemctl status service_name
journalctl -u service_name
# 查看服务的完整输出
systemctl status service_name -l
# 检查服务文件语法
systemd-analyze verify service_name.service
# 查看服务启动顺序
systemd-analyze critical-chain service_name
性能分析
# 分析系统启动时间
systemd-analyze
# 分析服务启动时间
systemd-analyze blame
# 生成启动图表
systemd-analyze plot > boot.svg
# 分析关键路径
systemd-analyze critical-chain
批量操作
# 批量启动多个服务
systemctl start service1 service2 service3
# 批量停止服务
systemctl stop nginx apache2 mysql
# 使用通配符操作
systemctl stop *.mount
# 批量重启所有失败的服务
systemctl restart $(systemctl --failed --no-legend | awk '{print $1}')
注意事项
- 权限: 大多数systemctl操作需要root权限
- 服务名: 服务名可以省略.service后缀
- 单元类型: systemd管理多种单元类型(service, timer, mount等)
- 配置重载: 修改服务文件后需要daemon-reload
- 依赖关系: 停止服务可能影响依赖它的其他服务
相关命令
journalctl
- 查看systemd日志systemd-analyze
- 分析systemd性能systemd-run
- 运行临时服务loginctl
- 管理用户会话timedatectl
- 管理系统时间
systemctl是现代Linux系统服务管理的核心工具,掌握其用法对系统运维工作至关重要。