当前位置:首页>Linux>Linux30天入门计划|Day11:systemctl服务管理,控制服务启停与开机自启

Linux30天入门计划|Day11:systemctl服务管理,控制服务启停与开机自启

  • 2026-10-11 09:40:07
Linux30天入门计划|Day11:systemctl服务管理,控制服务启停与开机自启

前面我们学会了软件安装,绝大多数服务(Nginx、MySQL、Redis等)安装完成后,都需要统一管理启停、开机自启。

Linux7之后主流系统统一使用 systemctl 管控系统服务,传统service命令逐步淘汰。

今天Day11,吃透服务整套运维操作,项目部署、中间件运维必备核心知识点。

一、今日学习目标

✅ 掌握systemctl基础启停、重载、查看状态命令

✅ 配置服务开机自启、关闭开机自启

✅ 学会查看服务日志,定位服务启动失败问题

✅ 理解运行目标、防火墙服务基础管控

✅ 熟练搭建服务常规运维标准流程

二、systemctl核心基础命令

所有操作通用格式: systemctl 动作 服务名 

1、启停服务

bash  

# 启动服务

systemctl start nginx

# 停止服务

systemctl stop nginx

# 重启服务(修改配置后通用操作)

systemctl restart nginx

# 平滑重载(不中断业务,nginx修改配置首选)

systemctl reload nginx

2、查看服务运行状态

bash  

systemctl status nginx

重点关注:

active (running):正常运行

inactive (dead):已经停止

failed:启动失败,存在故障

3、开机自启管理

bash  

# 设置开机自动启动

systemctl enable nginx

# 关闭开机自启

systemctl disable nginx

# 查看服务是否开机自启

systemctl is-enabled nginx

三、查看服务运行日志(排查启动失败神器)

服务启动报错,绝大多数人不知道去哪里看日志。

bash  

# 实时查看服务日志

journalctl -u nginx -f

# 查看历史全部日志

journalctl -u nginx

# 查看当天日志

journalctl -u nginx --since "today"

四、系统运行目标(系统启动模式)

多用户模式(字符界面,服务器标准模式)

bash  

systemctl set-default multi-user.target

图形桌面模式

bash  

systemctl set-default graphical.target

紧急救援模式(系统无法启动时使用)

bash  

systemctl isolate rescue.target

五、防火墙firewalld基础管控(云服务器常用)

云服务器外网访问端口,防火墙放行规则高频操作

bash  

# 临时关闭防火墙

systemctl stop firewalld

# 禁止开机自启

systemctl disable firewalld

# 放行80端口永久生效

firewall-cmd --permanent --add-port=80/tcp

# 重载防火墙规则

firewall-cmd --reload

小贴士:Ubuntu默认使用ufw防火墙,配置方式略有区别,生产场景按需选择。

六、企业运维标准操作流程

1. 修改服务配置文件

2. 执行重载 restart/reload

3. systemctl status 验证运行状态

4. 访问业务测试连通性

5. 业务正常后设置开机自启

七、今日高频实战场景

场景1:网站服务nginx部署完整流程

bash  

apt install nginx -y

systemctl start nginx

systemctl enable nginx

systemctl status nginx

场景2:服务启动失败排查

bash  

# 查看详细报错日志

journalctl -u nginx

八、今日实操作业

bash  

# 安装nginx服务

apt install nginx -y

# 启动服务

systemctl start nginx

# 设置开机自启

systemctl enable nginx

# 查看运行状态

systemctl status nginx

# 平滑重载服务

systemctl reload nginx

# 查看服务日志

journalctl -u nginx -f

九、今日知识点总结

1. systemctl 是现代Linux统一服务管理工具,替代老旧service

2. 核心动作:start/stop/restart/reload/status

3. enable设置开机自启,disable取消开机自启

4. journalctl -u 查看服务日志,排查启动故障核心手段

5. 配置修改后优先使用reload平滑重载,避免业务中断

6. 服务器优先使用多用户字符模式,不开启图形桌面

一起学习,一起成长,加关注不迷路

最新文章

随机文章