使用systemctl status查看服务状态,如systemctl status ssh;2. 用sudo systemctl enable启用开机自启,如nginx;3. 用sudo systemctl disable禁用开机自启,如apache;4. 用start、stop、restart立即控制服务运行。

在linux系统中,服务的自动启动与禁用主要依赖于系统的初始化系统,目前大多数现代发行版使用systemd。掌握服务的启停和开机自启管理,是系统运维的基本技能。
查看服务状态
要管理服务,首先需要确认其当前状态。使用以下命令查看某个服务是否正在运行:
systemctl status 服务名
例如查看SSH服务:
systemctl status ssh
输出中会显示服务是否处于“active (running)”或“inactive (dead)”状态。
启用服务开机自启
若希望某个服务在系统启动时自动运行,需将其设置为开机启用:
sudo systemctl enable 服务名
例如启用Nginx开机启动:
sudo systemctl enable nginx
该命令会创建一个符号链接,将服务单元文件链接到启动目标(如multi-user.target),确保系统启动时加载。
禁用服务开机自启
如果不需要某服务随系统启动,可执行禁用操作:
sudo systemctl disable 服务名
例如关闭Apache的开机启动:
sudo systemctl disable httpd
此操作会删除启动目标中的符号链接,但不会影响当前正在运行的服务。
立即启动或停止服务
启用或禁用仅影响下次开机行为。若需立即生效,应配合使用start、stop或restart命令:
- 启动服务:sudo systemctl start 服务名
 - 停止服务:sudo systemctl stop 服务名
 - 重启服务:sudo systemctl restart 服务名
 
这些操作即时生效,常用于配置更改后刷新服务。
基本上就这些。熟练使用enable、disable、start、stop等指令,就能有效控制系统服务的运行时机,提升安全性和资源利用率。


