首页 > 技术文章 > Systemd 初始化进程

Hunry 2018-08-05 15:16 原文

1、Linux操作系统的开机过程

从BIOS开始 → 进入Boot Loader → 加载系统内核 → 内核进行初始化 → 启动初始化进程。

初始化进程作为Linux系统的第一个进程,它需要完成Linux系统中相关的初始化工作,为用户提供合适的工作环境。红帽RHEL 7系统已经替换掉了熟悉的初始化进程服务System V init,正式采用全新的systemd初始化进程服务。如果读者之前学习的是RHEL 5或RHEL 6系统,可能会不习惯。systemd初始化进程服务采用了并发启动机制,开机速度得到了不小的提升。虽然systemd初始化进程服务具有很多新特性和优势,但目前还是下面4个槽点。

  • 槽点1:systemd初始化进程服务的开发人员Lennart Poettering就职于红帽公司,这让其他系统的粉丝很不爽。
  • 槽点2:systemd初始化进程服务仅仅可在Linux系统下运行,“抛弃”了UNIX系统用户。
  • 槽点3:systemd接管了诸如syslogd、udev、cgroup等服务的工作,不再甘心只做初始化进程服务。
  • 槽点4:使用systemd初始化进程服务后,RHEL 7系统变化太大,而相关的参考文档不多,令用户着实为难。

2、systemd与System V init的区别以及作用

无论怎样,RHEL 7系统选择systemd初始化进程服务已经是一个既定事实,因此也没有了“运行级别”这个概念,Linux系统在启动时要进行大量的初始化工作,比如挂载文件系统和交换分区、启动各类进程服务等,这些都可以看作是一个一个的单元(Unit),systemd用目标(target)代替了System V init中运行级别的概念,这两者的区别如表1所示。

表1 systemd与System V init的区别以及作用

System V init运行级别

systemd目标名称

作用

0

runlevel0.target, poweroff.target

关机

1

runlevel1.target, rescue.target

单用户模式

2

runlevel2.target, multi-user.target

等同于级别3

3

runlevel3.target, multi-user.target

多用户的文本界面

4

runlevel4.target, multi-user.target

等同于级别3

5

runlevel5.target, graphical.target

多用户的图形界面

6

runlevel6.target, reboot.target

重启

emergency

emergency.target

紧急Shell

 

如果想要将系统默认的运行目标修改为“多用户,无图形”模式,可直接用ln命令把多用户模式目标文件连接到/etc/systemd/system/目录:

 [root@linuxprobe ~]# ln -sf /lib/systemd/system/multi-user.target /etc/systemd/

 system/default.target 

 

3、RHEL 6系统中System V init命令与RHEL 7系统中systemctl命令的对比

如果有读者之前学习过RHEL 6系统,或者已经习惯使用service、chkconfig等命令来管理系统服务,那么现在就比较郁闷了,因为在RHEL 7系统中是使用systemctl命令来管理服务的。表2和表3所示RHEL 6系统中System V init命令与RHEL 7系统中systemctl命令的对比,您可以先大致了解一下,后续章节中会经常用到它们。

表2 systemctl管理服务的启动、重启、停止、重载、查看状态等常用命令

System V init命令(RHEL 6系统)

systemctl命令RHEL 7系统)

作用

service foo start

systemctl start foo.service

启动服务

service  foo restart

systemctl restart foo.service

重启服务

service foo stop

systemctl stop foo.service

停止服务

service foo reload

systemctl reload foo.service

重新加载配置文件(不终止服务)

service foo status

systemctl status foo.service

查看服务状态

 

表3   systemctl设置服务开机启动、不启动、查看各级别下服务启动状态等常用命令

System V init命令(RHEL 6系统)

systemctl命令(RHEL 7系统)

作用

chkconfig foo on

systemctl enable foo.service

开机自动启动

chkconfig foo off

systemctl disable foo.service

开机不自动启动

chkconfig foo

systemctl is-enabled foo.service

查看特定服务是否为开机自动启动

chkconfig --list

systemctl list-unit-files --type=service

查看各个级别下服务的启动与禁用情况

推荐阅读