1. 首页 > Linux教程 > 正文

Linux教程FG115-systemd系统服务管理基础命令(systemctl)

本文档风哥主要介绍systemd系统服务管理基础命令的使用,包括systemd的概念、systemd与SysVinit的区别、systemctl命令的概念、systemctl基础操作详解、systemctl高级使用技巧、systemd服务管理自动化、服务管理实战案例、服务故障排查实战案例、systemd服务管理故障排查与解决等内容,参考Red Hat Enterprise Linux 10官方文档,适合Linux运维人员在学习和测试中使用,如果要应用于生产环境则需要自行确认。

Part01-基础概念与理论知识

1.1 systemd的概念

systemd是Linux系统的系统和服务管理器,用于初始化系统、管理系统服务、管理进程和日志等。systemd采用并行启动方式,可以显著提高系统启动速度。systemd使用单元(unit)来管理不同类型的系统资源,包括服务(service)、挂载点(mount)、设备(device)、套接字(socket)等。更多视频教程www.fgedu.net.cn

systemd的主要特点:

  • 并行启动,提高系统启动速度
  • 按需启动服务
  • 自动处理服务依赖关系
  • 支持服务监控和自动重启
  • 统一的日志管理

1.2 systemd与SysVinit的区别

systemd与SysVinit的区别:

  • 启动方式:systemd并行启动,SysVinit串行启动
  • 服务管理:systemd使用systemctl命令,SysVinit使用service命令
  • 依赖处理:systemd自动处理依赖关系,SysVinit手动配置依赖
  • 日志管理:systemd使用journald统一管理日志,SysVinit使用syslog
  • 兼容性:systemd兼容SysVinit脚本

1.3 systemctl命令的概念

systemctl是systemd的主要命令行工具,用于控制systemd系统和服务管理器。systemctl命令可以启动、停止、重启、启用、禁用系统服务,查看服务状态,列出所有服务等。systemctl命令是Linux系统管理中最常用的命令之一。

风哥提示:systemd是Linux系统的系统和服务管理器,systemctl是systemd的主要命令行工具。建议熟练掌握systemctl命令的使用。

Part02-生产环境规划与建议

2.1 systemd服务管理在生产环境的规划

systemd服务管理在生产环境的规划要点:

# systemd服务管理生产环境规划
– 合理配置服务依赖关系
– 配置服务自动启动
– 配置服务自动重启
– 定期检查服务状态
– 监控服务运行状态

# systemd服务管理注意事项
– 了解服务依赖关系
– 配置正确的启动顺序
– 设置合理的超时时间
– 配置资源限制
– 记录服务日志

2.2 systemd服务管理最佳实践

systemd服务管理最佳实践:

  • 依赖管理:合理配置服务依赖关系
  • 自动启动:配置服务自动启动
  • 自动重启:配置服务自动重启
  • 定期检查:定期检查服务状态
  • 监控告警:配置监控告警

2.3 systemd服务管理安全配置建议

systemd服务管理安全配置建议:

  • 最小权限:使用最小权限运行服务
  • 资源限制:配置资源限制
  • 安全加固:启用安全加固选项
  • 日志审计:启用日志审计
  • 访问控制:配置访问控制
生产环境建议:合理配置服务依赖关系,配置服务自动启动和自动重启,定期检查服务状态。学习交流加群风哥微信: itpux-com

Part03-生产环境项目实施方案

3.1 systemctl基础操作详解

3.1.1 查看服务状态

# 查看所有服务状态
# systemctl list-units –type=service
UNIT LOAD ACTIVE SUB DESCRIPTION
atd.service loaded active running Deferred execution scheduler
auditd.service loaded active running Security Auditing Service
chronyd.service loaded active running NTP client/server
crond.service loaded active running Command Scheduler
dbus.service loaded active running D-Bus System Message Bus
firewalld.service loaded active running firewalld – dynamic firewall daemon
getty@tty1.service loaded active running Getty on tty1
httpd.service loaded active running The Apache HTTP Server
irqbalance.service loaded active running irqbalance daemon
kdump.service loaded active exited Crash recovery kernel arming
NetworkManager.service loaded active running Network Manager
nginx.service loaded active running nginx – high performance web server
rsyslog.service loaded active running System Logging Service
sshd.service loaded active running OpenSSH server daemon
systemd-journald.service loaded active running Journal Service
systemd-logind.service loaded active running Login Service
systemd-udevd.service loaded active running udev Kernel Device Manager

LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.

17 loaded units listed. Pass –all to see loaded but inactive units, too.
To show all installed unit files use ‘systemctl list-unit-files’.

# 查看特定服务状态
# systemctl status nginx
● nginx.service – nginx – high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2026-03-31 10:00:00 CST; 1h 30min ago
Docs: http://nginx.org/en/docs/
Main PID: 1234 (nginx)
Tasks: 2 (limit: 4915)
Memory: 2.4M
CGroup: /system.slice/nginx.service
├─1234 nginx: master process /usr/sbin/nginx
└─1235 nginx: worker process

Mar 31 10:00:00 localhost systemd[1]: Starting nginx – high performance web server…
Mar 31 10:00:00 localhost nginx[1234]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Mar 31 10:00:00 localhost systemd[1]: Started nginx – high performance web server.

3.1.2 启动、停止、重启服务

# 启动服务
# systemctl start nginx

# 验证服务已启动
# systemctl status nginx
● nginx.service – nginx – high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2026-03-31 10:00:00 CST; 5s ago

# 停止服务
# systemctl stop nginx

# 验证服务已停止
# systemctl status nginx
● nginx.service – nginx – high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Thu 2026-03-31 10:00:00 CST; 5s ago

# 重启服务
# systemctl restart nginx

# 验证服务已重启
# systemctl status nginx
● nginx.service – nginx – high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2026-03-31 10:00:00 CST; 5s ago

# 重新加载服务配置
# systemctl reload nginx

# 验证服务配置已重新加载
# systemctl status nginx
● nginx.service – nginx – high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2026-03-31 10:00:00 CST; 10min ago

3.1.3 启用、禁用服务

# 启用服务(开机自动启动)
# systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

# 验证服务已启用
# systemctl is-enabled nginx
enabled

# 禁用服务(开机不自动启动)
# systemctl disable nginx
Removed /etc/systemd/system/multi-user.target.wants/nginx.service.

# 验证服务已禁用
# systemctl is-enabled nginx
disabled

# 查看服务是否正在运行
# systemctl is-active nginx
active

# 查看服务是否启用
# systemctl is-enabled nginx
disabled

# 查看服务是否失败
# systemctl is-failed nginx
inactive

3.1.4 查看所有服务

# 查看所有已加载的服务
# systemctl list-units –type=service

# 查看所有已安装的服务单元文件
# systemctl list-unit-files –type=service
UNIT FILE STATE
atd.service enabled
auditd.service enabled
autovt@.service alias
chronyd.service enabled
crond.service enabled
dbus.service static
dbus-org.fedoraproject.FirewallD1.service alias
dbus-org.freedesktop.NetworkManager.service alias
dbus-org.freedesktop.nm-dispatcher.service alias
firewalld.service enabled
getty@.service enabled
httpd.service disabled
irqbalance.service enabled
kdump.service enabled
NetworkManager.service enabled
nginx.service disabled
rsyslog.service enabled
sshd.service enabled
systemd-journald.service static
systemd-logind.service static
systemd-udevd.service static

20 unit files listed.

3.2 systemctl高级使用技巧

3.2.1 查看服务依赖关系

# 查看服务依赖关系
# systemctl list-dependencies nginx
nginx.service
● ├─system.slice
● ├─network.target
● └─sysinit.target
● ├─dev-hugepages.mount
● ├─dev-mqueue.mount
● ├─dracut-shutdown.service
● ├─import-state.service
● ├─loadmodules.service
● ├─ldconfig.service
● ├─nis-domainname.service
● ├─sys-fs-fuse-connections.mount
● ├─systemd-ask-password-console.path
● ├─systemd-ask-password-wall.path
● ├─systemd-binfmt.service
● ├─systemd-firstboot.service
● ├─systemd-hwdb-update.service
● ├─systemd-journal-catalog-update.service
● ├─systemd-journal-flush.service
● ├─systemd-journald.service
● ├─systemd-machine-id-commit.service
● ├─systemd-modules-load.service
● ├─systemd-random-seed.service
● ├─systemd-sysctl.service
● ├─systemd-sysusers.service
● ├─systemd-tmpfiles-setup-dev.service
● ├─systemd-tmpfiles-setup.service
● ├─systemd-udev-trigger.service
● ├─systemd-udevd.service
● ├─systemd-update-done.service
● ├─systemd-update-utmp.service
● └─cryptsetup.target

# 查看反向依赖关系
# systemctl list-dependencies –reverse nginx
nginx.service

3.2.2 屏蔽和解屏蔽服务

# 屏蔽服务(防止服务被启动)
# systemctl mask nginx
Created symlink /etc/systemd/system/nginx.service → /dev/null.

# 验证服务已屏蔽
# systemctl status nginx
● nginx.service
Loaded: masked (Reason: Unit nginx.service is masked.)
Active: inactive (dead)

# 解屏蔽服务
# systemctl unmask nginx
Removed /etc/systemd/system/nginx.service.

# 验证服务已解屏蔽
# systemctl status nginx
● nginx.service – nginx – high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: inactive (dead)

3.3 systemd服务管理自动化

3.3.1 自动检查服务状态

# 创建自动检查脚本
# cat > /usr/local/bin/check-services.sh << 'EOF' #!/bin/bash # 自动检查服务状态脚本 SERVICES="nginx httpd sshd crond firewalld" LOG_FILE="/var/log/service-check.log" DATE=$(date '+%Y-%m-%d %H:%M:%S') # 记录检查开始时间 echo "[$DATE] Starting service status check..." >> $LOG_FILE

# 检查每个服务状态
for service in $SERVICES; do
status=$(systemctl is-active $service)
if [ “$status” = “active” ]; then
echo “[$DATE] Service $service is running.” >> $LOG_FILE
else
echo “[$DATE] Service $service is NOT running!” >> $LOG_FILE
# 尝试启动服务
systemctl start $service
if [ $? -eq 0 ]; then
echo “[$DATE] Service $service started successfully.” >> $LOG_FILE
else
echo “[$DATE] Failed to start service $service!” >> $LOG_FILE
# 发送告警邮件
echo “Service $service is down on $(hostname)” | mail -s “Service Alert” admin@example.com
fi
fi
done

echo “[$DATE] Service status check completed.” >> $LOG_FILE
EOF

# 设置脚本执行权限
# chmod +x /usr/local/bin/check-services.sh

# 测试脚本
# /usr/local/bin/check-services.sh

# 查看检查日志
# cat /var/log/service-check.log
[2026-03-31 10:00:00] Starting service status check…
[2026-03-31 10:00:00] Service nginx is running.
[2026-03-31 10:00:00] Service httpd is running.
[2026-03-31 10:00:00] Service sshd is running.
[2026-03-31 10:00:00] Service crond is running.
[2026-03-31 10:00:00] Service firewalld is running.
[2026-03-31 10:00:00] Service status check completed.

# 配置cron任务
# cat > /etc/cron.d/check-services << 'EOF' */5 * * * * root /usr/local/bin/check-services.sh EOF

风哥提示:systemctl命令支持多种高级选项,如查看服务依赖关系、屏蔽和解屏蔽服务、自动化检查等。学习交流加群风哥QQ113257174

Part04-生产案例与实战讲解

4.1 服务管理实战案例

4.1.1 完整服务管理流程

# 1. 查看nginx服务状态
# systemctl status nginx
● nginx.service – nginx – high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: inactive (dead)

# 2. 启动nginx服务
# systemctl start nginx

# 3. 验证服务已启动
# systemctl status nginx
● nginx.service – nginx – high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2026-03-31 10:00:00 CST; 5s ago

# 4. 启用nginx服务(开机自动启动)
# systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

# 5. 验证服务已启用
# systemctl is-enabled nginx
enabled

# 6. 查看服务日志
# journalctl -u nginx
— Logs begin at Thu 2026-03-31 09:00:00 CST. —
Mar 31 10:00:00 localhost systemd[1]: Starting nginx – high performance web server…
Mar 31 10:00:00 localhost nginx[1234]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Mar 31 10:00:00 localhost systemd[1]: Started nginx – high performance web server.

# 7. 重新加载服务配置
# systemctl reload nginx

# 8. 验证服务配置已重新加载
# systemctl status nginx
● nginx.service – nginx – high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2026-03-31 10:00:00 CST; 10min ago

# 9. 停止nginx服务
# systemctl stop nginx

# 10. 验证服务已停止
# systemctl status nginx
● nginx.service – nginx – high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Thu 2026-03-31 10:00:00 CST; 5s ago

# 11. 禁用nginx服务(开机不自动启动)
# systemctl disable nginx
Removed /etc/systemd/system/multi-user.target.wants/nginx.service.

# 12. 验证服务已禁用
# systemctl is-enabled nginx
disabled

4.2 服务故障排查实战案例

4.2.1 服务启动失败排查

# 1. 尝试启动nginx服务
# systemctl start nginx
Job for nginx.service failed because the control process exited with error code.
See “systemctl status nginx.service” and “journalctl -xe” for details.

# 2. 查看服务状态
# systemctl status nginx
● nginx.service – nginx – high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Thu 2026-03-31 10:00:00 CST; 5s ago
Process: 1234 ExecStart=/usr/sbin/nginx (code=exited, status=1/FAILURE)

Mar 31 10:00:00 localhost systemd[1]: Starting nginx – high performance web server…
Mar 31 10:00:00 localhost nginx[1234]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Mar 31 10:00:00 localhost nginx[1234]: nginx: configuration file /etc/nginx/nginx.conf test failed
Mar 31 10:00:00 localhost systemd[1]: nginx.service: Control process exited, code=exited status=1
Mar 31 10:00:00 localhost systemd[1]: nginx.service: Failed with result ‘exit-code’.
Mar 31 10:00:00 localhost systemd[1]: Failed to start nginx – high performance web server.

# 3. 查看详细日志
# journalctl -u nginx -n 20
— Logs begin at Thu 2026-03-31 09:00:00 CST. —
Mar 31 10:00:00 localhost systemd[1]: Starting nginx – high performance web server…
Mar 31 10:00:00 localhost nginx[1234]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Mar 31 10:00:00 localhost nginx[1234]: nginx: configuration file /etc/nginx/nginx.conf test failed
Mar 31 10:00:00 localhost systemd[1]: nginx.service: Control process exited, code=exited status=1
Mar 31 10:00:00 localhost systemd[1]: nginx.service: Failed with result ‘exit-code’.
Mar 31 10:00:00 localhost systemd[1]: Failed to start nginx – high performance web server.

# 4. 检查端口占用情况
# ss -tlnp | grep :80
LISTEN 0 128 0.0.0.0:80 0.0.0.0:* users:((“httpd”,pid=5678,fd=4))

# 5. 停止httpd服务
# systemctl stop httpd

# 6. 重新启动nginx服务
# systemctl start nginx

# 7. 验证服务已启动
# systemctl status nginx
● nginx.service – nginx – high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2026-03-31 10:00:00 CST; 5s ago

# 8. 预防措施
# – 检查端口占用情况
# – 修改服务监听端口
# – 配置服务依赖关系

4.3 systemd服务管理故障排查与解决

4.3.1 服务无法启动

# 问题现象:服务无法启动
# 分析步骤:

# 1. 查看服务状态
# systemctl status nginx
● nginx.service – nginx – high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Thu 2026-03-31 10:00:00 CST; 5s ago
Process: 1234 ExecStart=/usr/sbin/nginx (code=exited, status=1/FAILURE)

# 2. 查看详细日志
# journalctl -u nginx -n 50
— Logs begin at Thu 2026-03-31 09:00:00 CST. —
Mar 31 10:00:00 localhost systemd[1]: Starting nginx – high performance web server…
Mar 31 10:00:00 localhost nginx[1234]: nginx: [emerg] unknown directive “serverr” in /etc/nginx/nginx.conf:10
Mar 31 10:00:00 localhost nginx[1234]: nginx: configuration file /etc/nginx/nginx.conf test failed
Mar 31 10:00:00 localhost systemd[1]: nginx.service: Control process exited, code=exited status=1
Mar 31 10:00:00 localhost systemd[1]: nginx.service: Failed with result ‘exit-code’.
Mar 31 10:00:00 localhost systemd[1]: Failed to start nginx – high performance web server.

# 3. 检查配置文件
# nginx -t
nginx: [emerg] unknown directive “serverr” in /etc/nginx/nginx.conf:10
nginx: configuration file /etc/nginx/nginx.conf test failed

# 4. 修复配置文件
# vi /etc/nginx/nginx.conf
# 修改第10行,将”serverr”改为”server”

# 5. 验证配置文件
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

# 6. 重新启动服务
# systemctl start nginx

# 7. 验证服务已启动
# systemctl status nginx
● nginx.service – nginx – high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2026-03-31 10:00:00 CST; 5s ago

# 8. 预防措施
# – 定期检查配置文件
# – 使用nginx -t验证配置
# – 备份配置文件

生产环境建议:systemd服务管理故障排查需要耐心和细致。服务无法启动需要查看详细日志和检查配置文件。更多学习教程公众号风哥教程itpux_com

Part05-风哥经验总结与分享

5.1 systemd服务管理经验总结

systemd服务管理经验总结:

  • 依赖管理:合理配置服务依赖关系
  • 自动启动:配置服务自动启动
  • 自动重启:配置服务自动重启
  • 定期检查:定期检查服务状态
  • 监控告警:配置监控告警

5.2 systemd服务管理检查清单

systemd服务管理检查清单:

  • 配置前:了解服务依赖关系
  • 配置时:配置正确的启动顺序
  • 配置后:验证服务状态
  • 使用时:定期检查服务状态
  • 维护时:定期更新服务配置
  • 故障排查:查看详细日志、检查配置文件

5.3 systemd服务管理相关工具推荐

systemd服务管理相关工具推荐:

  • systemctl:systemd控制命令
  • journalctl:systemd日志查看命令
  • systemd-analyze:systemd启动分析工具
  • systemd-cgls:控制组列表工具
  • systemd-cgtop:控制组监控工具
风哥提示:systemd是Linux系统的系统和服务管理器,systemctl是systemd的主要命令行工具。建议熟练掌握systemctl命令的使用。

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息