Linux chkconfig 命令示例--添加、删除、查看、更改服务

举报
Tiamo_T 发表于 2022/04/13 10:10:56 2022/04/13
【摘要】 Chkconfig 命令用于设置、查看或更改配置为在系统启动期间自动启动的服务。 本文包含 7 个实际示例,说明如何使用 chkconfig 命令。

Chkconfig 命令用于设置、查看或更改配置为在系统启动期间自动启动的服务。

本文包含 7 个实际示例,说明如何使用 chkconfig 命令。

1. 从 Shell 脚本检查服务启动状态

当您仅使用服务名称执行 chkconfig 命令时,如果服务配置为启动,则返回 true。以下代码片段显示了如何从 shell 脚本检查服务是否配置为启动。

# vi check.sh
chkconfig network && echo "Network service is configured"
chkconfig junk && echo "Junk service is configured"

# ./check.sh
Network service is configured

您还可以专门检查它是否配置为特定的运行级别。

# vi check1.sh
chkconfig network --level 3 && echo "Network service is configured for level 3"
chkconfig network --level 1 && echo "Network service is configured for level 1"

# ./check1.sh
Network service is configured for level 3

2.查看启动服务的当前状态

–list 选项显示具有当前启动配置状态的所有服务。

# chkconfig --list
abrtd   0:off   1:off   2:off   3:on    4:off   5:on    6:off
acpid   0:off   1:off   2:off   3:off   4:off   5:off   6:off
atd     0:off   1:off   2:off   3:on    4:on    5:on    6:off
...

要仅查看配置为在系统启动期间启动的服务,请执行以下操作。请注意,这假设您的系统启动级别为 3。

chkconfig --list | grep 3:on

注意:要查看所有可用的系统运行级别


要查看特定服务的启动配置,请 grep 该服务的“chkconfig –list”输出。

chkconfig --list | grep network

3.在启动中添加一个新服务

使用 –add 选项将特定服务添加到将在系统重新启动期间启动的服务列表中。

以下示例显示如何将新服务(例如 iptables)添加到需要启动的服务列表中。'chkconfig –add' 命令也将自动打开级别 2、3、4 和 5,如下所示。

# chkconfig --list | grep iptables

# chkconfig --add iptables

# chkconfig --list | grep iptables
iptables       0:off   1:off   2:on    3:on    4:on    5:on    6:off

注意:“chkconfig –add”仅将现有服务添加到启动列表中。如果该服务不存在,您应该先安装它,然后再将其添加到系统启动列表中。虽然这很明显,但值得一提的是,新手可能会犯这个错误。

4.从启动列表中删除服务

以下示例显示为启动配置了 ip6tables 服务。

# chkconfig --list | grep ip6tables
ip6tables 0:off 1:off 2:off 3:on 4:off 5:off 6:off

要将其从启动列表中删除,请使用 –del 选项,如下所示。

# chkconfig --del ip6tables

# chkconfig --list | grep ip6tables

5.为选定的运行级别打开或关闭服务

有时您可能不想删除整个服务。相反,您可能只想为特定运行级别(针对特定服务)打开或关闭标志。

以下示例将关闭 5 级的 nfserver 服务

# chkconfig --level 5 nfsserver off

您还可以组合多个级别。以下示例将关闭级别 3 和级别 5 的 nfsserver。

# chkconfig --level 35 nfsserver off

6. rc.d 子目录下的脚本文件

每当您从 chkconfig 控件添加或删除服务时,它都会在 /etc/rc.d 子目录下执行以下操作。

  • 执行 chkconfig –add 命令时,会在对应的 rc 目录下创建一个符号链接文件来启动和停止服务。
  • 当执行 chkconfig –del 命令时,它会从相应的 rc 目录中删除符号链接文件。

以下示例显示为运行级别 3 和 5 启用了 xinetd。

所以,xinetd 在 rc3.d 目录下会有两个文件,在 rc5.d 目录下会有两个文件。以 K 开头的文件在关机时使用(K 代表 kill)。以 S 开头的文件在启动时使用(S 代表启动)。

# chkconfig --list | grep xinetd
xinetd                    0:off  1:off  2:off  3:on   4:off  5:on   6:off
xinetd based services:

# cd /etc/rc.d/rc3.d
# ls | grep xinetd
K08xinetd
S14xinetd

# cd /etc/rc.d/rc5.d

# ls | grep xinetd
K08xinetd
S14xinetd

7. 添加操作的rcx.d目录更改

当您将新服务添加到 chkconfig 控件时,该服务的默认运行级别将自动打开,并在相应的 rcx 目录下创建文件。

例如,如果 chkconfig 控件中不存在 nfsserver 服务,则 /etc/rc.d/rc*.d 目录下将不存在 nfsserver 服务启动文件,如下所示。

# chkconfig  --list | grep nfsserver
nfsserver                 0:off  1:off  2:off  3:off  4:off  5:off  6:off

# ls /etc/rc.d/rc3.d | grep nfsserver

# ls /etc/rc.d/rc5.d | grep nfsserver

添加 nfsserver 服务后,您将在这些目录下看到符号链接。

# chkconfig --add nfsserver
nfsserver                 0:off  1:off  2:off  3:on   4:off  5:on   6:off

# cd  /etc/rc.d/rc3.d
# ls -l | grep nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 K08nfsserver -> ../nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 S14nfsserver -> ../nfsserver

# cd /etc/rc.d/rc5.d
# ls -l | grep nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 K08nfsserver -> ../nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 S14nfsserver -> ../nfsserver

当您使用–del 选项或–level 选项关闭服务时,rcx.d 目录下的相应符号链接文件将被删除,如下所示。

# chkconfig --level 5 nfsserver off

# ls /etc/rc.d/rc5.d  | grep nfsserver
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。