一、linux操作系统引导过程
1.1 引导过程详解
开机自检(bios) 加电检测硬件是否有问题。 如果没有问题, 根据bios设置的第一启动项(第一个能够引导系统的设备),来寻找操作系统。
MBR引导 运行放在MBR扇区里的启动GRUB引导程序(MBR前446位是GRUB引导文件,后64位是分区),来启动完整的GRUB引导程序。
GRUB菜单 GRUB引导程序通过配置文件 /boot/grub2/grub.cfg来找到操作系统。
加载内核 内核是操作系统的核心。 把内核和镜像文件系统加载到内存中,使其可以使用
init进程初始化 运行中的程序称为进程。 加载硬件驱动程序,以及初始化进程,内核把init进程加载到内存中运行。 init进程是由内核直接启动的第一个用户级进程,负责启动系统的用户终端(登录界面),启动各种系统服务和守护进程等等。
1.2 系统初始化进程
init vs systemd 第一个启动的进程 ,后续进程都要依托于此进程启动
CentOS 7 — systemd 并行启动后续进程,速度快
CentOS 6 — init 一个一个启动,速度慢
1.3 systemd单元类型
二、排除启动类故障
2.1 MBR扇区故障
2.1.1 故障原因
病毒、木马的等造成的破坏 不正确的分区操作、磁盘读写误操作等
2.1.2 模拟修复MBR扇区故障
[root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 100G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 99G 0 part ├─centos-root 253:0 0 50G 0 lvm / ├─centos-swap 253:1 0 2G 0 lvm [SWAP] └─centos-home 253:2 0 47G 0 lvm /home sdb 8:16 0 20G 0 disk └─vg01-qa 253:3 0 6G 0 lvm /mnt/qa sdc 8:32 0 20G 0 disk sr0 11:0 1 4.2G 0 rom [root@localhost ~]# fdisk /dev/sdc ##分区 欢迎使用 fdisk (util-linux 2.23.2)。 更改将停留在内存中,直到您决定将更改写入磁盘。 使用写入命令前请三思。 Device does not contain a recognized partition table 使用磁盘标识符 0xa929623c 创建新的 DOS 磁盘标签。 命令(输入 m 获取帮助):n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): Using default response p 分区号 (1-4,默认 1): 起始 扇区 (2048-41943039,默认为 2048): 将使用默认值 2048 Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039):+10G 分区 1 已设置为 Linux 类型,大小设为 10 GiB 命令(输入 m 获取帮助):p 磁盘 /dev/sdc:21.5 GB, 21474836480 字节,41943040 个扇区 Units = 扇区 of 1 * 512 = 512 bytes 扇区大小(逻辑/物理):512 字节 / 512 字节 I/O 大小(最小/最佳):512 字节 / 512 字节 磁盘标签类型:dos 磁盘标识符:0xa929623c 设备 Boot Start End Blocks Id System /dev/sdc1 2048 20973567 10485760 83 Linux 命令(输入 m 获取帮助):w The partition table has been altered! Calling ioctl() to re-read partition table. 正在同步磁盘。 [root@localhost ~]# hexdump -C -n 512 /dev/sda ##用于和故障后的对比 00000000 eb 63 90 10 8e d0 bc 00 b0 b8 00 00 8e d8 8e c0 |.c..............| 00000010 fb be 00 7c bf 00 06 b9 00 02 f3 a4 ea 21 06 00 |...|.........!..| 00000020 00 be be 07 38 04 75 0b 83 c6 10 81 fe fe 07 75 |....8.u........u| 00000030 f3 eb 16 b4 02 b0 01 bb 00 7c b2 80 8a 74 01 8b |.........|...t..| 00000040 4c 02 cd 13 ea 00 7c 00 00 eb fe 00 00 00 00 00 |L.....|.........| 00000050 00 00 00 00 00 00 00 00 00 00 00 80 01 00 00 00 |................| 00000060 00 00 00 00 ff fa 90 90 f6 c2 80 74 05 f6 c2 70 |...........t...p| 00000070 74 02 b2 80 ea 79 7c 00 00 31 c0 8e d8 8e d0 bc |t....y|..1......| 00000080 00 20 fb a0 64 7c 3c ff 74 02 88 c2 52 be 05 7c |. ..d|<.t...R..|| 00000090 b4 41 bb aa 55 cd 13 5a 52 72 3d 81 fb 55 aa 75 |.A..U..ZRr=..U.u| 000000a0 37 83 e1 01 74 32 31 c0 89 44 04 40 88 44 ff 89 |7...t21..D.@.D..| 000000b0 44 02 c7 04 10 00 66 8b 1e 5c 7c 66 89 5c 08 66 |D.....f..\|f.\.f| 000000c0 8b 1e 60 7c 66 89 5c 0c c7 44 06 00 70 b4 42 cd |..`|f.\..D..p.B.| 000000d0 13 72 05 bb 00 70 eb 76 b4 08 cd 13 73 0d 5a 84 |.r...p.v....s.Z.| 000000e0 d2 0f 83 de 00 be 85 7d e9 82 00 66 0f b6 c6 88 |.......}...f....| 000000f0 64 ff 40 66 89 44 04 0f b6 d1 c1 e2 02 88 e8 88 |d.@f.D..........| 00000100 f4 40 89 44 08 0f b6 c2 c0 e8 02 66 89 04 66 a1 |.@.D.......f..f.| 00000110 60 7c 66 09 c0 75 4e 66 a1 5c 7c 66 31 d2 66 f7 |`|f..uNf.\|f1.f.| 00000120 34 88 d1 31 d2 66 f7 74 04 3b 44 08 7d 37 fe c1 |4..1.f.t.;D.}7..| 00000130 88 c5 30 c0 c1 e8 02 08 c1 88 d0 5a 88 c6 bb 00 |..0........Z....| 00000140 70 8e c3 31 db b8 01 02 cd 13 72 1e 8c c3 60 1e |p..1......r...`.| 00000150 b9 00 01 8e db 31 f6 bf 00 80 8e c6 fc f3 a5 1f |.....1..........| 00000160 61 ff 26 5a 7c be 80 7d eb 03 be 8f 7d e8 34 00 |a.&Z|..}....}.4.| 00000170 be 94 7d e8 2e 00 cd 18 eb fe 47 52 55 42 20 00 |..}.......GRUB .| 00000180 47 65 6f 6d 00 48 61 72 64 20 44 69 73 6b 00 52 |Geom.Hard Disk.R| 00000190 65 61 64 00 20 45 72 72 6f 72 0d 0a 00 bb 01 00 |ead. Error......| 000001a0 b4 0e cd 10 ac 3c 00 75 f4 c3 00 00 00 00 00 00 |.....<.u........| 000001b0 00 00 00 00 00 00 00 00 3d 3c 0a 00 00 00 80 20 |........=<..... | 000001c0 21 00 83 aa 28 82 00 08 00 00 00 00 20 00 00 aa |!...(....... ...| 000001d0 29 82 8e fe ff ff 00 08 20 00 00 f8 5f 0c 00 00 |)....... ..._...| 000001e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.| 00000200 [root@localhost ~]# mkdir /test ##新建test文件夹 作为挂载点(备份点) [root@localhost test]# mount /dev/sdc1 /test ##sdc1挂载到/test下 [root@localhost test]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 100G 0 disk ├─sda1 8:1 0 1G 0 part └─sda2 8:2 0 99G 0 part ├─centos-root 253:0 0 50G 0 lvm / ├─centos-swap 253:1 0 2G 0 lvm [SWAP] └─centos-home 253:2 0 47G 0 lvm sdb 8:16 0 20G 0 disk sdc 8:32 0 20G 0 disk └─sdc1 8:33 0 10G 0 part /test sr0 11:0 1 4.2G 0 rom [root@localhost test]# dd if=/dev/sda of=/test/mbr.bak bs=512 count=1 ##将mbr扇区备份到新硬盘的分区 记录了1+0 的读入 记录了1+0 的写出 512字节(512 B)已复制,0.000134482 秒,3.8 MB/秒 [root@localhost test]# ls lost+found mbr.bak [root@localhost test]# dd if=/dev/zero of=/dev/sda bs=512 count=1 ##空字符替换 模拟扇区故障 记录了1+0 的读入 记录了1+0 的写出 512字节(512 B)已复制,0.000125378 秒,4.1 MB/秒 [root@localhost test]# hexdump -C -n 512 /dev/sda ##故障后的mbr扇区 00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| * 00000200 [root@localhost test]# reboot ##重启
虚拟机无法正常启动,选择Troubleshooting
然后在命令行中输入exit退出急救模式并重启
2.2 grub 引导故障
2.2.1 故障原因
-
MBR中的GRUB引导程序遭到破坏
-
grub.cfg 文件丢失、引导配置有误
2.2.2 模拟修复grub配置文件
[root@localhost ~]# cd /boot/grub2 [root@localhost grub2]# rm -rf grub.cfg [root@localhost grub2]# ls device.map fonts grubenv i386-pc locale
reboot重启
丢失grub配置文件,无法登录
重新启动,在读条界面按 Esc 键进入启动菜单,改用光盘启动,引导界面进入急救模式
exit退出临时根reboot重启观察状态
2.3 用户密码
2.3.1 模拟修改root密码
三、linux运行级别
运行级别(runlevel)指的是系统的工作状态,定义了系统启动时运行的服务和进程. init 0-6
可以切换到 对应的运行级别
[root@localhost ~]# systemctl get-default ##获得当前的运行级别 [root@localhost ~]# systemctl isolate xxx.target ##不重启切换运行级别
3.1 运行级别0(halt)
系统关机状态,所有服务已停止,可以安全地关闭电源。
3.2运行级别1(single user mode)
单用户模式,只有root用户可以登录,用于系统修复和维护。
3.3 运行级别2(multi-user mode)
文本模式多用户模式,登录后进入命令行界面, 功能不全的字符界面。
3.4 运行级别3(full multi-user mode)
文本模式多用户模式,登录后进入命令行界面, 完整的字符界面。
3.5 运行级别4(unused)
保留,未分配特定用途,可以根据需要进行自定义。
3.6 运行级别5(graphical multi-user mode)
图形多用户模式,登录后进入 图形界面。
3.7 运行级别6
重启
四、Systemd服务管理–systemctl
4.1 什么是服务程序
为其他机器提供服务的程序,服务程序启动后,会持续监听端口。
使用systemctl 命令管理systemd服务。
4.2 systemctl命令
systemctl是用于管理systemd系统和服务管理器的命令行工具。
命令格式
systemctl [选项] 程序名
start | 开启 |
---|---|
stop | 关闭 |
status | 查看程序状态 |
reload | 重新加载配置文件而不重启 |
restart | 重启 |
enable | 开机自启 |
disable | 开机不自启 |
systemctl enable --now 程序名 | 开机自启并立即启动 |
systemctl start nginx ##启动nginx systemctl status nginx ##查看nginx运行状态 systemctl stop firewalld ##关闭防火墙 setenforce 0 ##永久关闭
编译安装后如何启动服务
[root@localhost system]# vim /usr/lib/systemd/system/sshd.service [Unit] #unit段(单元段) Description=OpenSSH server daemon # 描述信息 Documentation=man:sshd(8) man:sshd_config(5) After=network.target sshd-keygen.service#表示在运行该程序之前运行network.target sshd- keygen.service Wants=sshd-keygen.service #表示sshd.service与sshd-keygen.service之间存在"弱依 赖"关系,即如果"sshd-keygen.service"启动失败或停止运 行,不影响sshd.service继续执行。 [Service] #service段(服务段) Type=notify # 在启动完成后会发送一个通知消息。还需要配合 NotifyAccess 来让 Systemd 接收消息 EnvironmentFile=/etc/sysconfig/sshd # 环境配置文件路径为/etc/sysconfig/sshd ExecStart=/usr/sbin/sshd -D $OPTIONS # 指明启动unit要运行命令或脚本的绝对路径 ExecReload=/bin/kill -HUP $MAINPID #特殊的环境变量 $MAINPID 可用于表示主进程的PID KillMode=process #这里process表示通过只杀主进程来停止服务 Restart=on-failure #当服务单元文件中设置了智能重启,当设定Restart=1 时,则当 次daemon服务意外终止后,会再次自动启动此服务 RestartSec=42s #重启服务前暂停42秒(如果为指定单位将以秒(s)为单位 [Install] #install段(开机启动) WantedBy=multi-user.target #这里表示被multi-user.target所弱依赖,必须先运行混合模式 才能运行本程序 # 其他选项及注释参考以下内容!
[Unit]段的常用选项:
-
Description:描述信息
-
After:定义unit的启动次序,表示当前unit应该晚于哪些unit启动,其功能与Before相反
-
Requires:依赖到的其它units,强依赖,被依赖的units无法激活时,当前unit也无法激活
-
Wants:依赖到的其它units,弱依赖
-
Conflicts:定义units间的冲突关系
[service]段
-
Type:定义影响ExecStart及相关参数的功能的unit进程启动类型
-
simple:默认值,这个daemon主要由ExecStart接的指令串来启动,启动后常驻于内存中
-
forking:由ExecStart启动的程序透过spawns延伸出其他子程序来作为此daemon的主要服务。原生父程序在启动结束后就会终止
-
oneshot:与simple类似,不过这个程序在工作完毕后就结束了,不会常驻在内存中
-
dbus:与simple类似,但这个daemon必须要在取得一个D-Bus的名称后,才会继续运作.因此通常也要同时设定BusNname= 才行
-
notify:在启动完成后会发送一个通知消息。还需要配合 NotifyAccess 来让 Systemd 接收消息
-
idle:与simple类似,要执行这个daemon必须要所有的工作都顺利执行完毕后才会执行。这类的daemon通常是开机到最后才执行即可的服务
Type=notify
-
EnvironmentFile:环境配置文件
EnvironmentFile=/etc/sysconfig/sshd
-
ExecStart:指明启动unit要运行命令或脚本的绝对路径
ExecStart=/usr/sbin/sshd -D $OPTIONS
-
ExecStartPre: ExecStart前运行
-
ExecStartPost: ExecStart后运行
-
ExecStop:指明停止unit要运行的命令或脚本
-
Restart:当设定Restart=1 时,则当次daemon服务意外终止后,会再次自动启动此服务
Restart字段 | 含义 |
---|---|
no(默认值) | 退出后不会重启 |
on-success | 只有正常退出时(退出状态码为0),才会重启 |
on-failure | 非正常退出时(退出状态码非0),包括被信号终止和超时,才会重启 |
on-abnormal | 只有被信号终止和超时,才会重启 |
on-abort | 只有在收到没有捕捉到的信号终止时,才会重启 |
on-watchdog | 超时退出,才会重启 |
always | 不管是什么退出原因,总是重启 |
Restart=on-failure
-
RestartSec: 设置在重启服务( Restart= )前暂停多长时间。 默认值是100毫秒(100ms)。 如果未指定时间单位,那么将视为以秒为单位。 例如设为"20"等价于设为"20s"。
RestartSec=42s
-
PrivateTmp:设定为yes时,会在生成/tmp/systemd-private-UUID-NAME.service-XXXXX/tmp/目录
-
KillMode字段 含义 control-group(默认值) 当前控制组里面的所有子进程,都会被杀掉 process 只杀主进程 mixed 主进程将收到 SIGTERM 信号,子进程收到 SIGKILL 信号 none 没有进程会被杀掉,只是执行服务的 stop 命令
[install]段:选择启动的运行级别
Install段的常用选项:
Alias:别名,可使用systemctl command Alias.service
RequiredBy:被哪些units所依赖,强依赖
WantedBy:被哪些units所依赖,弱依赖
WantedBy=multi-user.target
Also:安装本服务的时候还要安装别的相关服务
在centos6及以前版本编译安装后如何启动服务
chkconfig
chkconfig工具:
格式:
chkconfig --list [服务名称]
chkconfig --add 服务名称
chkconfig --level 级别列表服务名on/off
chkconfig --add httpd
chkconfig --level 35 httpd on
cd /opt wget https://nginx.org/download/nginx-1.25.4.tar.gz #下载nginx包 yum -y install gcc pcre-devel openssl-devel zlib-devel openssl-devel#依赖包,编译软件 tar zxvf nginx-1.25.4.tar.gz -C /opt #解压nginx包 cd nginx-1.25.4 #进入nginx文件夹 ./configure --prefix=/usr/local/nginx #指定安装路径 make && make install #编译安装 ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #这步可以不做,做软链接,让系统可以 直接使用 cat /usr/local/nginx/logs/nginx.pid #查看nginx进程号,便于停止 vim /etc/init.d/nginx #写一个脚本 #!/bin/bash #chkconfig: - 99 20 #description:Nginx Service Control Script PROG="/usr/local/nginx/sbin/nginx" PIDF="/usr/local/nginx/logs/nginx.pid" case "$1" in start) $PROG ;; stop) kill -s QUIT $(cat $PIDF) ;; restart) $0 stop $0 start ;; reload) kill -s HUP $(cat $PIDF) ;; *) echo "Usage: $0 {start|stop|restart|reload}" exit 1 esac exit 0 :wq #保存退出 chmod +x /etc/init.d/nginx #给脚本加上权限 ss -ntap |grep nginx #查看服务有没有启动 chkconfig --add nginx #将服务加入让chkconfig管理 chkconfig --list nginx #查看服务 chkconfig --level 35 nginx on #开启3和5自动开启
实验
cd /opt wget https://nginx.org/download/nginx-1.25.4.tar.gz #下载nginx包 yum -y install gcc pcre-devel openssl-devel zlib-devel openssl-devel#依赖包,编译软件 tar zxvf nginx-1.25.4.tar.gz #解压nginx包 cd nginx-1.25.4 #进入nginx文件夹 ./configure --prefix=/usr/local/nginx #指定安装路径 make && make install #编译安装 ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #做软链接 vim /lib/systemd/system/nginx.service [Unit] Description=nginx - high performance web server [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/bin/kill -s TERM $MAINPID [Install] WantedBy=multi-user.target wq #保存退出 systemctl daemon-reload #重新加载配置 systemctl start nginx #启动nginx服务 systemctl status nginx #查看nginx服务状态 systemctl stop nginx #关闭nginx服务 systemctl status nginx #查看nginx服务状态