systemd 服务管理
学习使用 systemctl 管理系统服务、查看状态和启用/禁用服务。 · 难度:入门 · +15XP
概念
systemd 是现代 Linux 的初始化系统和服务管理器。服务单元文件位于 /etc/systemd/system/。用法
systemctl status service:查看服务状态。systemctl start/stop/restart service:启动/停止/重启。systemctl enable/disable service:开机自启/禁用。systemctl list-units --type=service:列出所有服务。journalctl -u service:查看服务日志。代码示例
systemctl status sshd
sudo systemctl restart networking
systemctl enable --now nginx
journalctl -u sshd --since "1 hour ago"
练习提示
1. 查看cron 或 ssh 服务的状态。
2. 用 systemctl list-units --failed 查看失败服务。
3. 创建一个简单的自定义服务单元文件。
4. 用 systemctl mask 完全禁用服务。
5. 使用 journalctl -f 实时查看系统日志。