0%

添加交换分区

向您的系统添加一个额外的交换分区 756MiB。交换分区应在系统 启动时自动挂载。不要删除或以任何方式改动系统上的任何现有交换分区。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
[root@node2 ~]# lsblk 
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 252:0 0 10G 0 disk
├─vda1 252:1 0 1M 0 part
├─vda2 252:2 0 100M 0 part /boot/efi
└─vda3 252:3 0 9.9G 0 part /
vdb 252:16 0 4G 0 disk
├─vdb1 252:17 0 510M 0 part
│ └─myvol-vo 253:0 0 232M 0 lvm /reports
└─vdb2 252:18 0 512M 0 part
└─vgroup-swap 253:1 0 256M 0 lvm [SWAP]
vdc 252:32 0 10G 0 disk
[root@node2 ~]# parted /dev/vdb unit MiB print
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 4096MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number Start End Size Type File system Flags
1 1.00MiB 511MiB 510MiB primary
2 511MiB 1023MiB 512MiB primary

[root@node2 ~]# parted /dev/vdb mkpart primary 1024MiB 1780MiB
Information: You may need to update /etc/fstab.

[root@node2 ~]# mkswap /dev/vdb3
mkswap: /dev/vdb3: warning: wiping old swap signature.
Setting up swapspace version 1, size = 756 MiB (792719360 bytes)
no label, UUID=114ea12c-6f82-420c-b1fe-1e9eb896cdb5
[root@node2 ~]# swapon /dev/vdb3
[root@node2 ~]# swapon --show
NAME TYPE SIZE USED PRIO
/dev/dm-0 partition 256M 0B -2
/dev/vdb3 partition 756M 0B -3
[root@node2 ~]# swapoff /dev/vdb3
[root@node2 ~]# lsblk --fs
NAME FSTYPE LABEL UUID MOUNTPOINT
vda
├─vda1
├─vda2 vfat 399C-0F7D /boot/efi
└─vda3 xfs root 3cd0d4ca-93f6-423b-a469-70ab2b10b667 /
vdb
├─vdb1 LVM2_member Xrxvxp-xXd8-Bs15-k0hI-lVOT-fcLM-kvqbjA
│ └─myvol-vo ext4 64c880fa-00b3-43bd-8d38-3d36344bbf0a /reports
├─vdb2 LVM2_member e8VJoR-Cuzu-vufT-8UsH-zgJb-chQQ-QlBvVx
│ └─vgroup-swap swap 822650ee-0dfc-4c0a-906d-99c71e784fca [SWAP]
└─vdb3 swap 114ea12c-6f82-420c-b1fe-1e9eb896cdb5
vdc
[root@node2 ~]# vim /etc/fstab
...
UUID=114ea12c-6f82-420c-b1fe-1e9eb896cdb5 swap swap defaults 0 0
[root@node2 ~]# systemctl daemon-reload
[root@node2 ~]# swapon -a
[root@node2 ~]# swapon --show
NAME TYPE SIZE USED PRIO
/dev/dm-0 partition 256M 0B -2
/dev/vdb3 partition 756M 0B -3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@node2 ~]# fdisk /dev/vdb

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type
p primary (2 primary, 0 extended, 2 free)
e extended (container for logical partitions)
Select (default p):

Using default response p.
Partition number (3,4, default 3):
First sector (2095106-8388607, default 2097152):
Last sector, +sectors or +size{K,M,G,T,P} (2097152-8388607, default 8388607): +756M

Created a new partition 3 of type 'Linux' and of size 756 MiB.

Command (m for help): w
The partition table has been altered.
Syncing disks.

默认情况下,parted 显示以 10 的幂次方表示的所有空间大小(KB、MB、GB)。您可以使用 unit 子命令来更改默认设置,该子命令接受以下参数:

s 表示扇区

B 表示字节

MiB、GiB 或 TiB(2 的幂次方)

MB、GB 或 TB(10 的幂次方)

设置逻辑卷大小

将逻辑卷 vo 及其文件系统的大小调整到 230 MiB。确保文件系统内容保持不变。注:分区大小很少与请求的大小完全相同,因此可以接受范围为 217 MiB 到 243 MiB 的大小。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
[root@node2 ~]# lvdisplay 
--- Logical volume ---
LV Path /dev/vgroup/swap
LV Name swap
VG Name vgroup
LV UUID 80k3wh-3b52-387w-VFW9-pLdC-7jH6-EQ0cps
LV Write Access read/write
LV Creation host, time (none), 2023-02-19 11:58:34 -0500
LV Status available
# open 2
LV Size 256.00 MiB
Current LE 64
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1

--- Logical volume ---
LV Path /dev/myvol/vo
LV Name vo
VG Name myvol
LV UUID 91S5cv-jxFA-XK1F-79ZQ-pfxh-ho1Y-JUZXwM
LV Write Access read/write
LV Creation host, time (none), 2023-02-19 11:58:33 -0500
LV Status available
# open 1
LV Size 184.00 MiB
Current LE 46
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0

[root@node2 ~]# vgdisplay myvol
--- Volume group ---
VG Name myvol
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 1
Act PV 1
VG Size 508.00 MiB
PE Size 4.00 MiB
Total PE 127
Alloc PE / Size 46 / 184.00 MiB
Free PE / Size 81 / 324.00 MiB
VG UUID Vxlt2s-G798-c1pt-B7Pf-uqaU-u6fE-j72DLZ

[root@node2 ~]# man lvextend
...
Extend an LV by a specified size.

lvextend -L|--size [+]Size[m|UNIT] LV
[ -l|--extents [+]Number[PERCENT] ]
[ -r|--resizefs ]
[ -i|--stripes Number ]
[ -I|--stripesize Size[k|UNIT] ]
[ --poolmetadatasize [+]Size[m|UNIT] ]
[ COMMON_OPTIONS ]
[ PV ... ]
[root@node2 ~]# lvextend -L 230m /dev/myvol/vo
Rounding size to boundary between physical extents: 232.00 MiB.
Size of logical volume myvol/vo changed from 184.00 MiB (46 extents) to 232.00 MiB (58 extents).
Logical volume myvol/vo successfully resized.
[root@node2 ~]# lvdisplay /dev/myvol/vo
--- Logical volume ---
LV Path /dev/myvol/vo
LV Name vo
VG Name myvol
LV UUID 91S5cv-jxFA-XK1F-79ZQ-pfxh-ho1Y-JUZXwM
LV Write Access read/write
LV Creation host, time (none), 2023-02-19 11:58:33 -0500
LV Status available
# open 1
LV Size 232.00 MiB
Current LE 58
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0

[root@node2 ~]# resize2fs /dev/mapper/myvol-vo
resize2fs 1.45.4 (23-Sep-2019)
Filesystem at /dev/mapper/myvol-vo is mounted on /reports; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 2
The filesystem on /dev/mapper/myvol-vo is now 237568 (1k) blocks long.

[root@node2 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 887M 0 887M 0% /dev
tmpfs tmpfs 914M 0 914M 0% /dev/shm
tmpfs tmpfs 914M 25M 889M 3% /run
tmpfs tmpfs 914M 0 914M 0% /sys/fs/cgroup
/dev/vda3 xfs 9.9G 1.6G 8.4G 16% /
/dev/vda2 vfat 100M 6.8M 94M 7% /boot/efi
/dev/mapper/myvol-vo ext4 221M 2.1M 204M 1% /reports
tmpfs tmpfs 183M 0 183M 0% /run/user/0

xfs_growfs 与 resize2fs 之间的主要区别是为识别文件系统而传递的参数。xfs_growfs 采用挂载点,而 resize2fs 采用逻辑卷名称。

配置您的系统以使用默认存储库

YUM 存储库已可以从 http://foundation0.ilt.example.com/dvd/BaseOShttp://foundation0.ilt.example.com/dvd/AppStream 使用配置您的系统,以将这些位置用作默认存储库

1
2
3
4
5
6
7
8
9
10
11
[root@node2 ~]# scp root@172.25.250.100:/etc/yum.repos.d/*.repo /etc/yum.repos.d/
The authenticity of host '172.25.250.100 (172.25.250.100)' can't be established.
ECDSA key fingerprint is SHA256:1H687jfusVXYAUzAuByFfx1U/lB4VS+6h04wRhXhmZU.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '172.25.250.100' (ECDSA) to the list of known hosts.
root@172.25.250.100's password:
defaults.repo 100% 315 75.6KB/s 00:00
[root@node2 ~]# yum repolist all
repo id repo name status
AppStream AppStream enabled
BaseOS BaseOS enabled

设置 root 密码

将 node2 的 root 密码设置为 flectrag。您需要获得系统访问权限才能进行此操作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Send key -> Ctrl+Alt+Del
# Press 'e' to edit the selected item
# linux ($root)/... rd.break console=tty0
# Press Ctrl-x to start
switch_root:/# mount | grep sysroot
/dev/vda3 on /sysroot type xfs (ro,relatime,attr2,inode64,noquota)
...
switch_root:/# mount -o remount,rw /sysroot/
switch_root:/# mount | grep sysroot
/dev/vda3 on /sysroot type xfs (rw,relatime,attr2,inode64,noquota)
...
switch_root:/# chroot /sysroot/
sh-4.4# echo flectrag | passwd --stdin root
Changing password for user root.
passwd: all authentication tokens updated successfully.
# 确保所有未标记的文件(包括此时的 /etc/shadow)在启动过程中都会重新获得标记
sh-4.4# touch /.autorelabel
sh-4.4# exit
exit
switch_root:/# exit
logout
...

在红帽企业 Linux 8 中,可以使从 initramfs 运行的脚本在某些点暂停,提供 root shell,然后在该 shell 存在的情况下继续。这主要是为了进行测试,但也可以使用该方法来重置丢失的 root 密码。

要访问该 root shell,请按照以下步骤操作:

  1. 重新启动系统。

  2. 按任意键(Enter 除外)中断启动加载器倒计时。

  3. 将光标移至要启动的内核条目。

  4. 按 e 编辑选定的条目。

  5. 将光标移到内核命令行(以 linux 开头的行)。

  6. 附加 rd.break。利用该选项,就在系统 initramfs 向实际系统移交控制权前,系统将会中断。

  7. 按 Ctrl+x 使用这些更改进行启动。

此时,系统会显示 root shell,且磁盘上的实际根文件系统会在 /sysroot 中以只读方式挂载。由于进行故障排除经常要求修改根文件系统,因此您需要将根文件系统更改为读/写。

系统尚未启用 SELinux,因此您所创建的任何文件都没有 SELinux 上下文。有些工具(例如 passwd 命令)首先会创建一个临时文件,然后移动新文件以代替要编辑的文件,从而有效地创建不带 SELinux 上下文的新文件。因此,当您对 passwd 命令使用 rd.break 时,/etc/shadow 文件并没有获得 SELinux 上下文。

配置一个应用

配置一个应用 rhcsa

这个应用以 natasha 身份运行时,会显示一个字符串 This is a rhcsa

1
2
3
4
5
[root@node1 ~]# su - natasha 
[natasha@node1 ~]$ echo "alias rhcsa='echo This is a rhcsa'" >> .bashrc
[natasha@node1 ~]$ source .bashrc
[natasha@node1 ~]$ rhcsa
This is a rhcsa