使用 chkconfig 删除服务
如果我们不再需要使用某个服务,我们可以在启动时使用“chkconfig off”开关禁用它:
# chkconfig [servicename] off
然后,我们应该继续使用以下命令停止服务运行:
# service [servicename] stop
上述命令将立即生效。
但是,为了完成此过程,我们可能希望通过键入以下内容将其从 chkconfig 管理工具中删除:
# chkconfig --del [servicename]
重置服务信息
使用服务是有教育意义的,只要我们有 /etc/rc.d 目录树的备份以及返回系统以恢复它的方法。
然而,这种激烈的行动通常是没有必要的。
相反,我们可以通过发出以下命令将服务的启动优先级和其他信息恢复为推荐设置。
# chkconfig [servicename] reset
此命令将所有内容恢复为(希望)正常的默认值。
使用 chkconfig 添加服务
要根据 chkconfig 的建议向所有运行级别添加新服务,请使用以下命令:
# chkconfig --add [servicename]
chkconfig 一举将服务的所有链接设置在正确的目录中。
注意:安装应用程序或者服务时,会生成初始化脚本并自动添加到 /etc/init.d 中。
因此,如果我们在识别服务名称时遇到困难,请访问 /etc/init.d,找到适当的脚本并从其内容中获取服务名称。
在启动时启用或者禁用服务
在这个例子中,我们将使用 iptables 服务。
如果需要,请列出服务将启动的当前规则:
# chkconfig --list iptables httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
“chkconfig on”不指定任何运行级别将在运行级别 2. 3. 4 和 5 上启用服务。
例如:
# chkconfig iptables on
# chkconfig --list iptables iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
同样,要在所有运行级别禁用该服务,请使用“chkconfig off”命令。
例如:
# chkconfig iptables off
# chkconfig --list iptables iptables 0:off 1:off 2:off 3:off 4:off 5:off 6:off
chkconfig 精细控制
可以将 -level 选项提供给 chkconfig 以指定要进行更改的运行级别(打开或者关闭)。
其他运行级别不会改变。
这将配置系统以在运行级别 3 和 5 中启动 iptables:
# chkconfig --level 35 iptables on
# chkconfig --list iptables iptables 0:off 1:off 2:off 3:on 4:off 5:on 6:off
CentOS/RHEL 为我们提供了一个简单的命令行工具 (chkconfig),用于管理在系统不同运行级别期间启动的服务。
chkconfig 需要在实际的 init 脚本中添加一些另外的注释行,以告诉它应该在哪个运行级别启动服务,以及在运行级别初始化期间相对应何时启动服务。
(init 脚本按照特定的顺序处理,以确保依赖于其他人的服务在它们所依赖的服务之后启动。
)这些行,取自 httpd init 脚本,如下所示:
# chkconfig: 345 85 15 # description: Apache is a World Wide Web server. It is used to serve # HTML files and CGI.
其中:
345 - 默认情况下将启用服务的运行级别。
85 - 启动优先级。
数字越小,优先级越高,服务在给定的运行级别内启动得越快。
15 - 停止优先。
数字越小,优先级越高,服务在给定的运行级别内停止的速度越快。
使用 chkconfig 列出服务
要获取在哪个运行级别启动的服务列表,请使用命令“chkconfig -list”。
# chkconfig --list acpid 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 blk-availability 0:off 1:on 2:on 3:on 4:on 5:on 6:off cgconfig 0:off 1:off 2:off 3:off 4:off 5:off 6:off ...
或者,我们可以添加一个名称作为添加参数,chkconfig 将仅列出该服务的信息。
以下是我系统上 chkconfig -list iptables 的输出:
# chkconfig --list iptables iptables 0:off 1:off 2:off 3:on 4:on 5:on 6:off
在这种情况下,chkconfig 报告要为运行级别 3. 4 和 5 启动 iptables 服务。