systemctl命令。
chkconfig 是Red Hat、CentOS、Fedora等Linux发行版中用于管理SysV init系统服务的工具。它允许管理员在不同的运行级别(runlevel)中配置哪些服务在系统启动时自动启动,哪些服务需要手动启动。通过chkconfig,可以方便地管理系统服务的启动状态,确保系统按照需要运行必要的服务。
chkconfig [选项] [服务名] [on|off|reset|resetpriorities]
| 选项 | 说明 |
|---|---|
--list [服务名] |
列出服务的启动状态(如果不指定服务名,则列出所有服务) |
--add 服务名 |
添加一个新的服务到chkconfig管理 |
--del 服务名 |
从chkconfig管理中删除一个服务 |
--level 运行级别 |
指定要操作的运行级别(可以是单个或多个,如2345) |
--override 服务名 |
覆盖服务的默认配置 |
--no-redirect |
不重定向输出(与--list一起使用时) |
--version |
显示版本信息 |
--help |
显示帮助信息 |
| 运行级别 | 描述 | 典型用途 |
|---|---|---|
| 0 | 停机(halt) | 关闭系统 |
| 1 | 单用户模式(single user mode) | 系统恢复、维护 |
| 2 | 多用户模式,无网络(Multiuser, without NFS) | 无网络连接的多用户模式 |
| 3 | 完全多用户模式(Full multiuser mode) | 标准操作模式,文本界面 |
| 4 | 未使用(User-definable) | 可根据需要自定义 |
| 5 | 图形界面模式(X11) | 图形登录界面 |
| 6 | 重启(reboot) | 重新启动系统 |
chkconfig --list
输出示例:
NetworkManager 0:off 1:off 2:on 3:on 4:on 5:on 6:off
auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
...
chkconfig --list sshd
显示sshd服务在不同运行级别的启动状态。
启用sshd服务在运行级别3、5自启动:
sudo chkconfig --level 35 sshd on
设置sshd在运行级别3和5自动启动。
sudo chkconfig httpd off
禁用httpd服务在所有运行级别的自动启动。
sudo chkconfig --add myservice
将myservice服务添加到chkconfig管理系统中。
sudo chkconfig --del myservice
从chkconfig管理系统中删除myservice服务。
sudo chkconfig sshd reset
重置sshd服务的启动状态到默认设置。
sudo chkconfig --level 2345 crond on
设置crond服务在运行级别2、3、4、5自动启动。
SysV init服务配置文件位置:
/etc/rc.d/init.d/ # 服务启动脚本目录
/etc/rc.d/rc0.d/ # 运行级别0对应的服务链接目录
/etc/rc.d/rc1.d/ # 运行级别1对应的服务链接目录
/etc/rc.d/rc2.d/ # 运行级别2对应的服务链接目录
/etc/rc.d/rc3.d/ # 运行级别3对应的服务链接目录
/etc/rc.d/rc4.d/ # 运行级别4对应的服务链接目录
/etc/rc.d/rc5.d/ # 运行级别5对应的服务链接目录
/etc/rc.d/rc6.d/ # 运行级别6对应的服务链接目录
服务链接命名规则:
S##服务名:启动服务(S表示Start,##表示启动顺序)K##服务名:停止服务(K表示Kill,##表示停止顺序)S10network表示启动顺序10的network服务| 特性 | chkconfig (SysV init) | systemctl (systemd) |
|---|---|---|
| 系统版本 | RHEL/CentOS 6及以下 | RHEL/CentOS 7及以上 |
| 命令语法 | chkconfig --listchkconfig sshd on |
systemctl list-unit-filessystemctl enable sshd |
| 运行级别 | 0-6共7个运行级别 | 使用target(如multi-user.target) |
| 服务状态查看 | service sshd status |
systemctl status sshd |
| 服务管理 | service sshd start|stop|restart |
systemctl start|stop|restart sshd |
| 配置文件 | /etc/rc.d/init.d/ |
/usr/lib/systemd/system/ |
| 服务名称 | 描述 | 典型设置 |
|---|---|---|
sshd |
SSH远程登录服务 | 运行级别3、5启用 |
httpd |
Apache Web服务器 | 运行级别3、5启用 |
mysqld |
MySQL数据库服务 | 运行级别3、5启用 |
crond |
定时任务服务 | 所有运行级别启用(2-5) |
network |
网络服务 | 运行级别2、3、4、5启用 |
iptables |
防火墙服务 | 运行级别3、5启用 |
auditd |
审计服务 | 运行级别3、5启用 |
syslog |
系统日志服务 | 所有运行级别启用(2-5) |
创建自定义服务的步骤:
/etc/rc.d/init.d/示例服务脚本:
#!/bin/bash
# /etc/rc.d/init.d/myservice
# chkconfig: 2345 90 10
# description: My custom service
# 服务管理逻辑
case "$1" in
start)
echo "Starting myservice..."
# 启动命令
;;
stop)
echo "Stopping myservice..."
# 停止命令
;;
restart)
echo "Restarting myservice..."
$0 stop
$0 start
;;
status)
echo "Checking myservice status..."
# 状态检查命令
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
exit 0
关键配置行说明:
# chkconfig: 2345 90 10:表示在运行级别2、3、4、5启动,启动顺序90,停止顺序10# description: My custom service:服务描述信息添加服务到chkconfig:
# 设置脚本权限
sudo chmod +x /etc/rc.d/init.d/myservice
# 添加服务到chkconfig管理
sudo chkconfig --add myservice
# 启用服务
sudo chkconfig myservice on
# 验证服务状态
sudo chkconfig --list myservice
chkconfig --list | grep ":on"ls /etc/rc.d/rc3.d/service 服务名 start 或 /etc/init.d/服务名 startps aux | grep 服务名 或 netstat -tulpn | grep 端口cp -r /etc/rc.d/init.d/ /backup/init.d_backuptail -f /var/log/boot.logsudo yum install chkconfig(RHEL/CentOS)/var/log/messages/etc/rc.d/init.d/目录中在systemd系统中管理服务的对应命令:
# 查看服务状态(对应chkconfig --list)
systemctl list-unit-files --type=service
# 启用服务自启动(对应chkconfig 服务名 on)
systemctl enable 服务名
# 禁用服务自启动(对应chkconfig 服务名 off)
systemctl disable 服务名
# 查看服务是否启用(对应chkconfig --list 服务名)
systemctl is-enabled 服务名
# 启动服务(对应service 服务名 start)
systemctl start 服务名
# 停止服务(对应service 服务名 stop)
systemctl stop 服务名
# 重启服务(对应service 服务名 restart)
systemctl restart 服务名
# 查看服务状态(对应service 服务名 status)
systemctl status 服务名
转换注意事项:在从SysV init迁移到systemd时,原有服务通常会自动兼容,但自定义服务可能需要创建systemd服务单元文件。
sudo yum install httpd mysql-server php
# 启用Apache
sudo chkconfig --level 235 httpd on
# 启用MySQL
sudo chkconfig --level 235 mysqld on
# 验证配置
sudo chkconfig --list httpd
sudo chkconfig --list mysqld
sudo service httpd start
sudo service mysqld start
# 启用防火墙
sudo chkconfig --level 235 iptables on
# 添加HTTP端口例外
sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT
sudo service iptables save
sudo reboot
# 重启后检查服务状态
service httpd status
service mysqld status
netstat -tulpn | grep :80