10.24- 10.25 chkconfig和systemd
chkconfig
windows系统有开机启动项,linux也同样有开机启动项。在centos6上的开机启动项管理工具为chkconfig,所有的开机启动服务都可以在/etc/init.d/目录看到。但是centos7已经不在用chkconfig管理开机启动项。
[root@cent01 sbin]# ls /etc/init.d/
functions mysqld netconsole network README
系统的预设服务都可以通过这种方式启动:
service 服务名 start|restart|stop
服务名就是/etc/init.d/目录下的这些文件。
启动crond服务除了用:service crond start
之外,还可以用 /etc/init.d/crond start
我们可以用chkconfig –list查看所有的服务及其在每个级别的开启状态。
[root@cent01 sbin]# chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
但是这里只显示了SysV管理的服务,centos6及之前都是这个管理工具,但是在centos7用的是systemd管理,所以systemd管理的服务在这里没有显示出来。
运行级别为系统启动级别,具体含义如下:
0 shutdown关机
1 单用户模式
2 无NFS支持的多用户模式
3 完全多用户模式,常用的命令行模式
4 保留给用户自定义
5 图形界面登录,比3多了一个图形界面
6 重启
运行级别0,1,6作为保留模式。通常做设置时,如果不指定级别,则默认为更改2,3,4,5。
[root@cent01 sbin]# chkconfig --level 3 network off //关闭3级别的network服务
[root@cent01 sbin]# chkconfig --list //3级别已关闭
network 0:关 1:关 2:开 3:关 4:开 5:开 6:关
[root@cent01 sbin]# chkconfig network on //不输入级别,默认打开2,3,4,5级别
[root@cent01 sbin]# chkconfig --list
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@cent01 sbin]# chkconfig --del network //删除network
[root@cent01 sbin]# chkconfig --list //network已消失
mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
[root@cent01 sbin]# chkconfig --add network //增加network服务
[root@cent01 sbin]# chkconfig --list //network服务又恢复了
mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
systemd
systemd是管理开机启动程序的工具,截止到2015年,大部分linux发行版都已经用systemd取代了SysV管理开机启动项。但是也遭到了一些批评,因为systemd违背了linux系统追求简单的设计思想。
d取自于daemon,表示守护神。systemd的PID为1。
SysV启动开机进程时一次只能启动一个,而systemd则一次可以启动多个服务,这样就导致systemd的开机速度会更快。
[root@cent01 sbin]# systemctl list-units --all --type=service //列出系统所有服务
UNIT LOAD ACTIVE SUB DESCRIPTION
abrt-ccpp.service loaded active exited Install ABRT coredump hook
abrt-oops.service loaded active running ABRT kernel log watcher
abrt-vmcore.service loaded inactive dead Harvest vmcores for ABRT
abrt-xorg.service loaded inactive dead ABRT Xorg log watcher
abrtd.service loaded active running ABRT Automated Bug Reporting Tool
accounts-daemon.service loaded inactive dead Accounts Service
alsa-restore.service loaded inactive dead Save/Restore Sound Card State
alsa-state.service loaded active running Manage Sound Card State (restore and store)
● apparmor.service not-found inactive dead apparmor.service
[root@cent01 sbin]# ls /usr/lib/systemd/system //启动的脚本文件目录
abrt-ccpp.service iscsid.socket saslauthd.service
abrtd.service iscsi.service selinux-policy-migrate-local-changes@.service
abrt-oops.service iscsi-shutdown.service serial-getty@.service
abrt-pstoreoops.service iscsiuio.service shutdown.target
abrt-vmcore.service iscsiuio.socket shutdown.target.wants
abrt-xorg.service kdump.service sigpwr.target
accounts-daemon.service kexec.target sleep.target
alsa-restore.service kexec.target.wants -.slice
alsa-state.service kmod-static-nodes.service slices.target
systemctl enable crond.service //开机启动 .service可以省略
systemctl disable crond.service //禁止开机启动
systemctl status crond.service //查看服务状态
systemctl start crond.service //启动服务
systemctl stop crond.service //停止服务
systemctl restart crond.service //重启服务
systemctl is-enabled crond.service //查看某个服务是否开机启动
unit
/usr/lib/systemd/system 此目录下列出了很多文件,这些文件都是unit。
类别如下:
- service 系统服务
- target 多个unit组成的组
- device 硬件设备
- mount 文件系统挂载点
- automount 自动挂载点
- path 文件或路径
- scope 不是由systemd启动的外部进程
- slice 进程组
- snapshot systemd快照
- socket 进程间通信的套接字
- swap swap文件
- timer 定时器
[root@cent01 ~]# systemctl list-units //列出正在运行的unit
[root@cent01 ~]# systemctl list-units --all //列出所有的unit,包括active和inactive
[root@cent01 ~]# systemctl list-units --all --state=inactive //列出inactive的unit
[root@cent01 ~]# systemctl list-units --all --type=service //列出所有状态的service
[root@cent01 ~]# systemctl list-units --type=service //列出active的service
[root@cent01 ~]# systemctl is-active crond.service //查看某个unit是否active
target
target类似于centos6的启动级别,target内又包含多个unit的组合,当然target内也可以包含target。启动target就是启动多个unit,用target来管理这些unit。
[root@cent01 ~]# systemctl list-units --all --type=target //查看当前所有的target
UNIT LOAD ACTIVE SUB DESCRIPTION
basic.target loaded active active Basic System
cryptsetup.target loaded active active Encrypted Volumes
emergency.target loaded inactive dead Emergency Mode
final.target loaded inactive dead Final Step
getty.target loaded active active Login Prompts
graphical.target loaded inactive dead Graphical Interface
[root@cent01 ~]# systemctl list-dependencies multi-user.target //查看一个target内包含的所有units
multi-user.target
● ├─abrt-ccpp.service
● ├─abrt-oops.service
● ├─abrt-vmcore.service
● ├─abrt-xorg.service
● ├─abrtd.service
● ├─atd.service
● ├─auditd.service
● ├─avahi-daemon.service
[root@cent01 ~]# systemctl get-default //查看系统默认的target
multi-user.target
[root@cent01 ~]# systemctl set-default multi-user.target //设置默认的target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.
运行级别
multi-user.target等同于centos6的运行级别3。他们的对应关系如下:
SysV运行级别 | systemd target | 备注 |
---|---|---|
0 | poweroff.target | 关闭系统 |
1 | rescure.target | 单用户模式 |
2 | multiuser.target | 用户自定义级别,通常识别为3 |
3 | multiuser.target | 多用户命令行模式 |
4 | multiuser.target | 用户自定义级别,通常识别为3 |
5 | graphical.target | 多用户图形界面,比级别3只多一个GUI |
6 | reboot.target | 重启 |
所以总结起来,一个service属于一种unit,多个unit组成一个target,当然target里面也可以包含target。
在/usr/lib/systemd/system/sshd.service中有一个字段[install],这里定义了该service属于哪个target。