Contents

1.阐述Linux操作系统启动流程(grub2/systemd)

Linux操作系统启动引导的过程linux重置系统盘命令,系统的控制权移交大致分为如下4步:

BIOSPOST,及BIOS加电自检。GRUB装载辨识并装载内核。内核辨识根文件系统并启动systemd。systemd打算系统资源,最终完成系统启动,系统就绪。

在第一步中,从按下电源键显卡BIOS开始加电自检(POST,即Power-On-Self-Test)并完成硬件初始化,假如在加电自检阶段失败,则未能启动系统,启动流程不会往前进行;当加电自检和硬件初始化操作完成,BIOS才会读取可引导设备的第一个磁道中的主引导记录(MBR),之后主引导记录中的引导装载器(GRUB/GRUB2)接管此后的启动流程。

在GRUB通过在其中记录的保存有/boot分区文件系统驱动(例如支持EXT系列文件系统、FAT文件系统、NTFS文件系统),至此,就可以辨识/boot分区了。接出来通过定位/boot分区中的vmlinuz内核文件(自解压文件),将其装载到显存中,同时将此后的启动控制权移交给内核。在GRUB2原先的版本中,GRUB控制阶段可以被细分为3个过程(stage1,stage1.5,stage2)。GRUB2的官方说明中其实并没有细分的3个阶段的说明,然而实际依然是依循的这个过程。

stage1阶段

这个阶段GRUB会依照MBR(第一个磁道的前446个字节)中记录的内容定位到stage1.5阶段的分区位置。而stage1.5阶段坐落MBR以后,到第一个分区之前的这块区域中。MBR中的内容记录的就是“/usr/lib/grub/i386-pc/boot.img”的内容,但实际查看二者内容并不完全一致(在446字节以后是64字节的分区表,共记录4个分区记录,每位分区记录占16个字节,最后2个字节为MBR的固定标示,为55AA)。

关于上述文件内容与MBR的内容对比,如下所示:

[root@c7u6-ha1 ~]# ls -lh /usr/lib/grub/i386-pc/boot.img
-rw-r--r--. 1 root root 512 Nov  9  2018 /usr/lib/grub/i386-pc/boot.img
[root@c7u6-ha1 ~]# hexdump -Cv -n 512 /usr/lib/grub/i386-pc/boot.img
00000000  eb 63 90 00 00 00 00 00  00 00 00 00 00 00 00 00  |.c..............|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000040  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000050  00 00 00 00 00 00 00 00  00 00 00 80 01 00 00 00  |................|
00000060  00 00 00 00 ff fa eb 05  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  00 00 00 00 00 00 24 12  |..............$.|
000001c0  0f 09 00 52 be bd 7d 31  c0 cd 13 46 8a 0c 84 c9  |...R..}1...F....|
000001d0  75 0f be da 7d e8 cc ff  eb 96 46 6c 6f 70 70 79  |u...}.....Floppy|
000001e0  00 bb 00 70 8e c3 31 db  b8 01 02 b5 00 b6 00 cd  |...p..1.........|
000001f0  13 72 d4 b6 01 b5 4f e9  f1 fe 00 00 00 00 55 aa  |.r....O.......U.|
00000200
[root@c7u6-ha1 ~]#
接下来查看磁盘的第一个扇区的内容,即主引导记录MBR的内容。具体如下所示:
[root@c7u6-ha1 ~]# hexdump -Cv -n 512 /dev/vda
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  e9 5f 09 00 00 00 80 20  |........._..... |
000001c0  21 00 83 65 24 41 00 08  00 00 00 00 10 00 00 65  |!..e$A.........e|
000001d0  25 41 82 ef 2c c3 00 08  10 00 00 00 20 00 00 ef  |%A..,....... ...|
000001e0  2d c3 8e fe ff ff 00 08  30 00 00 f8 4f 02 00 00  |-.......0...O...|
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200

stage1.5

这个阶段中储存的是core.img这个文件,其中包含了通用文件系统的驱动程序,例如EXT系列文件系统、FAT文件系统、NTFS文件系统等。通过将这个阶段的core.img装载到显存中,就可以辨识到/boot分区的文件系统了。因为core.img中一般不包含lvm的软件以及相应的驱动,所以/boot分区不能安装在lvm逻辑卷上。另外,也不能将/boot分区低格为XFS文件系统。这个阶段执行完成以后,就可以辨识到/boot分区了。stage2

这个阶段中,GRUB就可以定位并就能辨识/boot分区了,此时就可以将/boot分区中的内核文件vmlinuz装在到显存中,同时GRUB会将启动流程控制权移交给内核。因为此时,假若将根文件系统装在lvm逻辑卷上,低格为EXT之外的其他文件系统,这么内核在这个阶段还不能辨识根分区的文件系统。此时就须要利用/boot分区中的initramfs显存虚拟根文件系统中保存的对应驱动以及程序结合系统储存设备映射文件System.map辨识根分区。

至此,内核就可以装载systemd程序,并将此后的启动控制权移交给systemd程序。

上述过程概括为如右图:

systemd是现今主流发行版的通用资源管理方法,systemd是所有其他进程的母进程,不仅进程管理之外,还支持好多其他资源管理,例如挂载文件系统、启动和管理系统服务等等。systemd接管启动流程以后,首先会通过定义/etc/fstab挂在文件系统,此时就可以访问根目录中的/etc/下的配置文件了,包括其自身的配置文件/etc/systemd/system/default.target,这个文件决定了系统的默认运行级别,该文件只是一个链接文件,其链接到系统启动以后的目标target上。

具体如下所示:

[root@c7u6-ha1 ~]# ll /etc/systemd/system/default.target
lrwxrwxrwx. 1 root root 37 Feb 18  2020 /etc/systemd/system/default.target -> /lib/systemd/system/multi-user.target
[root@c7u6-ha1 ~]# cat /etc/systemd/system/default.target
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it 
#  under the terms of the GNU Lesser General Public License as published by 
#  the Free Software Foundation; either version 2.1 of the License, or 
#  (at your option) any later version.
[Unit]
Description=Multi-User System
Documentation=man:systemd.special(7)
Requires=basic.target
Conflicts=rescue.service rescue.target
After=basic.target rescue.service rescue.target
AllowIsolate=yes
[root@c7u6-ha1 ~]# 

上述即为default.target的内容。

multi-user.target对应SystemVinit中的运行级别为runleve3,而graphical.target则对应于runleve5。

关于各个运行级别,概括总结如下:

SystemVRunlevelsystemdtargetsystemtargetaliasDescription

halt.target

Haltsthesystemwithoutpoweringitdown.

poweroff.target

runlevel0.target

Haltsthesystemandturnsthepoweroff.

emergency.target

Singleusermode.Noservicesarerunning;filesystemsarenotmounted.Thisisthemostbasiclevelofoperationwithonlyanemergencyshellrunningonthemainconsolefortheusertointeractwiththesystem.

rescue.target

runlevel1.target

Abasesystemincludingmountingthefilesystemswithonlythemostbasicservicesrunningandarescueshellonthemainconsole.

runlevel2.target

Multiuser,withoutNFSbutallothernon-GUIservicesrunning.

multi-user.target

runlevel3.target

Allservicesrunningbutcommandlineinterface(CLI)only.

runlevel4.target

Unused.

graphical.target

runlevel5.target

multi-userwithaGUI.

reboot.target

runlevel6.target

Reboot

default.target

Thistargetisalwaysaliasedwithasymboliclinktoeithermulti-user.targetorgraphical.target.systemdalwaysusesthedefault.targettostartthesystem.Thedefault.targetshouldneverbealiasedtohalt.target,poweroff.target,orreboot.target.

systemd接管启动流程以后的操作如右图所示:

上图中的sysinit.target和basic.target可以看作是启动过程中的检测点,即使systemd支持资源的并行启动,并且总会有一些服务和资源对象须要在其他资源对象之前启动。当sysinit.target执行完成以后,都会执行basic.target这个资源,当这种先决资源启动完成以后,最后就是启动运行级别资源对象开启对应的用户插口了。

当systemd将系统资源打算完成,操作系统就完成了启动的整个流程。此时用户就可以登入系统进行操作了。

2.systemctl管理命令

后面列举了systemd的几个运行级别,用于指定不同的用户操作环境。而systemd作为SystemVinit脚本和Upstart的代替者,其存在就是用于管理系统资源和服务。其提供了丰富的特点,例如系统启动阶段的服务并行启动、按需激活必要的守护进程以及基于依赖关系的服务控制逻辑等。

2.1.systemd的unit介绍

systemd中引入了单元(unit)的概念,这种单元涉及到系统服务、监听套接字以及其他init系统相关的对象。要查看系统上systemd支持的单元都有什么,可以执行命令systemctl-thelp获得。在CentOS7.6上有13个单元;而在CentOS8.2上则降低到了11个。具体如下所示:

[root@LiuXianQiE ~]# cat /etc/redhat-release 
CentOS Linux release 8.3.2011
[root@LiuXianQiE ~]# systemctl -t help
Available unit types:
service
socket
target
device
mount
automount
swap
timer
path
slice
scope
[root@LiuXianQiE ~]# systemctl -t help | egrep -v unit | wc -l
11
[root@LiuXianQiE ~]# 
[root@c7u6-ha1 ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[root@c7u6-ha1 ~]# systemctl -t help | egrep -v unit
service
socket
busname
target
snapshot
device
mount
automount
swap
timer
path
slice
scope
[root@c7u6-ha1 ~]# systemctl -t help | egrep -v unit | wc -l
13
[root@c7u6-ha1 ~]# 

从上述输出中可以看出,CentOS8.x中比CentOS7.x中少了snapshot以及busname这两个单元。

关于单元,一些简单的介绍总结如下表所示:

UnitTypeFileExtensionDescription

Serviceunit

.service

Asystemservice.

Targetunit

.target

Agroupofsystemdunits.

Automountunit

.automount

linux重置磁盘_linux重置系统盘命令_linux系统重置

Afilesystemautomountpoint.

Deviceunit

.device

Adevicefilerecognizedbythekernel.

Mountunit

.mount

Afilesystemmountpoint.

Pathunit

.path

Afileordirectoryinafilesystem.

Scopeunit

.scope

Anexternallycreatedprocess.

Sliceunit

.slice

Agroupofhierarchicallyorganizedunitsthatmanagesystemprocesses.

Snapshotunit

.snapshot

Asavedstateofthesystemdmanager

Socketunit

.socket

Aninter-processcommunicationsocket.

Swapunit

.swap

Aswapdeviceoraswapfile

Timerunit

.timer

Asystemdtimer.

上述是一些类型的单元以及其扩充名。那些文件一般储存在一下三个地方,具体如下表所示:

DirectoryDescription

/usr/lib/systemd/system/

SystemdunitfilesdistributedwithinstalledRPMpackages.

/run/systemd/system/

Systemdunitfilescreatedatruntime.Thedirectorytakesprecedenceoverthedirectorywithinstalledserviceunitfiles.

/etc/systemd/system

Systemdunitfilescreatedbysystemctlenableaswellasunitfilesaddedforextendingaservice.Thisdirectorytakesprecedenceoverthedirectroywithruntimeunitfiles.

上述三个路径中,一般将用户定义的单元(unit)放置在第一个路径中,即/usr/lib/systemd/system这个目录中。

而systemd自身的配置文件是/etc/systemd/system.conf,可以通过更改这个配置文件中的内容,来调整systemd的行为。例如默认的超时上限是90秒,通过更改这个配置文件中的数组DefaultTimeoutStartSec可以改变systemd的超时时间。例如改为DefaultTimeoutStartSec=120。就将超时值更改为了120秒。

2.2.systemd的主要特点

systemd提供的主要特点概括如下:

2.3.systemctl命令管理系统服务

因为systemd是向上兼容SysVinit的服务管理脚本的,所以在CentOS7.x中依然可以使用service命令以及chkconfig这两个命令对服务进行管理。而systemd提供的服务管理命令则是systemctl。这个命令可以对.service类型的unit执行start,stop,restart,enable,disable等操作。关于service与systemctl命令的对比,详见下表:

servicesystemctlDescription

servicenamestart

systemctlstartname.service

Startsaservice.

servicenamestop

systemctlstopname.service

Stopsaservice.

servicenamerestart

systemctlrestartname.service

Restartsaservice.

servicenamecondrestart

systemctltry-restartname.service

Restartsaserviceonlyifitisrunning.

servicenamereload

systemctlreloadname.service

Reloadsconfiguration.

servicenamestatus

systemctlstatusname.service

systemctlis-activename.service

Checksifaserviceisrunning.

service--status-all

systemctllist-units--typeservice--all

Displaysthestatusofallservices.

上述是service命令与systemctl命令的对比。

chkconfig命令与systemctl命令的对比结果如下表所示:

linux重置磁盘_linux重置系统盘命令_linux系统重置

chkconfigsystemctlDescription

chkconfignameon

systemctlenablename.service

Enablesaservice.

chkconfignameoff

systemctldisablename.service

Disablesaservice.

chkconfig--listname

systemctlstatusname.service

systemctlis-enabledname.service

Checksifaserviceisenabled.

chkconfig--list

systemctllist-unit-files--typeservice

Listsallservicesandchecksiftheyareenabled.

chkconfig--list

systemctllist-dependencies--after

Listsservicesthatareorderedtostartbeforethespecifiedunit.

chkconfig--list

systemctllist-dependencies--before

Listsservicesthatareorderedtostartafterthespecifiedunit.

在使用systemctl命令管理服务的时侯,通常会写上.service这个后缀,然而因为默认的服务都是以.service结尾,所以这个后缀是可以省略的。所以执行systemctlstartnfs-server.service与systemctlstartnfs-server的疗效是相同的。

另外,可以查看某个服务的详尽信息,此时须要执行命令systemctlshowname.service即可。假如要查看某一项信息,可以使用-pname进行指定。例如systemctlshownfs-server.service-pNames就可以查看nfs-server.service这个服务的Names这个信息,即这个服务有几个名子,是否有别称的存在,都可以被列下来。

要列举当前系统下所有早已装载的服务,可以执行命令systemctllist-units--typeservice。对于每一个.service类型的unit,这个命令会显示其完整的服务名称(UNIT)、该服务是否被装载(LOAD)、以及其中级(ACTIVE)和低级(SUB)单元文件的激活状态,以及该服务的短描述信息(DESCRIPTION)。

默认情况下,systemctllist-units这个命令只显示活动的单元linux重置系统盘命令,假如想要查看所有的单元,而不管其是否处于活动状态,须要加上-a/--all这个选项。此时的命令为systemctllist-units--typeservice--all。

假如要查看系统上早已被使能的服务单元,可以执行命令systemctllist-unit-files--typeservice进行查看。对于每一个服务单元,该命令会显示服务单元的完整名称(UNITFILE)、该服务单元是否处于使能状态(STATE)。

要查看服务的状态,可以执行命令systemctlstatusname.service进行查看。该命令的输出数组包括如下:

FieldDescription

Loaded

Informationwhethertheserviceunithasbeenloaded,theabsolutepathtotheunitfile,andanotewhethertheunitisenabled.

Active

Informationwhethertheserviceunitisrunningfollowedbyatimestamp.

MainPID

ThePIDofthecorrespondingsystemservicefollowedbyitsname.

Status

Additionalinformationaboutthecorrespondingsystemservice.

Process

Additionalinformationaboutrelatedprocesses.

CGroup

AdditionalinformationaboutrelatedControlGroups(cgroups).

假如要验证服务单元是否处于活动状态,或则运行状态,可以执行命令systemctlis-activename.service进行查看。

假如要查看某个服务单元是否早已被使能,可以执行命令systemctlis-enabledname.service进行查看。

假如相关的服务单元处于运行状态或则是被使能状态,这么这两条命令的退出状态码都是0。

要查看某个服务单元的依赖关系,例如查看该服务是在什么服务以后启动,可以执行命令systemctllist-dependencies--aftername.service进行查看,换句话说linux游戏,就是该命令会递归列举After=这个依赖关系中列举的单元(即该服务所依赖的服务);假如要查看该服务是在什么服务之前启动,可以执行命令systemctllist-dependencies--beforename.service进行查看,换句话说,这个命令会递归列举Before=中列举的单元(也就是依赖于该服务的单元有什么)。

要启动一个服务单元,可以执行命令systemctlstartname.service进行查看。

要停止一个服务单元,可以执行命令systemctlstopname.service进行查看。

要重启一个服务单元,可以执行命令systemctlrestartname.service进行查看,该命令会在当前会话中停止指定的服务单元,之后立刻启动该服务单元。重要的一点是,倘若指定的服务单元此时并没有处于运行状态,这么这个重启的操作也会启动它。若果只想对早已运行的服务单元执行重启操作,而未运行的服务单元则不执行重启操作,这么可以执行命令systemctltry-restartname.service这个命令来完成。另外,某系服务容许在不中断服务执行的前提下重新装在配置文件,对于这种服务,可以执行命令systemctlreloadname.service进行配置文件的重新装载。入果指定的服务单元并不支持这个操作,这么这个命令执行的时侯会被忽视掉。若果不确定服务是否支持reload操作,这么可以执行命令systemctlreload-or-restartname.service进行重新装载配置文件或则重启的操作;若果只希望对早已运行的服务执行重新装载或则重启的操作,这么须要执行systemctlreload-or-try-restartname.service命令进行重新装载配置文件或则对早已运行的服务单元进行重启的操作。

要使能(即设置开机手动运行)服务单元,须要执行命令systemctlenablename.service来完成,这个命令会在/etc/systemd/system目录中创建对应服务单元的符号链接文件。假如想要相关服务的符号链接文件被重新创建,此时可以执行命令systemctlreenablename.service来完成。这个命令会首先禁用指定的服务(即删掉相关服务的符号链接,之后再重新创建该链接)。假如对于未运行的服务单元,在使能其运行的同时,让其立刻运行,此时可以执行命令systemctlenable--nowname.service,表示让该服务开机手动运行,但是现今立刻运行。

要禁用指定的服务(正式其设置为开机不手动运行),此时可以执行命令systemctldisablename.service。假如想要严禁某个服务被自动启动或则被其他服务启动,此时可以将其掩藏上去,执行命令systemctlmaskname.service即可,这个命令的实际操作就是/etc/systemd/system/name.service创建为/dev/null的软链接。所以要容许指定的服务可以被自动启动或则被其他服务启动,这么就须要将掩藏拆掉,即执行命令systemctlunmaskname.service即可。

2.4.systemctl与target

在RHEL/CentOS7.x之前的发行版中,使用SysVinit以及Upstart实现预定义的运行级别来抒发特定的操作模式,其RHEL/CentOS5.x使用的是SysVinit,在RHEL/CentOS6.x中使用的则是Upstart。在RHEL/CentOS7.x以及8.x中则是使用的systemd的target来取代此前的三者,实现类似运行级别来抒发特定的操作模式的目的。

systemd的target表现为target单元,即以.target为结尾的文件,其惟一的目的就是将其他systemd的单元(unit)根据依赖关系链进行分组。例如graphical.target这个单元用于启动一个图形化的交互环境,其会启动例如GNOMEDisplayManager(gdm.service)这个系统服务、AccountService(accounts-daemon.service)这个服务,同时也会激活multi-user.target这个单元。类似的,multi-user.target这个单元会启动一些其他必要的系统服务,例如NetworkManager(NetworkManager.service)或则D-Bus(dbus.service),同时激活basic.target这个单元。

关于各类运行级别,参见本篇博客的第一部份的内容。

要查看和切换运行级别,新老命令的对例如下表所示:

OldCommandNewCommandDescription

runlevel

systemctllist-units--typetarget

Listscurrentlyloadedtargetunits.

telinit

runlevel

systemctlisolatename.target

Changesthecurrenttarget.

要查看当前系统的默认target,执行命令systemctlget-default即可。要查看当前系统上早已装载的所有的target单元,执行命令systemctllist-units--typetarget即可。对于每一个target单元,这个命令就会显示完整的名子(UNIT)、该单元是否被装载(LOAD)、高级(ACTIVE)单元和低级(SUB)单元的激活状态以及该单元的简略描述(DESCRIPTION)。

默认情况下,systemctllist-units--targettarget只会列举活动的单元,假如想要列举所有的target单元,而不管其是否处于活动状态,这么此时就须要在命令行中加上选项--all/-a。此时的命令为systemctllist-units--typetarget--all即可。

假如要给当前系统设置默认的target,即设置默认运行级别,此时就须要执行命令systemctlset-defaultname.target。这个命令会将链接文件/etc/systemd/system/default.target指向/usr/lib/systemd/system/name.target,这么上次系统启动的时侯,默认的运行级别就弄成了name.target所指定的运行级别。

假如要在当前会话中临时更改target,这么就可以执行命令systemctlisolatename.target即可。例如将当前的target更改为解救模式,及rescue.target,执行命令systemctlisolaterescue.target即可,更简单的写法是systemctlrescue,这两种方法的疗效相同。在切换到解救模式以后,系统会尝试挂在所有的本地文件系统,而且会启动一些重要的系统服务,而且并不会激活网卡插口,也不允许多用户登陆,此时只容许root用户登入。在切换为解救模式以后,须要正确输入root用户的密码以后才会步入到解救模式。另外,在切换到解救模式的时侯,系统默认会给所有当前正处于登入状态的用户发送广播消息,假如要禁用这个广播消息,此时可以加上选项--no-wall即可,此时的命令为systemctl--no-wallrescue。

假如要切换为紧急模式,即emergency.target单元,此时须要执行命令systemctlisolateemergency.target,或则更简略的方式systemctlemergency,这两种命令方式疗效是一样的,都是切换到紧急模式。在紧急模式中,操作系统会将根分区挂载为只读方式,而且不会尝试挂载其他文件本地文件系统,同样不会激活网卡插口,而且只会启动一些必要的服务,同样不允许多用户登入。在切换为紧急模式以后,须要正确输入root用户密码以后才会步入到紧急模式。另外,默认情况下,在切换为紧急模式的时侯系统会给当前正处于登入状态的用户发送广播消息。假如要禁用广播消息,可以加上选项--no-wall,此时的命令为systemctlisolateemergency--no-wall或则systemctl--no-wallemergency。

2.5.systemctl命令实现系统关掉、挂起和冬眠

linux重置磁盘_linux系统重置_linux重置系统盘命令

RHEL/CentOS7.x开始,由systemd提供系统的电源管理命令。相关的电源管理命令如下表所示:

OldCommandNewCommandDescription

halt

systemctlhalt

Haltsthesystem.

poweroff

systemctlpoweroff

Powersoffthesystem.

reboot

systemctlreboot

Restartsthesystem.

pm-suspend

systemctlsuspend

Suspendsthesystem.

pm-hibernate

systemctlhibernate

Hibernatesthesystem.

pm-suspend-hybrid

systemctlhybrid-sleep

Hibernatesandsuspendsthesystem.

假如要关掉运行的系统,可以使用传统的shutdown命令,例如shutdown-hnow即可立刻关掉系统。而使用systemctl命令进行系统关掉的操作,就须要执行命令systemctlpoweroff即可,这个命令相当于直接长按主机的电源键进行死机。假如要通过关掉并停止系统,而不是通过类似按电源键死机的的疗效,此时可以执行命令systemctlhalt即可。默认情况下,这两个命令就会给登陆用户发送广播消息,假若不想用户收到广播消息,可以在命令行中降低选项--no-wall即可。此时的命令为systemctl--no-wallpoweroff或则systemctl--no-wallhalt,这二者都可以。

倘若不是立刻关掉,实现延时关掉的疗效,此时就须要将上述的命令改为选项模式,例如指定在几点几分关掉系统,此时可以执行命令systemctl--poweroffHH:MM即可,注意,此时poweroff这个命令已然弄成长选项格式了,不要省略掉两个中文全角连字符。假如要指定在几分钟或则几个小时以后关掉系统,此时可以执行命令systemctl--halt+m,这条命令中的halt同样变为选项模式,不能省略两个中文全角连字符,同时+m表示m分钟后关掉系统。若果将+m替换为now,则等效于+0,表示立刻关掉系统。对于上述仍未执行的挂起状态的死机命令,可以执行shutdown-c进行取消。

假如要重启系统,可以执行命令systemctlreboot即可,默认该命令会给所有早已登入的用户发送广播消息,假如要禁用这个广播消息,此时可以加上命令行选项--no-wall即可,此时的命令为systemctl--no-wallreboot。

假如要挂起系统,可以执行命令systemctlsuspend即可。这个命令会将系统的状态信息保存到显存中,当旁边切换回系统的时侯,系统会直接从显存中回复此前保存的状态,而无需重新启动系统。因为系统的状态信息是保存在系统的显存中,而不是保存在c盘中,所以恢复速率很快,但与此同时,也会造成当系统遭到忽然的断电时导致保存的状态遗失的情况。

倘若要将系统冬眠,此时就须要执行命令systemctlhibernate。这个命令将系统状态保存到c盘中,之后关掉系统。当你重新切回到系统的时侯,操作系统会从硬碟中读取保存的状态完成恢复,而无需重新启动系统。因为系统状态信息是保存在硬碟中,而不是保存在显存中,所以恢复的时侯速率会相对较慢。

假如要冬眠而且挂起系统,可以执行命令systemctlhybrid-sleep命令即可。

2.6.控制远程主机上的systemd

不仅可以控制本地系统的systemd以及服务管理之外,systemctl命令还可以通过ssh合同与远程主机的systemd进行交互。须要确保远程主机上的sshd服务处于开启状态,此时就可以通过systemctl-H/--host方式的命令对远程主机进行操作了。具体命令为systemctl--hostremote_user@remote_hostcommand即可。具体如下边的示例所示:

[root@LiuXianQiE ~]# systemctl -H root@c7u6-ha1 is-active sshd.service
active
[root@LiuXianQiE ~]# systemctl -H root@c7u6-ha1 status sshd.service
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2021-06-07 11:12:56 CST; 13h ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 2985
   CGroup: /system.slice/sshd.service
           └─1715308 /usr/sbin/sshd -D -oCiphers=aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes256-ctr,aes256-cbc,aes128-gcm@openssh.c>2985 /usr/libexec/gsd-power
lines 1-9/9 (END)

上述在远程主机上分别执行了两条命令,第一条命令查看远程主机上的sshd.service服务是否处于激活状态;第二条命令是查看远程主机上的sshd.service服务的状态信息。为了与上述的输出结果做对比,下边在本机上再查看一下sshd.service的状态信息,具体如下所示:

[root@LiuXianQiE ~]# systemctl status sshd.service
. sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2021-06-07 11:14:26 CST; 13h ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 1715308 (sshd)
    Tasks: 1 (limit: 409082)
   Memory: 1.2M
   CGroup: /system.slice/sshd.service
           └─1715308 /usr/sbin/sshd -D -oCiphers=aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes256-ctr,aes256-cbc,aes128-gcm@openssh.c>
Jun 07 11:14:26 LiuXianQiE systemd[1]: Starting OpenSSH server daemon...
Jun 07 11:14:26 LiuXianQiE sshd[1715308]: Server listening on 0.0.0.0 port 22.
Jun 07 11:14:26 LiuXianQiE sshd[1715308]: Server listening on :: port 22.
Jun 07 11:14:26 LiuXianQiE systemd[1]: Started OpenSSH server daemon.
lines 1-15/15 (END)

2.7.创建以及更改systemd的unit文件

因为systemd的行为是由对应的单元(unit)文件定义的,而单元文件中包含了单元描述以及行为定义的指令,为了更出众的适应systemd的管理方法,管理员通常须要自动编辑以及创建单元文件。其中/etc/systemd/system这个目录用于保存系统管理员创建以及自定义的单元文件,并且实际上,更多的是直接在/usr/lib/systemd/system这个目录中进行单元文件的创建以及编辑自定义操作。

对于单元文件,可以通过目录增补额外的配置文件,例如要给sshd.service增补自定义配置选项,就须要创建sshd.service.d/custom.conf这个配置文件并在其中插入额外的配置指令。除此之外,还可以创建sshd.service.wants/目录以及sshd.service.requires/目录,这两个目录中包含了sshd服务所依赖服务的符号链接文件。那些符号链接文件是根据sshd服务的单元文件sshd.service中的[Install]选项手动创建下来的,或则是在运行时基于[Unit]选项手动创建下来的。其实,也可以自动创建这种目录和符号链接文件。

2.7.1.理解单元文件(unitfile)的结构

单元文件典型的结构由三部份组成:

Table1:Important[Unit]SectionOptions

Option[a]Description

Description

Ameaningfuldescriptionoftheunit.Thistextisdisplayedforexampleintheoutputofthesystemctlstatuscommand.

Documentation

ProvidesalistofURIsreferencingdocumentationfortheunit.

After[b]

Definestheorderinwhichunitsarestarted.TheunitstartsonlyaftertheunitsspecifiedinAfterareactive.UnlikeRequires,Afterdoesn’texplicitlyactivatethespecifiedunits.TheBeforeoptionhastheoppositefunctionalitytoAfter.

Requires

Configuresthedependenciesonotherunits.TheunitslistedinRequiresareactivatedtogetherwiththeunit.Ifanyoftherequiredunitsfailtostart,theunitisnotactivated.

Wants

ConfiguresweakerdependenciesthanRequires.Ifanyofthelistedunitsdoesnotstartsuccessfully,ithasnoimpactontheunitactivation.Thisistherecommendedwaytoestablishcustomunitdependencies.

Confilicts

Configuresnegativedependencies,anoppositetoRequires.

[a]:关于[Unit]部份更详细的配置选项,参见**systemd.unit(5)**帮助指南,即man5systemd.unit。

[b]:多数情况下,只须要在[Unit]部份通过After以及Before就足以指定次序依赖关系。假如与此同时也设置了Wants或则Requires指定需求的单元,这么依然须要通过After以及Before指定次序依赖关系。这是由于次序以来关系和需求的单元之间彼此是互相独立的。

Table2:Important[Service]SectionOptions

Option[a]Description

Type

ConfigurestheunitprocessstartuptypethataffectsthefunctionalityofExecStartandrelatedoptions.Oneof:

1.simple:Thedefaultvalue.TheprocessstartedwithExecStartisthemainprocessoftheservice.

2.forking:TheprocessstartedwithExecStartspawnsachildprocessthatbecomesthemainprocessoftheservice.Theparentprocessexitswhenthestartupiscomplete.

3.oneshot:Thistypeissimilartosimple,buttheprocessexitsbeforestartingconsequentunits.

4.dbus:Thistypeissimilartosimple,butconsequentunitsarestartedonlyafterthemainprocessgainsaD-Busname.

5.notify:Thistypeissimilartosimple,butconsequentunitsarestartedonlyafteranotificationmessageissentviathesd_notify()function.

6.idle:Similartosimple,theacutalexecutionoftheservicebinaryisdelayeduntilalljobsarefinished,whichavoidsmixingthestatusoutputwithshelloutputofservices.

ExecStart

Specifiescommandsorscriptstobeexecutedwhentheunitisstarted.ExecStartPreandExecStartPostspecifycustomcommandstobeexecutedbeforeandafterExecStart.Type=oneshotenablesspecifyingmultiplecustomcommandsthatarethenexecutedsequentially.

ExecStop

Specifiescommandsorscriptstobeexecutedwhentheunitisstopped.

ExecReload

Specifiescommandsorscriptstobeexecutedwhentheunitisreloaded.

Restart

Withthisoptionenabled,theserviceisrestartedafteritsprocessexits,withtheexceptionofacleanstopbythesystemctlcommand.

RemainAfterExit

IfsettoTrue,theserviceisconsideredactiveevenwhenallitsprocessesexited.DefaultvalueisFalse.ThisoptionisespeciallyusefulifType=oneshotisconfigured.

[a]:要查看关于[Service]部份的完整选项列表,参见systemd.service(5)的帮助指南,即执行命令man5systemd.service即可。

Table3:Important[Install]Sectionoptions

Option[a]Description

Alias

Providesaspace-separatedlistofadditionalnamesfortheunit.Mostsystemctlcommands,excludingsystemctlenable,canusealiasinsteadoftheactualunitname.

RequiredBy

Alistofunitsthatdependontheunit.Whenthisunitisenabled,theunitlistedinRequiredBygainaRequiredependencyontheunit.

WantedBy

Alistofunitsthatweaklydependontheunit.Whenthisunitisenabled,theunitslistedinWantedBygainaWantdependencyontheunit.

Also

Specifiesalistofunitstobeinstalledoruninstalledalongwiththeunit.

DefaultInstance

Limitedtoinstantiatedunits,thisoptionspecifiesthedefaultinstanceforwhichtheunitisenabled.

[a]:关于[Install]更详尽的选项列表,参见systemd.unit(5)的帮助指南,即执行命令man5systemd.unit即可。

关于这三部份的内容组织,以sshd.service单元文件的内容为例,具体如右图所示:

上图的[Unit]部份指定了sshd服务的基本描述信息,以及这个服务的帮助文档获取方法;同时指定了该服务的以来关系,即sshd服务依赖network.target和sshd-keygen.target这两个单元,须要在这两个单元就绪以后才会启动sshd服务;也通过Wants显式申明了该服务须要ssh-keygen.target这个单元。

在上图的[Service]部份,指定了服务的进程启动类型受ExecStart选项的影响形式,notify的含意是该服务的进程被ExecStart启动以后以主进程的方式存在,此后的单元只是在通过sd_notify()函数发送了通知消息以后才能启动。ExecStart指定了服务的启动命令,ExecReload指定了重新装载配置文件时须要执行的操作(参见man5systemd.service的输出);KillMode指定了进程应当以如何的形式被关掉,process表示只有主进程会被杀掉(关于KillMode的帮助信息,参见man5systemd.kill的输出)。Restart表示在哪些情况下须要重启该服务,on-failure的涵义是当服务的进程返回非零退出状态码的时侯,才能重启该服务的主进程(例如当服务的重新装载配置文件操作超时或则触发了看门狗超时的时侯,该选项的更详尽的信息,参见mansystemd.service的输出);RestartSec表示重启服务之前的睡眠时间,以秒为单位,默认100秒。另外,还通过选项EnvironmentFile定义了两个环境变量文件,该选项指定的值应当是文件的绝对路径,或则键值表达式,加上前缀英语全角连字符”-“的时侯,表示假如指定的文件不存在,就不再尝试读取该文件,同时不会记录错误或则警告信息(关于该选项的更详尽的信息,参见man5systemd.exec的输出)。

上图中[Install]部份指定了选项WantedBy,表示在执行systemctlenable命令将该服务设置为开机手动运行的时侯,该服务所依赖的其他单元,此处指定的值为multi-user.target,表示在开机手动运行该服务之前,系统须要先运行multi-user.target这个单元以后,就会启动该服务。

2.7.2.创建自定义的单元文件(unitfiles)

要创建自定义的服务单元文件,通常的步骤如下所示:

打算自定义服务的可执行文件。可以是自己写的脚本,也可以是软件提供者提供的可执行程序。假如有必要,可以打算PID文件,拿来储存自定义服务的主进程的PID信息,也可以通过环境变量文件给自定义服务提供shell变量。请确保脚本或则程序具有执行权限,假如没有执行权限,须要执行chmoda+x给其降低执行权限。

在/etc/systemd/system/目录或则/usr/lib/systemd/system/目录中创建自定义服务的单元文件(unitfiles),同时须要注意文件的权限。具体如下所示:

创建自定义服务的单元文件:

# touch /etc/systemd/system/name.service
# chmod 644 /etc/systemd/system/name.service
或者
# touch /usr/lib/systemd/system/name.service
# chmod 644 /usr/lib/systemd/system/name.service

上述的两种方式都是可以的。

用文本编辑器打开上面创建的单元文件,并添加对应的服务配置选项。下边的简单示例如下:

[Unit]

Description=service_description

After=network.target

[Service]

ExecStart=path_to_executable

Type=forking

PIDFile=path_to_pidfile

[Install]

WantedBy=default.target

在上述的内容中,service_description部份会显示在systemctlstatus命令输出的日志文件中。After确保这个自定义服务在network.target启动以后才能运行,假如依赖多个其他单元,这么可以在After选项的旁边通过空格将多个单元分隔。path_to_executable代表了才能启动服务的脚本或则程序。Type=forking代表ExecStart选项中启动的服务会调用fork()程序启动子进程,当子进程启动完成,但是所有的通讯通道都设置完成以后,此时就应当退出父进程。此时子进程作为服务的主进程持续运行,假如使用了forking的方式,这么也须要提供PIDFile来保存服务的PID信息,便于systemd就能辨识到这个服务的进程。WantedBy这个自定义服务所依赖的其他单元。

通知systemd守护进程,有新的自定义服务被添加进来。

以root身分执行如下命令:

# systemctl daemon-reload
# systemctl start name.service

警告:在每次降低了新的单元文件或则更改了既有的单元文件以后,都应当执行systemctldaemon-reload命令。否则执行systemctlstart或则systemctlenable命令的时侯就难以辨识新增或更改的内容。

下边通过示例演示自定义服务的单元文件的步骤和过程。一直以sshd这个服务为例,创建sshd服务的第二个单元文件,并使用不同的服务端标语。具体过程如下所示:

复制既有的sshd服务的配置文件

[root@c7u6-ha1 ~]# cp /etc/ssh/sshd{,-second}_config
[root@c7u6-ha1 ~]# ll /etc/ssh/sshd*_config
-rw------- 1 root root 3908 Jun  8 14:14 /etc/ssh/sshd_config
-rw------- 1 root root 3908 Jun  8 14:17 /etc/ssh/sshd-second_config
[root@c7u6-ha1 ~]#

编辑里面的sshd-second_config这个配置文件,更改其中的服务端标语和PID文件

[root@c7u6-ha1 ~]# vim /etc/ssh/sshd-second_config
[root@c7u6-ha1 ~]# cat /etc/ssh/sshd-second_config | egrep '2212|PidFile'
Port 2212
PidFile /var/run/sshd-second.pid
[root@c7u6-ha1 ~]#

关于sshd_config的配置选项,详尽信息参见sshd_config(5)的帮助信息,执行命令man5sshd_config即可。

里面在第二个配置文件手指定的PID文件无需实现创建,在重启sshd服务的时侯会手动创建这个PID文件。

拷贝既有的sshd.service单元文件

[root@c7u6-ha1 ~]# cp /usr/lib/systemd/system/sshd{,-second}.service
[root@c7u6-ha1 ~]# ll /usr/lib/systemd/system/sshd*.service
-rw-r--r--. 1 root root 313 Apr 11  2018 /usr/lib/systemd/system/sshd-keygen.service
-rw-r--r--  1 root root 373 Jun  8 14:25 /usr/lib/systemd/system/sshd-second.service
-rw-r--r--. 1 root root 373 Apr 11  2018 /usr/lib/systemd/system/sshd.service
-rw-r--r--. 1 root root 260 Apr 11  2018 /usr/lib/systemd/system/sshd@.service
[root@c7u6-ha1 ~]#

上述是将sshd.service直接在/usr/lib/systemd/system/这个目录中复制了一份,也可以将其复制到/etc/systemd/system/这个目录中。RHEL官方文档建议系统管理员应当将自定义的服务单元文件放置在/etc/systemd/system/这个目录中。

更改此前复制的sshd-second.service这个单元文件

a.更改Description选项

Description=OpenSSHserversecondinstancedaemon

b.将sshd.service添加为该服务的依赖服务,即sshd-second.service须要在sshd.service以后启动

After=syslog.targetnetwork.targetauditd.servicesshd.service

c.因为第一个sshd.service中早已包含了秘钥生成,所以在第二个服务中须要将秘钥生成部份移除

ExecStartPre=/usr/sbin/sshd-keygen

移除前面这一行

d.添加-f/etc/ssh/ssh-second_config这个参数

ExecStart=/usr/sbin/sshd-D-f/etc/ssh/ssh-second_config$OPTIONS

执行了上述更改以后,此时sshd-second.service文件的内容如下所示:

linux系统重置_linux重置磁盘_linux重置系统盘命令

[root@c7u6-ha1 ~]# cat /usr/lib/systemd/system/sshd-second.service
[Unit]
Description=OpenSSH server second instance daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service sshd.service
Wants=sshd-keygen.service
[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D -f /etc/ssh/sshd-second_config $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
[root@c7u6-ha1 ~]#

至此,sshd-second.service这个单元文件就打算好了。

使能sshd-second.service,使其能否在上次开机的时侯手动启动

重新装载配置文件,并设置开机手动启动,具体如下所示:

[root@c7u6-ha1 ~]# systemctl daemon-reload
[root@c7u6-ha1 ~]# systemctl enable --now sshd-second.service
Created symlink from /etc/systemd/system/multi-user.target.wants/sshd-second.service to /usr/lib/systemd/system/sshd-second.service.
[root@c7u6-ha1 ~]#

上述使用systemctldaemon-reload通知服务进程重新装载配置文件,也可以执行initq命令,具有同样的疗效。做完上述设置以后,查看系统是否有2212端口处于窃听状态。

[root@c7u6-ha1 ~]# ss -ntl | egrep 2212
LISTEN     0      128          *:2212                     *:*
LISTEN     0      128         :::2212                    :::*
[root@c7u6-ha1 ~]#

在宿主机上开启另一个终端,并尝试使用前面的2212号端口登陆虚拟机,具体如下所示:

[root@LiuXianQiE ~]# ssh -p 2212 root@c7u6-ha1
Last login: Tue Jun  8 14:19:19 2021 from 192.168.122.1
[root@c7u6-ha1 ~]#

从上述输出中可以看出,可以通过2212号端口正常登陆虚拟机。

上述即为创建自定义单元文件的过程。

2.7.3.更改既存单元文件

早已安装到系统上的服务,会将自带的默认单元文件储存在/usr/llib/systemd/system/这个目录中。通常不建议系统管理员直接更改这个目录中的单元文件,应当尽量将自定义更改放置在/etc/systemd/system/这个目录中。具体操作形式,取决于所要求修改的程度,不同的更改程度,对应于下边的两种形式:

3.重置root用户密码

关于root用户密码重置,有两种形式:第一种是通过live光碟进行更改;第二种是通过更改grub信息实现更改,而这些方法又对应两种不同的内核参数,分别为rd.break和init指定交互的shell类库。

这两种方法分别介绍如下,以Fedora34为例:

3.1.通过live光碟重置root用户密码启动虚拟机,并在开始的时侯选择依照提示步入boot菜单,如右图所示:

在上图中按下ESC键步入boot菜单,弹出boot菜单,如右图所示:

选择上图的第2项从硬盘启动系统。在右图的引导界面中选择第一个,即”StartFedora-Workstation-Live34″

步入到右图所示的界面,选择”TryFedora”,具体如右图所示:

打开terminal,之后执行如右图所示操作,具体如右图所示:

2.重启系统,即可输入重置后的密码进行登陆

再上图中选择第一个,正常启动系统即可。

在上图中输入用户名root

在上图中输入重置后的密码,即可登入系统,在右图中打开终端,如右图所示:

登陆成功。3.2.通过更改grub引导中的内核参数重置root用户密码

这儿按照在grub引导菜单中使用的内核参数不同,又分为两种形式,这两种方法不屈从于具体的发行版,只要是采用GRUB2引导的Linux系统,理论上都可以使用下边的两种形式实现root用户密码重置。分别如下:

3.2.1.通过rd.break内核参数重置root用户密码

通过在启动的时侯,出现GRUB的引导菜单的时侯,按e键即可编辑内核选项。具体如右图所示:

再上图按e键,步入右图所示界面:

在上图的linux开始的行中,将末尾的rhgbquit去除,之后降低rd.break,同时将原先的ro更改为rw,将要根分区以读写的形式挂载到/sysroot这个目录上,之后按下Ctrl+x键即可继续启动系统,具体如右图所示:

在右图中按下Ctrl+d键,即可步入shell交互命令行,因为此时系统真正的根分区挂载在/sysroot上,所以须要将其以读写的形式重新进行挂载,之后执行命令chroot/sysroot切根,此时的上下文环境才是真正的根分区。之后在命令行中执行命令passwdroot重置root用户密码。假如原先的系统没有开启SELinux的话,这么此时执行exit退出切根环境,并输入reboot命令重启的话,就可以实现密码重置的操作了。但因为我这个Fedora是新安装的系统,且并没有关掉SELinux,所以,还须要再执行一下touch/.autorelabel这个命令,提示SELinux重新加载标签。在上次重启的时侯,假若系统的本地文件系统中文件好多,这个过程所须要的时间会比较长。重启以后,输入root用户名,就可以使用重置后的密码进行登陆了。具体如右图所示:

假如忘掉上图中根分区被挂载到那个目录下,可以在上图的命令行中执行命令mount,即可显示出根目录挂载的路径。上图重启完以后,输入root用户名和密码,就可以使用重置后的密码登入系统了。具体如右图所示:

上图是SELinux打标签的过程。

至此,通过更改grub,降低rd.break这个内核参数实现的root密码重置操作就完成了。

3.2.2.通过init内核参数重置root用户密码

这些方法整体流程跟上一种方法类似,只不过是使用的init这个内核参数,更改登入过程中的shell类库。

在grub的引导菜单中,将linux开头的行中的ro更改为rw,之后去除末尾的rhgbquiet,之后降低init=/sysroot/bin/bash,具体如右图所示:

在上图中按下Ctrl+x继续启动系统,在右图中按下Ctrl+d步入到我们在内核手指定的shell类库交互界面,如右图所示:

步入到shell以后,执行chroot/sysroot命令切根,之后执行命令passwdroot更改root用户的登陆密码linux主机,假如原先的系统中没有开启SELinux,这么此时执行exit退出切根环境,并执行reboot重启即可完成root用户密码重置的操作。并且因为我的Fedora34是新安装的系统,并未禁用SELinux,所以,此时必需要执行touch/.autorelabel命令给更改后的根分区中的配置文件重新打标签才可以使更改生效。假如省略掉这步,而系统又刚好开启了SELinux,这么上述的密码重置操作时不会生效的。具体如右图所示:

SELinux给文件系统中的文件打标签。这个过程历时长短取决于本地文件系统中文件的多少,文件越多,历时越长。

至此,root用户密码重置的操作就完成了。

4.ReferencesAnintroductiontotheLinuxbootandstartupprocessessystemdbootupFile:GNUGRUBonMBRpartitionedharddiskdrives.svgRed_Hat_Enterprise_Linux-7-System_Administrators_Guide-en-US.pdf

Author

这篇优质的内容由TA贡献而来

刘遄

《Linux就该这么学》书籍作者,RHCA认证架构师,教育学(计算机专业硕士)。

发表回复