Day22-系统服务(开机启动流程、系统运行级别、sytemd使用、单用户模式、救援模式)

Hyman's Road of Learning Linux.

Linux开机启动流程

CentOS 6和CentOS 7开机流程

CentOS 6和CentOS 7开机流程

Linux运行级别

1.什么是运行级别,运行级别就是操作系统当前正在运行的功能级别

System V init运行级别 systemd目标名称 作用
0 runlevel0.target, poweroff.target 关机
1 runlevel1.target, rescue.target 单用户模式
2 runlevel2.target, multi-user.target
3 runlevel3.target, multi-user.target 多用户的文本界面
4 runlevel4.target, multi-user.target
5 runlevel5.target, graphical.target 多用户的图形界面
6 runlevel6.target, reboot.target 重启

2.如何调整系统启动的运行级别?

systemd使用 'targets'而不是runlevels。默认情况下,有两个主要目标:

multi-user.target:类似于运行级别3
graphical.target: 类似于运行级别5*

#1.查看系统默认运行级别
[root@student ~]# systemctl get-default

#2.要设置默认目标,请运行
[root@student ~]# systemctl set-default TARGET.target

Linux systemd

1.systemd的由来

Linux一直以来都是采用init进程作为祖宗进程,但是init有两个缺点:

1、启动时间长。Init进程是串行启动,只有前一个进程启动完,才会启动下一个进程。
2、启动脚本复杂,初始化完成后系统会加载很多脚本,脚本都会处理各自的情况,这会让脚本多而复杂。

Centos5 是启动速度最慢的,串行启动过程,无论进程相互之间有无依赖关系。
Centos6 相对启动速度有所改进。有依赖的进程之间依次启动而其他与之没有依赖关系的则并行同步启动。
Centos7 所有进程无论有无依赖关系则都是并行启动(当然很多时候进程没有真正启动而是只有一个信号或者说是标记而已,在真正利用的时候才会真正启动。)

2.什么是systemd

systemd即为system daemon守护进程,systemd主要解决上文的问题而诞生,systemd的目标是,为系统的启动和管理提供一套完整的解决方案。

3.systemd的优势

1、最新系统都采用systemd管理(RedHat7,CentOS7,Ubuntu15等)
2、Centos7支持开机并行启动服务,显著提高开机启动效率。
3、Centos7关机只关闭正在运行的服务,而Centos6全部都关闭一次。
4、Centos7服务的启动与停止不在使用脚本进行管理,也就是/etc/init.d下不在有脚本。
5、Centos7使用systemd解决原有模式缺陷,比如原有service不会关闭程序产生的子进程。

4.systemd相关配置文件

/usr/lib/systemd/system/ #类似Centos6系统的启动脚本,/etc/init.d/
/etc/systemd/system/ #类似Centos6系统的/etc/rc.d/rcN.d/
/etc/systemd/system/multi-user.target.wants/

5.systemd管理服务相关命令

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

systemctl命令 作用
systemctl start crond.service 启动服务
systemctl stop crond.service 停止服务
systemctl restart crond.service 重启服务
systemctl reload crond.service 重新加载配置
systemctl status crond.servre 查看服务运行状态
systemctl is-active sshd.service 查看服务是否在运行中
systemctl mask crond.servre 禁止服务运行
systemctl unmask crond.servre 取消禁止服务运行

当我们使用systemctl启动一个守护进程后,可以通过sysytemctl status查看此守护进程的状态

状态 描述
loaded 服务单元的配置文件已经被处理
active(running) 服务持续运行
active(exited) 服务成功完成一次的配置
active(waiting) 服务已经运行但在等待某个事件
inactive 服务没有在运行
enabled 服务设定为开机运行
disabled 服务设定为开机不运行
static 服务开机不启动,但可以被其他服务调用启动

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

systemctl命令(7系统) 作用
systemctl enable crond.service 开机自动启动
systemctl disable crond.service 开机不自动启动
systemctl list-unit-files 查看各个级别下服务的启动与禁用
systemctl is-enabled crond.service 查看特定服务是否为开机自启动
systemctl daemon-reload 创建新服务文件需要重载变更

CentOS7系统, 管理员可以使用 systemctl 命令来管理服务器启动与停止

#关机相关命令
systemctl poweroff      #立即关机,常用
#重启相关命令
systemctl reboot        #重启命令,常用

你可能感兴趣的:(Day22-系统服务(开机启动流程、系统运行级别、sytemd使用、单用户模式、救援模式))