CentOS Stream 8启动项管理方法
CentOS Stream 8采用systemd作为初始化系统,管理启动项(服务)的核心工具为systemctl命令,同时可通过图形界面或手动编辑配置文件实现更精细的控制。
systemctl是管理systemd服务的主要工具,支持查看、启动、停止、启用/禁用开机自启等操作。
systemctl list-unit-files --type=service --state=enabled
该命令列出所有设置为开机自启的服务(enabled状态)。sudo systemctl enable <service_name>
例如,启用nginx服务开机自启:sudo systemctl enable nginx。sudo systemctl disable <service_name>
例如,禁用httpd服务开机自启:sudo systemctl disable httpd。sudo systemctl start <service_name> # 启动服务
sudo systemctl stop <service_name> # 停止服务
sudo systemctl restart <service_name> # 重启服务
sudo systemctl status <service_name>
该命令显示服务的运行状态(如active (running)表示正在运行)、日志片段及依赖关系。systemctl is-enabled <service_name>
返回enabled表示已设置开机自启,disabled表示未设置。若系统安装了图形界面(如GNOME),可通过以下步骤管理启动项:
对于需要自定义启动逻辑的服务,可直接编辑其启动脚本或systemd单元文件:
/etc/init.d/目录下,可通过/etc/init.d/<service_name> start/stop命令管理服务,但systemd已取代此方式,不建议新系统使用。/etc/systemd/system/(自定义服务)或/usr/lib/systemd/system/(系统自带服务)。编辑后需执行以下命令使配置生效:sudo systemctl daemon-reload # 重新加载systemd配置
sudo systemctl restart <service_name> # 重启服务
例如,编辑nginx的单元文件:sudo vi /etc/systemd/system/nginx.service,修改[Service]或[Install]部分后保存。若需删除开机时的多余引导项(如“System setup”“0-rescue-XXX”),可编辑GRUB配置文件:
sudo cp /boot/efi/EFI/centos/grub.cfg /boot/efi/EFI/centos/grub.cfg.bak
sudo vi /etc/default/grub
修改GRUB_TIMEOUT(开机等待时间)、GRUB_DISABLE_RECOVERY(禁用恢复模式)等参数,例如:GRUB_TIMEOUT=3
GRUB_DISABLE_RECOVERY=true
sudo grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg
重启后生效。systemctl命令需root权限(或使用sudo)。network、sshd)可能导致系统无法正常启动,操作前需确认服务用途。