场景引入:
凌晨3点,服务器突然宕机,报警短信炸醒了一屋子运维,你连滚带爬打开电脑,SSH连上去发现关键服务挂了——这时候要是记不住服务启动命令,怕是只能对着屏幕唱《凉凉》了,别慌!这份实战整理的Linux服务管理命令大全,专治各种服务启动不服。
绝大多数现代Linux发行版(CentOS 7+/Ubuntu 16+)都采用systemd管理服务,记住这些命令能解决90%的问题:
# 启动服务(例:nginx) sudo systemctl start nginx # 停止服务 sudo systemctl stop nginx # 重启服务(配置生效常用) sudo systemctl restart nginx
# 查看服务状态(绿色=正常,红色=异常) sudo systemctl status nginx -l # -l显示完整日志 # 开机自启(重要服务必设) sudo systemctl enable nginx # 禁止开机启动 sudo systemctl disable nginx # 重新加载服务配置(不中断服务) sudo systemctl reload nginx # 适合nginx/php-fpm等
# 查看服务是否开机自启 systemctl is-enabled nginx # 屏蔽服务(防止被意外启动) sudo systemctl mask nginx # 查看服务依赖关系 systemctl list-dependencies nginx # 杀死服务所有进程(强制) sudo systemctl kill nginx
某些旧系统(如CentOS 6)仍在使用,建议收藏备用:
# 启动服务 service httpd start # CentOS风格 /etc/init.d/httpd start # 通用写法 # 查看状态 service httpd status # 设置开机启动 chkconfig httpd on # CentOS 6 update-rc.d apache2 defaults # Debian系
# 查看详细日志(关键!) journalctl -u nginx -xe --no-pager # 测试配置文件(适用nginx/mysql等) sudo nginx -t # nginx配置测试 sudo mysqld --verbose --help # mysql检查
自己写的脚本如何做成服务?创建.service文件示例:
# /etc/systemd/system/my_service.service [Unit] Description=我的自定义服务 [Service] ExecStart=/usr/local/bin/my_script.sh Restart=always # 崩溃自动重启 [Install] WantedBy=multi-user.target
然后运行:
sudo systemctl daemon-reload sudo systemctl start my_service
遇到这些服务别懵逼:
服务名 | 实际作用 | 常用操作 |
---|---|---|
sshd | SSH远程连接 | systemctl restart sshd |
crond | 定时任务 | systemctl status crond |
firewalld | 防火墙 | systemctl stop firewalld |
docker | 容器服务 | systemctl enable docker |
postfix | 邮件服务 | journalctl -u postfix |
:
systemctl
,老系统记住service
journalctl
是你的好朋友) restart
再enable
能用手敲命令解决的,千万别急着重启服务器! (笑)
注:本文命令测试环境为CentOS 8/Ubuntu 22.04,最后更新参考2025年7月文档。
本文由 伟棠华 于2025-07-30发表在【云服务器提供商】,文中图片由(伟棠华)上传,本平台仅提供信息存储服务;作者观点、意见不代表本站立场,如有侵权,请联系我们删除;若有图片侵权,请您准备原始证明材料和公证书后联系我方删除!
本文链接:https://vps.7tqx.com/wenda/480632.html
发表评论