CentOS8系统启动流程:
1)BIOS加电自检
2)把MBR加载到内存
3)加载grub引导程序
4)Kernel自身初始化
5)启动第一个程序systemd
#可通过ps -ef查看,systemd的pid为1
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 09:54 ? 00:00:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 17
6)检查默认运行级别(默认是3)
[root@localhost ~]# cd /etc/systemd/system/
[root@localhost system]# ll
lrwxrwxrwx. 1 root root 37 Jun 9 12:03 default.target -> /lib/systemd/system/multi-user.target
7)启动相应运行级别下的所有程序服务 (用户可管理)
[root@localhost system]# cd multi-user.target.wants/
[root@localhost multi-user.target.wants]# ls (这里存放着需要开机启动的服务)
atd.service libstoragemgmt.service sshd.service
auditd.service mcelog.service sssd.service
crond.service mdmonitor.service tuned.service
dnf-makecache.timer NetworkManager.service vdo.service
firewalld.service remote-fs.target vmtoolsd.service
irqbalance.service rsyslog.service
kdump.service smartd.service
8)加载/etc/rc.d/rc.local脚本
用户可将自定义开机启动脚本放rc.local文件内,并修改rc.local为执行权限
[root@localhost ~]# cat /etc/rc.d/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
#这里说明,如果要在开机启动执行rc.local文件,必须将rc.local文件权限改成可执行,默认是普通文件
touch /var/lock/subsys/local
9)systemd执行multi-user.target下的getty.target及登录服务
[root@localhost ~]# cd /lib/systemd/system
[root@localhost system]# cd multi-user.target.wants/
[root@localhost multi-user.target.wants]# ls (这里存放着与登录有关的服务)
dbus.service systemd-ask-password-wall.path
getty.target systemd-logind.service
plymouth-quit.service systemd-update-utmp-runlevel.service
plymouth-quit-wait.service systemd-user-sessions.service
10)systemd执行graphical需要的服务(如果不使用图形界面,这步省略)
linux运行级别:
0 shutdown.target(关机)
1 emergency.target (紧急救援模式)
2 rescue.target (救援模式)
3 multi-user.target(多用户模式|字符系统模式)
4 无
5 graphical.target (桌面系统)
6 无(重启)
服务运行级别配置:
1)查看默认级别
systemctl get-default
2)查看当前运行级别
runlevel
3)设置默认级别
systemctl set-default multi-user.target
4)切换运行级别
方法1:init命令
init 0|1|3|5|6
方法2:systemctl命令
systemctl isolate multi-user.target
systemctl isolate graphical.target
5)列出所有的target
systemctl list-units --type=target --all
6)查看系统中所有服务的启动状态
systemctl list-unit-files
7)查看某个服务状态
systemctl status sshd.service
systemctl is-active sshd.service
systemctl is-enabled sshd.service
systemctl list-unit-files |grep sshd.service