使用 chkconfig 命令管理系统服务

  使用 chkconfig 命令,可以方便简单地管理不同运行级别下的系统服务,它能够实现添加、删除、更改服务启动状态;以列表输出所有服务启动状态;检验某服务启动状态等。

说明:chkconfig 命令通过更改 /etc/rc[0-6].d 目录下的配置文件决定服务的启动状态

 

列出所有服务的启动状态

  在 chkconfig 命令后使用 --list 选项或者不使用任何选项,结果将会以列表形式输出所有服务及它们的配置

[lugir@hope ~]$ chkconfig
NetworkManager 0:off 1:off 2:on 3:on 4:on 5:on 6:off
abrtd 0:off 1:off 2:off 3:on 4:off 5:on 6:off
acpid 0:off 1:off 2:on 3:on 4:on 5:on 6:off
atd 0:off 1:off 2:off 3:on 4:on 5:on 6:off
auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
avahi-daemon 0:off 1:off 2:off 3:on 4:on 5:on 6:off
bluetooth 0:off 1:off 2:off 3:on 4:on 5:on 6:off
btseed 0:off 1:off 2:off 3:off 4:off 5:off 6:off
……  

 

列出指定服务的启动状态

  使用 "chkconfig --list [服务名称]" 列出指定服务的启动状态

[lugir@hope ~]$ chkconfig --list sendmail
NetworkManager  0:off 1:off 2:on 3:on 4:on 5:on 6:off

 

更改服务启动状态

  使用 "chkconfig --level [运行级别] [服务名称] [启动状态]" 命令,可以更改服务在指定运行级别下的启动状态。管理启动服务属于系统管理任务,需要 root 权限。

  1. [lugir@hope ~]$ su
  2. 密码:
  3. [root@hope lugir]# chkconfig --level 24 sendmail off
  4. [root@hope lugir]# chkconfig --list sendmail
  5. sendmail   0:off 1:off 2:off 3:on 4:off 5:on 6:off
  6. [root@hope lugir]# chkconfig --level 24 sendmail on
  7. [root@hope lugir]#

说明:

  • 第1, 2 行 - 切换成 root 用户
  • 第 3 行 - 将 sendmail 在运行级别 2, 4 的状态设置为 off。当用户在运行级别 2, 4 时,sendmail 服务就不会启动了
  • 第 4, 5 行 - 验证 sendmail 当前的启动状态 (运行级别 2, 4 确实 off 了)
  • 第 6 行 - 将 sendmail 在运行级别 2, 4 的启动状态设置为 on

 

添加和删除启动服务

  使用 "chkconfig --add [服务名]" 和 "chkconfig --del [服务名]",能够添加或删除指定的启动服务。如果只想禁用某服务,而不是删除,可以使用 "chkconfig [服务名] off",即禁止指定服务在所有运行级别下启动。

  注意:添加和删除启动服务所指定的 [服务名],需要存在于 /etc/rc.d/init.d 目录下

 


付费阅读