开机后,将根据主板BIOS 中的设置对硬件进行检测,如cpu,内存,显卡,等设备,检测第一个引导系统的设备,可以是硬盘,光驱,U盘,网络引。
当从本机硬盘中启动系统时,根据第一个硬盘的第一个扇区里的MBR设置调用GRUB启动菜单。centOS7 使用GRUB2 启动引导器
GRUB引导程序读取GRUB的配置文件 /boot/grub2/grub.cfg,获取内核和镜像文件系统的设置和路径位置
将内核和镜像文件系统 加载到内存中 。CentOS 7 默认的内核文件位于"/boot/vmlinuz-3.10.0-514.e17.x86 64"
加载硬件驱动程序,内核将init 进程加载到内存中运行。init程序是在/sbin/init 中。CentOS 7 中为Systemd
# 传统 SysVinit 依赖于串行执行she11脚本启动服务导致效率低下,系统启动速度较慢
用 pstree 命令 可以查看到 进程树
用 ps -aux 可以查看到PID号
在CentOS 7 中,查看 /sbin/init 可以发现是一个指向 /lib/systemd/systemd 的软链接
单元类型:
单元类型
|
扩展名
|
说明
|
Service
|
.service
|
描述一个系统服务
|
Socket
|
.socket
|
描述一个进程间通信的套接字
|
Device
|
.device
|
描述一个内核识别的设备文件
|
Mount
|
.mount
|
描述一个文件的挂载点
|
Automount
|
.automount
|
描述一个文件系统的自动挂载点
|
Swap
|
.swap
|
描述一个内存交换设备或交换文件
|
Path
|
.path
|
描述一个文件系统中文件或目录
|
Timer
|
.timer
|
描述一个定时器(用于实现类似cron的调度任务)
|
Snapshot
|
.snapshot
|
用于保存一个systemd的状态
|
Scope
|
.scope
|
使用systemd的总线接口以编程的方式创建外部进程
|
Slice
|
.slice
|
描述居于Cgroup的一组通过层次组织的管理系统进程
|
Target
|
.target
|
描述一组systemd的单元
|
CentOS 7 的 /etc/rc.d 目录下有7个 rc0-6.d 的目录,对应系统的7个运行级别(0-6)
init 运行级别
此命令可以快速切换系统的运行级别
2.2.1 runlevel 命令
runlevel 命令可以查看到之前一次的运行级别和当前的运行级别
[root@localhost ~]# runlevel N 5 #N 为null ,表示运行级别尚未改变。 5 为当前的运行级别 [root@localhost ~]# runlevel 5 3 # 5 表示上一次的运行级别为 5 , 当前的运行级别为 3
2.2.2 who 命令
who -r 命令 可以查看到当前和上一次的运行级别
[root@localhost ~]# who -r 运行级别 3 2021-06-22 19:46 最近=5 #当前的运行级别是5 ,上一次的是3
2.2.3 systemctl 工具
systemctl 可以查看到默认的运行级别
[root@localhost ~]# systemctl get-default #查看当前的默认运行级别 graphical.target #这个默认运行级别是 5 图形化模式
2.3.1 临时切换运行级别
(1) 使用init 命令
init 命令后面跟上运行级别,可以快速切换
[root@localhost ~]# runlevel 5 3 [root@localhost ~]# init 5 #切换到 5 运行级别 [root@localhost ~]# runlevel 3 5
(2) systemctl 命令
systemctl isolate systemd的target
运行级别与systemd 的target对应
运行级别
|
Systemd的target
|
说明
|
0
|
target(poweroff.target)
|
系统停机(关机)模式,系统默认运行级别不能设置为0,否则不能正常启动,一开机就自动关机。
|
1
|
rescue.target
|
单用户模式,root权限,用于系统维护,禁止远程登陆,就像Windows下的安全模式登录。
|
2
|
multi-user.target
|
多用户模式,没有NFS网络支持。
|
3
|
multi-user.target
|
完整的多用户文本模式,有NFS,登陆后进入控制台命令行模式。
|
4
|
multi-user.target
|
系统未使用,保留一般不用,在一些特殊情况下可以用它来做一些事情。例如在笔记本电脑的电池用尽时,可以切换到这个模式来做一些设置。
|
5
|
graphical.target
|
图形化模式,登陆后进入图形GUI模式或GNOME、KDE图形化界面,如X Window系统。
|
6
|
reboot.target
|
重启模式,默认运行级别不能设为6,否则不能正常启动,就会一直开机重启开机重启。
|
[root@localhost ~]# runlevel 3 5 [root@localhost ~]# systemctl isolate multi-user.target #切换到运行级别3 [root@localhost ~]# runlevel 5 3 [root@localhost ~]# systemctl isolate poweroff.target #切换到运行级别0 (关机) Connection closing...Socket close.
2.3.2 设置默认运行级别与永久切换运行级别
(1) systemctl set-default systemd的target
[root@localhost ~]# systemctl set-default multi-user.target #设置默认运行级别 Removed symlink /etc/systemd/system/default.target. Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target. [root@localhost ~]# systemctl get-default #查看默认运行级别 multi-user.target
2 ln -fs /lib/systemd/system/运行级别target标签 /etc/systemd/system/default.target
查看默认运行级别,实际是查看软连接文件 /etc/systemd/system/default.target 。所以,可以通过修改该软连接的源文件,来修改默认运行级别
[root@localhost ~]# ll /etc/systemd/system/default.target lrwxrwxrwx. 1 root root 41 6月 23 19:04 /etc/systemd/system/default.target -> /usr/lib/systemd/system/multi-user.target [root@localhost ~]# ls /lib/systemd/system/multi-user.target /lib/systemd/system/multi-user.target [root@localhost ~]# ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
#将/etc/systemd/system/default.target 强制做成 /lib/systemd/system/multi-user.target 的软连接。 [root@localhost ~]# systemctl get-default multi-user.target #默认运行级别是 3 multi-user.target
(1)特点:
(2) 使用
命令行使用 ntsysv 命令进入
ntsysv 工具操作:
* 号表示启用自启
空格打上 * 或者 取消*
tab 键切换光标 到 【确定】 【取消】 或者切换回选项操作
systemctl 不提供交互式,可视化窗口
管理单个服务效率更高
用于CentOs 7 版本
systemctl 可以用 enable 和disable 开启和关闭 服务的自动启动
用 is-enabled 查看 服务是否设置了开机自启
[root@localhost ~]# systemctl is-enabled httpd #查看httpd 服务是否设置了开机自启 disabled #httpd 没有设置开机自启 [root@localhost ~]# systemctl enable httpd #设置httpd开机自启 Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service. [root@localhost ~]# systemctl is-enabled httpd enabled [root@localhost ~]# systemctl disable httpd #关闭 httpd 的开机自启 Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service. [root@localhost ~]# systemctl is-enabled httpd disabled
systemctl [选项] 命令 服务
常用选项
--after #在某个服务之后启动
--before #在某个服务之前启动
原文:https://www.cnblogs.com/zhijiyiyu/p/14920157.html