一、功能说明 chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息。
注意:它不是直接对服务进行启动或停止,只是设置下一次启动时的操作。也只是简单的更改了符号链接。
二、语法格式 chkconfig [选项] [参数]
chkconfig [选项] [参数] [on/off/reset]
三、常用选项 --add:添加服务给chkconfig管理
--del:删除chkconfig列表里面某个服务
--list:列出chkconfig管理的服务列表
--level:设置级别(启动级别)
四、参数 服务的名称
五、实例 列出当前的开机启动的服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| [iyunv@lixin init.d]# chkconfig--list
abrt-ccpp 0:off 1:off 2:off 3:off 4:off 5:off 6:off
abrtd 0:off 1:off 2:off 3:off 4:off 5:off 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:off 4:off 5:off 6:off
auditd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
cpuspeed 0:off 1:on 2:off 3:off 4:off 5:off 6:off
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
haldaemon 0:off 1:off 2:off 3:off 4:off 5:off 6:off
ip6tables 0:off 1:off 2:off 3:off 4:off 5:off 6:off
iptables 0:off 1:off 2:off 3:off 4:off 5:off 6:off
irqbalance 0:off 1:off 2:off 3:off 4:off 5:off 6:off
kdump 0:off 1:off 2:off 3:off 4:off 5:off 6:off
lvm2-monitor 0:off 1:on 2:off 3:off 4:off 5:off 6:off
mdmonitor 0:off 1:off 2:off 3:off 4:off 5:off 6:off
messagebus 0:off 1:off 2:off 3:off 4:off 5:off 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
netfs 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
nfs-rdma 0:off 1:off 2:off 3:off 4:off 5:off 6:off
sysstat 0:off 1:on 2:on 3:on 4:on 5:on 6:off
udev-post 0:off 1:on 2:off 3:off 4:off 5:off 6:off
[iyunv@lixin init.d]#
|
六、扩展1) 添加服务
1
| [iyunv@lixin init.d]# chkconfig--add lixin
|
注意:需要把启动脚本放在/etc/init.d下才可以使用chkconfig管理,并且由于chkconfig管理需要服务的启动脚本必须拥有指定格式才可以,所以我们添加的自启动服务,需要在文件头添加固定的格式 [iyunv@lixin init.d]# headiptables #!/bin/sh # # iptables Start iptables firewall # # chkconfig: 2345 08 92 //定义启动级别,以及start和kill顺序 # description: Starts,stops and saves iptables firewall # //定义文件描述符 # [iyunv@lixin init.d]# 固定格式为:#chkconfig 234508 92 #description:xxxxxx
其中chkconfig 2345 0892,分别表示启动级别、启动顺序、关闭顺序。 启动级别分为7中:
- 0级别:表示关机
- 1级别:表示单用户,一般用来做root密码修改等
- 2级别:多用户但不包含NFS服务
- 3级别:完整的多用户模式(命令行模式)
- 4级别:保留未使用
- 5级别:表示图形界面
- 6级别:表示重启
- 这些级别在/etc/inittab中有明确定义。
开机关闭顺序,是我们手工指定的,指定的时候最好去/etc/rc.d/rc3.d下查看,一般避免和其他服务重复数字,并且尽量保持在100以内。
小结:我们执行chkconfig –level 3sshd on 时,它会在对应级别的启动目录下,产生一个以S开头的软连接用来指定这个服务的位置(在/etc/init.d/下)。如果 off掉,它会把之前的文件删掉,产生一个以K开头的软连接文件指定服务的位置。(K,Kill。S,Start) 2) 精简开机启动服务由于系统完毕后,基本所有的服务在2345级别都是打开的状态,为了优化服务,我们一般会选择性的开启那些我们必须的服务,规避一下安全隐患。 - sshd服务:用来提供用户登录的服务。
- crond服务:用来执行计划(定时)任务。
- network服务:用来提供网络服务。
- rsyslog服务:用来打印系统日志信息的服务。
- sysstat服务:是一个软件包,包含监测系统性能及效率的一组工具包含:
- iostat:提供CPU的使用率等数据
- mpstat:提供单个或多个处理器的数据
- sar:收集、报告存储系统的活跃信息
建议新系统只开启上述5个服务 3) 批量开关服务方法1:因为所有服务默认是开启的,所以我们只需要关闭不需要的服务。 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
| [iyunv@lixin rc3.d]# chkconfig
abrt-ccpp 0:off 1:off 2:on 3:on 4:on 5:on 6:off
smartd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
svnserve 0:off 1:off 2:on 3:on 4:on 5:on 6:off
sysstat 0:off 1:on 2:on 3:on 4:on 5:on 6:off
udev-post 0:off 1:on 2:on 3:on 4:on 5:on 6:off
……
[iyunv@lixin rc3.d]#
[iyunv@lixin rc3.d]# chkconfig |grep -Ev "sshd|crond|network|rsyslog|sysstat"
abrt-ccpp 0:off 1:off 2:on 3:on 4:on 5:on 6:off
restorecond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
rngd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
saslauthd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
smartd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
svnserve 0:off 1:off 2:on 3:on 4:on 5:on 6:off
udev-post 0:off 1:on 2:on 3:on 4:on 5:on 6:off
…… //筛选出除sshd,crond,network,sysstat,rsyslog以外的服务
[iyunv@lixin rc3.d]# chkconfig |grep -Ev "sshd|crond|network|rsyslog|sysstat" | awk '{print $1}'
lvm2-monitor
mdmonitor
messagebus
netconsole
netfs
nfs-rdma
ntpd
ntpdate
postfix
psacct
quota_nld
…… //利用awk用空格分隔把服务的名称取出
[iyunv@lixin rc3.d]# chkconfig |grep -Ev "sshd|crond|network|rsyslog|sysstat" | awk '{print"chkconfig",$1,"off"}'
chkconfig restorecond off
chkconfig rngd off
chkconfig saslauthd off
chkconfig smartd off
chkconfig svnserve off
chkconfig udev-post off
…… //利用awk的print参数拼出命令
[iyunv@lixin rc3.d]# chkconfig |grep -Ev "sshd|crond|network|rsyslog|sysstat" | awk '{print"chkconfig",$1,"off"}' |bash //交给bash来执行
[iyunv@lixin rc3.d]# [root@lixinrc3.d]# chkconfig |grep -E 'crond|sshd|network|rsyslog|sysstat'
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off
sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
sysstat 0:off 1:on 2:on 3:on 4:on 5:on 6:off
[iyunv@lixin rc3.d]#
|
方法二:首先关闭所有服务,然后只开启我们需要的五个服务 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
| [iyunv@lixin rc3.d]# chkconfig |awk '{print $1}'
abrt-ccpp
abrtd
acpid
atd
sshd
svnserve
sysstat
…… //用awk过滤出服务名称
[iyunv@lixin rc3.d]# chkconfig |awk '{print "chkconfig",$1,"off"}'
chkconfig postfix off
chkconfig psacct off
chkconfig quota_nld off
chkconfig rdisc off
chkconfig rdma off
chkconfig restorecond off
chkconfig rngd off
…… //使用awk的print参数拼出命令
[iyunv@lixin rc3.d]# chkconfig | awk '{print"chkconfig",$1,"off"}'| bash //关闭所有服务
[iyunv@lixin rc3.d]# chkconfig
abrt-ccpp 0:off 1:off 2:off 3:off 4:off 5:off 6:off
abrtd 0:off 1:off 2:off 3:off 4:off 5:off 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:off 4:off 5:off 6:off
auditd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
messagebus 0:off 1:off 2:off 3:off 4:off 5:off 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
netfs 0:off 1:off 2:off 3:off 4:off 5:off 6:off
……
[iyunv@lixin rc3.d]# chkconfig--list|egrep "crond|sshd|network|rsyslog|sysstat"|awk '{print"chkconfig",$1,"on"}'|bash //开启五个服务
[iyunv@lixin rc3.d]# chkconfig|grep -E 'crond|sshd|network|rsyslog|sysstat' //验证
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off
sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
sysstat 0:off 1:on 2:on 3:on 4:on 5:on 6:off
[iyunv@lixin rc3.d]#
|
|