1.创建目录
mkdir a
a
mkdir a b c
a b c
mkdir aaa/bbb/ccc -p
tree aaa
├── aaa
│ └── bbb
│ └── ccc
# 创建多个相同开头字符的目录
mkdir a{1,2,3}
a1 a2 a3
# 创建指定范围的目录
mkdir test{1..5}
test1 test2 test3 test4 test5
2.移动目录
#将 a 目录移动到 b 目录中
mv a b
── b
│ └── a
3.复制目录
#将 c 目录复制到 b 目录中
cp -r c b
4.重命名目录
#将 b 目录修改为 bb 目录
mv b bb
将 d 目录修改为 dd 目录,并移到 bb 目录下
mv d bb/dd
5.删除目录
rm c
# 可以使用 -d 选项来删除空目录
rm -d c
# 删除非空目录需要使用 -r 选项,-f 选项是强制删除不给出提示
rm -rf cc
#删除多个目录
rm -rf aa a1
rm -rf test{1..5}
6.文件管理
#创建文件
touch test.txt
# 查看文件元数据信息
stat test.txt
Access: 2025-03-06 20:53:01.555979344 +0800 # 最后一次访问时间
Modify: 2025-03-06 20:53:01.555979344 +0800 # 最后一次修改时间
Change: 2025-03-06 20:53:01.555979344 +0800 # 最后一次修改元数据时间
Birth: 2025-03-06 20:53:01.555979344 +0800 # 文件的创建时间
如果是创建相同的文件名创建的时间不变其他全变
使用 touch 命令创建文件时,如果文件不存在则创建,如果文件存在则会修改文件的 atime、mtime、ctime 的值。
touch 命令只能创建空白文件
重定向创建文件
#使用覆盖重定向来创建文件
echo hello > hello.txt
cat hello.txt
#使用追加重定向来创建文件
echo mlgb >> hello.txt
cat hello.txt
mlgb
hello
#使用输入重定向创建文件
cat > demo <<EOF
> ni hao
> wo hao
> da jia hao
> EOF
cat demo
ni hao
wo hao
da jia hao
7.使用vi/vim编辑器创建
1、命令模式切到编辑模式
- i:在光标左边
- I:光标在行首
- a:在光标右边
- A:光标在行尾
- o:在光标的下一行
- O:在光标的上一行
编辑模式切换为命令模式
按 esc 键
命令模式下的快捷键
- yy:复制一行
- Nyy:复制N行,N为数字
- p:在光标所在位置的下一行插入数据
- P:在光标所在位置的上一行插入数据
- dd:删除一行
- Ndd:删除N行,N为数字
- gg:光标定位到内容的开头
- G:光标定位到内容的最后一行
- hjlk这四个键就是上下左右
- ^:将光标定位到行首
- $:将光标定位到行尾
- r:替换光标所在位置的字符
编辑模式下的快捷操作
- /关键字:在文件中搜索
- ?关键字:从文件的开头向下搜索
- :%s/被替换的字符/新的字符/g :将文件中的内容进行替换。
末行模式下的快捷操作
- w:保存但不退出
- q:退出但不保存
- w filename:将文件重新命令
- q!:强制退出且不保存
- wq:保存并退出
- ZZ:保存并退出
8.查看文件
#这个命令是用于查看文件所有内容的
cat/etc/passwd
这个命令只适合查看小文件
#这个命令用于分页查看文件内容,按 pagedown 或空格键可以向下翻页,按 pageup可以向上翻页,按回车或向下箭头可以向下一行一行查看
more /etc/passwd
less这个命令用于分页查看文件内容,按 pagedown 或空格键可以向下翻页,按 pageup可以向上翻页,按回车或向下箭头可以向下一行一行查看。文件查看完后命令不会退出,如果要退出则需要按 q 键。
less /etc/passwd
head查看文件最前面的10行数据,可以使用 -n 选项来指定要查看的行数。
head -n /etc/passwd | cat -n
tail查看文件最后的10行数据,可以使用 -n 选项来指定要查看的行数。
tail /etc/passwd
除了可以查看最后几行数据外,还可以使用 -f 选项来监控文件的变化
tail -f /etc/passwd
9.文件搜索
whereis这个命令用于搜索二进制文件所以位置,以及它的文档路径。
whereis cd
which这个命令用于搜索二进制文件所在位置
which cd
find这个命令可以按文件大小、文件权限、文件类型、文件名称、创建时间等查找。
find / -name hello.txt
# 通配符,查找所有 .log 文件
find /var/log -name "*.log"
# 忽略大小写 -iname
find . -iname "hello*.txt"
按文件类型 `-type` `f`:普通文件`d` 目录`l`:软链接
# 查找当前目录下所有目录
find . -type d
# 查找所有普通文件
find . -type f
# 大于10M的文件
find / -size +10M
# 小于100k的文件
find . -size -100k
# 等于50M
find . -size 50M
按权限 `-perm`
# 查找权限777的文件
find . -perm 777
按时间查找
- `-mtime`:修改时间(天)
- `-mmin`:修改时间(分钟)
# 7天以内修改过的文件
find . -mtime -7
# 超过30天没修改的文件
find . -mtime +30
# 最近30分钟修改的文件
find . -mmin -30
10.复制文件
使用 cp 命令
# 复制的同时修改名称 将hello复制到a 改名为hello1
cp hello a/hello1
注意:在使用 cp 命令复制目录时需要带 -r 选项才行
cp -r a b/g
scp 命令,也是可以进行文件复制的,但是它是用于虚拟机与虚拟机之间的复制操作
11.删除文件
使用 rm 命令
rm hello
用户和组管理
12.用户管理
超级用户:root,它的特点是 uid=0,gid=0,它不受权限的控制
系统用户:这类用户的 uid 在 1 ~ 999 之间,gid 也在1 ~ 999 之间,它的特点是这类用户不能用于登录,而是用于某个进程运行进所需要的用户
普通用户:这类用户是我们常见的用户。它的 uid 范围在 1000 ~ 60000 之间,gid 范围在 1000 ~ 60000 之间。它会受到权限的控制
对于用户的来说,会涉及到以下目录或文件:
1、/etc/passwd
2、/etc/shadow
3、/etc/group
4、/etc/gshadow
5、/home/用户名
6、/var/spool/mail/用户名
7、/etc/skel/
8、/etc/subgid
9、/etc/subuid
10、/etc/login.defs
创建用户
使用 useradd 命令来创建用户,默认情况下,创建用户时,会同时创建与用户名相同的组
#使用手册
man useradd
创建zhangsan账号 useradd 账号名
useradd zhangsan
id zhangsan
/etc/passwd 文件的各字段说明:
zhangsan : x : 1001 : 1001 : : /home/zhangsan : /bin/bash
1 2 3 4 5 6 7
1:用户名
2:密码占位符
3:用户的uid
4:用户的gid
5:用户的信息
6:用户的家目录
7:用户可执行的脚本
tail -1 /etc/shadow
zhangsan:!:20700:0:99999:7:::
# 如果是 !! 表示没有密码
# 给zhangsan 用户设置密码
passwd zhangsan
# 或者echo 123456 | passwd zhangsan --stdin
# 设置完密码后再次查看
tail -1 /etc/shadow
tail -1 /etc/group
zhangsan : x : 1001 :
1 2 3 4
1:组名
2:组密码占位符
3:组id
4:组用户
tail -1 /etc/gshadow
zhangsan : ! : :
1 2 3 4
1:组
2:组密码
3:附加组
13.修改用户
使用 usermod 命令
用户删除 删除zhangsan 用户
userdel zhangsan -r
14.组管理
创建组
使用 groupadd 命令
groupadd test
修改组
使用groupmod命令
将 test 组名修改为 demo
groupmod -n demo test
删除组
使用 groupdel 命令
删除 demo 组
groupdel demo
存储管理
15.增加磁盘
我们在虚拟机上增加两个磁盘,一块 sata,一块 nvme ,各自为 20G
增加好后启动虚拟机,使用如下命令来查看:
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 20G 0 disk
sr0 11:0 1 11G 0 rom
nvme0n1 259:0 0 20G 0 disk
├─nvme0n1p1 259:1 0 600M 0 part /boot/efi
├─nvme0n1p2 259:2 0 1G 0 part /boot
└─nvme0n1p3 259:3 0 18.4G 0 part
├─rhel-root 253:0 0 16.4G 0 lvm /
└─rhel-swap 253:1 0 2G 0 lvm [SWAP]
nvme0n2 259:4 0 20G 0 disk
16.分区分区不是必须的。
分区使用 fdisk 命令
fdisk 设备路径
在 linux 中分区目前有两个格式:
- mbr
- 磁盘大小不能超过 2.2T
- 最多只支持 4 个主分区(可以是 3 个主分区 + 1个扩展分区)
- 扩展分区不能直接使用,需要有逻辑分区
- gpt
-
可支持超过 2.2T 大小的磁盘
-
必须先创建 gtp 分区表
-
没有主分区和扩展分区的概念
[root@localhost ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.37.4).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xddc9f457.Command (m for help): m
Help:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flagGeneric
d delete a partition # 删除分区
F list free unpartitioned space
l list known partition types # 查看分区类型
n add a new partition # 增加一个分区
p print the partition table # 打印分区表
t change a partition type
v verify the partition table
i print information about a partitionMisc
m print this menu # 显示操作菜单
u change display/entry units
x extra functionality (experts only)Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script fileSave & Exit
w write table to disk and exit # 保存分区信息
q quit without saving changes # 退出分区但不保存Create a new label
g create a new empty GPT partition table # 创建gpt分区表
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition tableCommand (m for help):
Command (m for help): n # 输入n来创建一个分区
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): # 此处直接回车,来做一个主分区Using default response p.
Partition number (1-4, default 1): # 此处是选择主分区的序号,默认即可
First sector (2048-41943039, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-41943039, default 41943039): +10G # 指定分区大小Created a new partition 1 of type 'Linux' and of size 10 GiB.
Command (m for help):
接下来创建一个扩展分区
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): e # 选择 e 表示创建扩展分区
Partition number (2-4, default 2):
First sector (20973568-41943039, default 20973568):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (20973568-41943039, default 41943039): # 如果不指定大小,则表示将全部大小给到这个扩展分区Created a new partition 2 of type 'Extended' and of size 10 GiB.
创建逻辑分区
Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 5
First sector (20975616-41943039, default 20975616):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (20975616-41943039, default 41943039):Created a new partition 5 of type 'Linux' and of size 10 GiB.
Command (m for help):
查看分区信息
Command (m for help): p
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xddc9f457Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 20973567 20971520 10G 83 Linux
/dev/sda2 20973568 41943039 20969472 10G 5 Extended
/dev/sda5 20975616 41943039 20967424 10G 83 LinuxCommand (m for help):
如果分区信息没有问题,就可以输入 w 来保存分区信息
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.查看sda磁盘的分区情况
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 10G 0 part
├─sda2 8:2 0 1K 0 part
└─sda5 8:5 0 10G 0 part
sr0 11:0 1 11G 0 rom
nvme0n1 259:0 0 20G 0 disk
├─nvme0n1p1 259:1 0 600M 0 part /boot/efi
├─nvme0n1p2 259:2 0 1G 0 part /boot
└─nvme0n1p3 259:3 0 18.4G 0 part
├─rhel-root 253:0 0 16.4G 0 lvm /
└─rhel-swap 253:1 0 2G 0 lvm [SWAP]
nvme0n2 259:4 0 20G 0 disk
-
17.格式化
磁盘的格式很多,我们可以使用如下命令来查看
mkf
格式化顺序如下
xfs -> ext4 -> ext3
# 将 /dev/sda1 分区进行 xfs 类型的格式操作
[root@localhost ~]# mkfs.xfs /dev/sda1
meta-data=/dev/sda1 isize=512 agcount=4, agsize=655360 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1 bigtime=1 inobtcount=1 nrext64=0
data = bsize=4096 blocks=2621440, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=16384, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
# 查看格式化后信息
[root@localhost ~]# blkid
/dev/mapper/rhel-swap: UUID="07491280-a857-4390-b66d-fcda258ebf85" TYPE="swap"
/dev/nvme0n1p3: UUID="pTZ6j1-cfMW-JK0S-GBdM-I4vD-hH4v-POZgXd" TYPE="LVM2_member" PARTUUID="a505c12a-28d5-462e-ab7b-228e1db807b6"
/dev/nvme0n1p1: UUID="ED7B-335F" TYPE="vfat" PARTLABEL="EFI System Partition" PARTUUID="394e29ff-5b73-4b6d-a334-5e82e12f9349"
/dev/nvme0n1p2: UUID="d4bd25b8-cd26-4219-adf2-90e47bac466d" TYPE="xfs" PARTUUID="0ff45fa8-d933-48cb-bfe4-07e5cafe453f"
/dev/sr0: UUID="2024-10-07-15-23-22-00" LABEL="RHEL-9-5-0-BaseOS-x86_64" TYPE="iso9660" PTUUID="c6c4299d" PTTYPE="dos"
/dev/mapper/rhel-root: UUID="61ced70d-d636-4061-9aa0-aa713334b69a" TYPE="xfs"
/dev/sda5: PARTUUID="ddc9f457-05"
/dev/sda1: UUID="d6fe4868-e789-4b74-b6bb-8d01b894f515" TYPE="xfs" PARTUUID="ddc9f457-01"
18.挂在分区
手动挂载,系统启动后会丢失
#创建挂在目录
mkdir /sda1
#进行目录挂载
mount /dev/sda1 /sda1
#查看是否成功
df -h
#使用分区
echo aa > /sda1/a.txt
cat /sda1/a.txt
19.自动挂载
bash
[root@localhost ~]# vim /etc/fstab
/dev/mapper/rhel-root / xfs defaults 0 0
UUID=d4bd25b8-cd26-4219-adf2-90e47bac466d /boot xfs defaults 0 0
UUID=ED7B-335F /boot/efi vfat umask=0077,shortname=winnt 0 2
/dev/mapper/rhel-swap none swap defaults 0 0
# 增加如下一行
UUID="d6fe4868-e789-4b74-b6bb-8d01b894f515" /sda1 defaults 0 0
逻辑卷
pv (物理卷),一个pv对应一个分区,或者对应一块磁盘。相关的操作命令如下:
1、pvcreate 设备:用于创建一个物理卷
2、pvdisplay :显示物理卷
3、pvremove 设备:删除物理卷
vg(卷组),一个卷组由一到多个物理卷组成,一个卷组需要有一个名称,相关命令:
1、vgcreate 卷组名称 物理卷1 物理卷2 ...
2、vgdisplay :查看卷组
3、vgremove 卷组名 :删除卷组
lv(逻辑卷),一个逻辑卷可以由一到多个卷组组成,一个逻辑卷也需要有一个名称,并且需要给这个逻辑卷指定大小。
1、lvcreate -n 逻辑卷名称 -L 大小 卷组1 卷组2 ...
2、lvdisplay :查看逻辑卷
3、lvremove :删除逻辑卷
然后对逻辑卷进行格式化,然后再挂载使用。
为了演示这个部分,我们先把之前的分区挂载取消。
1)如果做了自动挂载,需要将 /etc/fstab 文件中的挂载信息删除。
2)然后卸载
umount /sda1/
3)删除分区(注意:此步不是必须的,我们也可以使用分区来做物理卷,此处只是为了演示)
root@localhost ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.37.4).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help):
# 查看当前的分区信息
Command (m for help): p
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xddc9f457
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 20973567 20971520 10G 83 Linux
/dev/sda2 20973568 41943039 20969472 10G 5 Extended
/dev/sda5 20975616 41943039 20967424 10G 83 Linux
# 输入d来删除分区
Command (m for help): d
Partition number (1,2,5, default 5): 5 # 首先删除逻辑分区
Partition 5 has been deleted.
Command (m for help): d
Partition number (1,2, default 2): 2 # 然后再删除扩展分区
Partition 2 has been deleted.
Command (m for help): d # 最后删除主分区
Selected partition 1
Partition 1 has been deleted.
# 再次打印分区信息
Command (m for help): p
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xddc9f457
Command (m for help):
# 输入w来保存当前的操作
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
# 查看磁盘信息
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 20G 0 disk
sr0 11:0 1 11G 0 rom
nvme0n1 259:0 0 20G 0 disk
├─nvme0n1p1 259:1 0 600M 0 part /boot/efi
├─nvme0n1p2 259:2 0 1G 0 part /boot
└─nvme0n1p3 259:3 0 18.4G 0 part
├─rhel-root 253:0 0 16.4G 0 lvm /
└─rhel-swap 253:1 0 2G 0 lvm [SWAP]
nvme0n2 259:4 0 20G 0 disk
21.创建卷组
# 1. 创建卷组
[root@localhost ~]# vgcreate vg1 /dev/sda /dev/nvme0n2
WARNING: dos signature detected on /dev/sda at offset 510. Wipe it? [y/n]: y
Wiping dos signature on /dev/sda.
Physical volume "/dev/sda" successfully created.
Physical volume "/dev/nvme0n2" successfully created.
Volume group "vg1" successfully created
# 查看卷组信息
[root@localhost ~]# vgdisplay vg1
--- Volume group ---
VG Name vg1
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 39.99 GiB
PE Size 4.00 MiB
Total PE 10238
Alloc PE / Size 0 / 0
Free PE / Size 10238 / 39.99 GiB
VG UUID pM3d0b-VEzg-YM69-5CXq-FYw8-D3ps-1La2SY
# 如果不想使用这个卷组,则可以使用下面的命令来删除(注意:这步此时不做)
[root@localhost ~]# vgremove vg1
22.创建逻辑卷
# 创建逻辑卷,命令格式为:lvcreate -n 逻辑卷名称 -L 逻辑卷大小 卷组名称
[root@localhost ~]# lvcreate -n lv1 -L 30G vg1
WARNING: xfs signature detected on /dev/vg1/lv1 at offset 0. Wipe it? [y/n]: y
Wiping xfs signature on /dev/vg1/lv1.
Logical volume "lv1" created.
# 查看创建好的逻辑卷信息
[root@localhost ~]# lvdisplay /dev/vg1/lv1
--- Logical volume ---
LV Path /dev/vg1/lv1
LV Name lv1
VG Name vg1
LV UUID nY0B8J-puLE-WbK0-yBMK-IP2O-yWGh-EggPqT
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2025-03-09 11:38:43 +0800
LV Status available
# open 0
LV Size 30.00 GiB
Current LE 7680
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2
23.格式化
```bash
[root@localhost ~]# mkfs.xfs /dev/vg1/lv1
meta-data=/dev/vg1/lv1 isize=512 agcount=4, agsize=1966080 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1 bigtime=1 inobtcount=1 nrext64=0
data = bsize=4096 blocks=7864320, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=16384, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
# 查看格式化后的信息
[root@localhost ~]# blkid
/dev/mapper/rhel-swap: UUID="07491280-a857-4390-b66d-fcda258ebf85" TYPE="swap"
/dev/nvme0n1p3: UUID="pTZ6j1-cfMW-JK0S-GBdM-I4vD-hH4v-POZgXd" TYPE="LVM2_member" PARTUUID="a505c12a-28d5-462e-ab7b-228e1db807b6"
/dev/nvme0n1p1: UUID="ED7B-335F" TYPE="vfat" PARTLABEL="EFI System Partition" PARTUUID="394e29ff-5b73-4b6d-a334-5e82e12f9349"
/dev/nvme0n1p2: UUID="d4bd25b8-cd26-4219-adf2-90e47bac466d" TYPE="xfs" PARTUUID="0ff45fa8-d933-48cb-bfe4-07e5cafe453f"
/dev/sr0: UUID="2024-10-07-15-23-22-00" LABEL="RHEL-9-5-0-BaseOS-x86_64" TYPE="iso9660" PTUUID="c6c4299d" PTTYPE="dos"
/dev/mapper/rhel-root: UUID="61ced70d-d636-4061-9aa0-aa713334b69a" TYPE="xfs"
/dev/mapper/vg1-lv1: UUID="8bc28620-6709-4565-8370-cdf211661e7a" TYPE="xfs"
/dev/nvme0n2: UUID="GKkJSA-kU7E-tm3k-NnET-dl71-K7Ip-31UJQn" TYPE="LVM2_member"
/dev/sda: UUID="meNNaJ-6jgC-oueH-lb93-MLDd-O8QP-BiYSrM" TYPE="LVM2_member"
24.挂载逻辑卷
```bash
# 将格式化好的逻辑卷挂载到 /sda1 目录下
[root@localhost ~]# mount /dev/mapper/vg1-lv1 /sda1/
mount: (hint) your fstab has been modified, but systemd still uses
the old version; use 'systemctl daemon-reload' to reload.
# 由于之前修改了 /etc/fstab 文件,系统提示我们需要执行 systemctl daemon-reload 来重启载入配置
[root@localhost ~]# systemctl daemon-reload
# 进行挂载
[root@localhost ~]# mount /dev/mapper/vg1-lv1 /sda1/
mount: /sda1: /dev/mapper/vg1-lv1 already mounted on /sda1.
# 验证挂载是否成功
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 777M 11M 766M 2% /run
efivarfs 256K 56K 196K 23% /sys/firmware/efi/efivars
/dev/mapper/rhel-root 17G 1.5G 15G 10% /
/dev/nvme0n1p2 960M 191M 770M 20% /boot
/dev/nvme0n1p1 599M 7.1M 592M 2% /boot/efi
tmpfs 389M 0 389M 0% /run/user/0
/dev/mapper/vg1-lv1 30G 247M 30G 1% /sda1
25.清理
取消挂载
root@localhost ~]# umount /sda1/
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 777M 11M 766M 2% /run
efivarfs 256K 56K 196K 23% /sys/firmware/efi/efivars
/dev/mapper/rhel-root 17G 1.5G 15G 10% /
/dev/nvme0n1p2 960M 191M 770M 20% /boot
/dev/nvme0n1p1 599M 7.1M 592M 2% /boot/efi
tmpfs 389M 0 389M 0% /run/user/0
删除 /sda1 目录
[root@localhost ~]# rm -rf /sda1/
删除逻辑卷
[root@localhost ~]# lvremove /dev/vg1/lv1
Do you really want to remove active logical volume vg1/lv1? [y/n]: y
Logical volume "lv1" successfully removed.
删除卷组
[root@localhost ~]# vgremove vg1
Volume group "vg1" successfully removed
删除物理卷
[root@localhost ~]# pvremove /dev/sda /dev/nvme0n2
Labels on physical volume "/dev/sda" successfully wiped.
Labels on physical volume "/dev/nvme0n2" successfully wiped.
此时就回到增加磁盘的最初状态:
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 20G 0 disk
sr0 11:0 1 11G 0 rom
nvme0n1 259:0 0 20G 0 disk
├─nvme0n1p1 259:1 0 600M 0 part /boot/efi
├─nvme0n1p2 259:2 0 1G 0 part /boot
└─nvme0n1p3 259:3 0 18.4G 0 part
├─rhel-root 253:0 0 16.4G 0 lvm /
└─rhel-swap 253:1 0 2G 0 lvm [SWAP]
nvme0n2 259:4 0 20G 0 disk
权限管理
26.普通权限
权限介绍
[root@localhost ~]# touch a.txt
[root@localhost ~]# ll
total 0
-rw-r--r--. 1 root root 0 Mar 15 19:18 a.txt
- rw- r-- r-- .
1 2 3 4 5
1:表示文件的类型
-:表示普通文件
d:表示目录
l:表示链接文件
b:块设备文件
c:字符设备文件
s:套接字文件
p:管道文件
2:表示文件所有者权限,可以使用字母 u 表示
r:表示可读,可以使用数字 4 表示
w:表示可写,可以使用数字 2 表示
x:表示可执行,可以使用数字 1 表示
-:表示无权限,可以使用数字 0 表示
3:表示文件所属组权限,可以使用字母 g 表示
4:表示其它用户权限,可以使用字母 o 表示
5:表示是否拥有ACL权限
. 表示设置没有 ACL 权限
+ 表示设置了 ACL 权限
a = ugo
对于文件:r 表示可读文件的内容,也就是可以使用 cat 命令查看
w 表示可写,也就是可以使用类似 echo xxx > 文件
x 表示该文件可以被执行 sh xx.sh
对于目录:r表示可以使用 ls 来查看
w 表示d可以在目录中创建或删除文件
x 表示可以使用 cd 命令进入到目录中
27.设置权限
普通权限
chmod [u/g/o/a][+/-][r/w/x/-] 文件目录
chmod [u/g/o/a]=[r/w/x/-] 文件目录
例子
chmod a+x a.txt # a所有人 +增加x执行权限
chmod o+w a.txt # o其他人 +增加w写权限
chmod g-r a.txt # g属组 -去掉r读权限
chmod o-r-x a.txt # o其他人 同时去掉r和x
chmod o=rwx a.txt # o其他人 直接设置权限为rwx(覆盖旧权限)
28.修改所属权限
语法:
chown 所属者[:[所属者]] 文件或目录
将d2目录所属者改为redhat
chown redhat d2
将d2目录的所属者和所属组都改为redhat
chown redhat : d2
将d2目录的所属者所属组都改为root
chown root :d2
将d2目录的所属组改为redhat
chown :redhat d2
29.特殊权限
suid
SUID 权限只能针对文件,该文件必须具有可执行权限。
作用:是将原本没有操作权限的用户执行该文件时临时提升为文件所有者权限,当执行完后提升的权限会被回收
使用 chmod u+s命令
例子
[root@localhost ~]# touch test.sh
[root@localhost ~]# ll test.sh
-rw-r--r--. 1 root root 0 Mar 15 19:45 test.sh
[root@localhost ~]# chmod u+x test.sh
[root@localhost ~]# ll test.sh
-rwxr--r--. 1 root root 0 Mar 15 19:45 test.sh
[root@localhost ~]# chmod u+s test.sh
[root@localhost ~]# ll test.sh
-rwsr--r--. 1 root root 0 Mar 15 19:45 test.sh
[root@localhost ~]# chmod u-s test.sh
[root@localhost ~]# ll test.sh
-rwxr--r--. 1 root root 0 Mar 15 19:45 test.sh
sgid
说明:SGID 权限可以针对文件,也可以针对目录,不管是文件还是目录都必须具备可执行权限。
作用:将原本没有对文件或目录操作的用户临时提升为该文件或目录所属组的权限来执行,执行完后提升的权限会被回收。
实现:使用 chmod g+s命令
例子
# 1. 对于文件
[root@localhost ~]# touch c.txt
[root@localhost ~]# ll c.txt
-rw-r--r--. 1 root root 0 Mar 15 19:52 c.txt
[root@localhost ~]# chmod g+s c.txt
[root@localhost ~]# ll c.txt
-rw-r-Sr--. 1 root root 0 Mar 15 19:52 c.txt
# 2. 对于目录操作
[root@localhost ~]# mkdir d3
[root@localhost ~]# ll -d d3
drwxr-xr-x. 2 root root 6 Mar 15 19:50 d3
[root@localhost ~]# chmod g+s d3
[root@localhost ~]# ll -d d3
drwxr-sr-x. 2 root root 6 Mar 15 19:50 d3
sbit
说明:SBIT权限只针对目录,对文件无效,目录必须具体可执行权限。
作用:当目录具有 SBIT 权限后,除了具体 SBIT 权限的用户和目录所有者外,其他用户不能删除该目录。
实现:使用 chmod o+t命令
示例:
[root@localhost ~]# mkdir d4
[root@localhost ~]# ll -d d4
drwxr-xr-x. 2 root root 6 Mar 15 19:57 d4
[root@localhost ~]# chmod o+t d4
[root@localhost ~]# ll -d d4
drwxr-xr-t. 2 root root 6 Mar 15 19:57 d4
[root@localhost ~]# touch d.txt
[root@localhost ~]# ll d.txt
-rw-r--r--. 1 root root 0 Mar 15 19:58 d.txt
[root@localhost ~]# chmod o+x d.txt
[root@localhost ~]# ll d.txt
-rw-r--r-x. 1 root root 0 Mar 15 19:58 d.txt
[root@localhost ~]# chmod o+t d.txt
[root@localhost ~]# ll d.txt
-rw-r--r-t. 1 root root 0 Mar 15 19:58 d.txt
30.ACL权限
作用:在不改变原来的权限的情况之下,为某个用户或某个组增加指定的权限。
语法:
setfacl [选项] u|g|o:用户名:权限 文件
选项:
-m:设置ACL权限
-x:取消ACL权限
-b:取消所有ACL权限
getfacl [u|g|o:用户名] 文件
例子
[root@localhost ~]# touch e.txt
[root@localhost ~]# ll e.txt
-rw-r--r--. 1 root root 0 Mar 15 20:02 e.txt
# 1. 为e.txt文件设置redhat用户可执行该文件所属者权限
[root@localhost ~]# setfacl -m u:redhat:rwx e.txt
[root@localhost ~]# ll e.txt
-rw-rwxr--+ 1 root root 0 Mar 15 20:02 e.txt
# 2. 为e.txt文件设置redhat用户可执行该文件所属组可读可写权限
[root@localhost ~]# setfacl -m g:redhat:rw- e.txt
[root@localhost ~]# ll e.txt
-rw-rwxr--+ 1 root root 0 Mar 15 20:02 e.txt
# 3. 查看e.txt文件的acl权限
[root@localhost ~]# getfacl u:redhat e.txt
getfacl: u:redhat: No such file or directory
# file: e.txt
# owner: root
# group: root
user::rw-
user:redhat:rwx
group::r--
group:redhat:rw-
mask::rwx
other::r--
# 4. 取消redhat用户的u权限
[root@localhost ~]# setfacl -x u:redhat e.txt
[root@localhost ~]# getfacl u:redhat e.txt
getfacl: u:redhat: No such file or directory
# file: e.txt
# owner: root
# group: root
user::rw-
group::r--
group:redhat:rw-
mask::rw-
other::r--
# 5. 取消e.txt文件的所有acl权限
[root@localhost ~]# setfacl -b e.txt
[root@localhost ~]# getfacl e.txt
# file: e.txt
# owner: root
# group: root
user::rw-
group::r--
other::r--
[root@localhost ~]# ll e.txt
-rw-r--r--. 1 root root 0 Mar 15 20:02 e.txt
31.权限掩码
在 linux 中,创建文件、目录等都有默认的权限。而这个默认的权限数据是写在 /etc/login.defs 文件中的
[root@localhost ~]# vim /etc/login.defs
......
# must make up their mind.
UMASK 022
......
对于权限来说,我们可以使用数字来表示:
- 644:
- 第一个数字表示文件所属者权限,
- 第二个数字表示文件所属组权限,
- 第三个数字表示其它用户权限
- 0644:
-
第一个数字表示特殊权限
- 0:表示没有特殊权限
- 1:表示SBIT权限
- 2:表示SGID权限
- 4:表示SUID权限
-
第二个数字表示文件所属者权限
-
第三个数字表示文件所属组权限
-
第四个数字表示其它用户权限
对于文件的创建,默认的权限为:
bash[root@localhost ~]# touch f.txt [root@localhost ~]# ll f.txt -rw-r--r--. 1 root root 0 Mar 15 20:30 f.txt 文件的默认权限是 644 = (777 - 022 = 755 )因为文件默认没有 x 权限,所以我们需要将 755 - 111 = 644。 对于目录的默认权限: [root@localhost ~]# mkdir d5 [root@localhost ~]# ll -d d5 drwxr-xr-x. 2 root root 6 Mar 15 20:33 d5 目录的默认权限是 755 = 777 - 022 umask 的值是可以修改,我们通过临时修改的方式来改变这个值为 025 [root@localhost ~]# umask 0022 [root@localhost ~]# umask 025 [root@localhost ~]# umask 0025 修改后我们再次创建文件和目录来测试: [root@localhost ~]# touch g.txt [root@localhost ~]# ll g.txt -rw-r---w-. 1 root root 0 Mar 15 20:35 g.txt # 642 = 777 - 025 = 752 - 110 = 642 [root@localhost ~]# mkdir d6 [root@localhost ~]# ll -d d6 drwxr-x-w-. 2 root root 6 Mar 15 20:36 d6 # 752 = 777 - 025
-
32.软件管理
-
rpm 安装
-
dnf 安装
-
源码安装
rpm [选项] 文件
-i :表示安装
-v :表示显示安装过程
-h :表示显示安装进程条
-e :表示删除软件
-update : 更新软件
-q : 查询软件-qa :查询所有安装的软件 -qc : 查询软件的配置文件所在位置 -ql : 查询软件所有文件的所在位置 dnf [选项] 文件 install :安装 remove : 卸载 search : 搜索 provide :查询软件的提供商 repolist :查看仓库列表源码安装例子:
[root@localhost ~]# wget https://nginx.org/download/nginx-1.26.3.tar.gz
--2025-03-15 20:54:23-- https://nginx.org/download/nginx-1.26.3.tar.gz
Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:5c0:2601::6, ...
Connecting to nginx.org (nginx.org)|3.125.197.172|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1260179 (1.2M) [application/octet-stream]
Saving to: 'nginx-1.26.3.tar.gz'nginx-1.26.3.tar.gz 100%[=================================================>] 1.20M 44.7KB/s in 12s
2025-03-15 20:54:37 (99.4 KB/s) - 'nginx-1.26.3.tar.gz' saved [1260179/1260179]
解压安装包
[root@localhost ~]# tar -zxvf nginx-1.26.3.tar.gz
nginx-1.26.3/
nginx-1.26.3/man/
nginx-1.26.3/LICENSE
nginx-1.26.3/configure
nginx-1.26.3/auto/
nginx-1.26.3/CHANGES
nginx-1.26.3/.hgtags
nginx-1.26.3/CHANGES.ru
......进入解压目录
[root@localhost ~]# cd nginx-1.26.3
[root@localhost nginx-1.26.3]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src配置文件
执行解压目录下的 configure 文件来对软件进行编译之前的环境配置
[root@localhost nginx-1.26.3]# ./configure
checking for OS- Linux 5.14.0-503.11.1.el9_5.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found
提示没有 C 语言的编译程序 cc。我们需要安装。
bash[root@localhost nginx-1.26.3]# dnf install gcc gcc-c++ -y ..... Installed: cpp-11.5.0-2.el9.x86_64 gcc-11.5.0-2.el9.x86_64 gcc-c++-11.5.0-2.el9.x86_64 glibc-devel-2.34-125.el9_5.1.x86_64 glibc-headers-2.34-125.el9_5.1.x86_64 kernel-headers-5.14.0-503.11.1.el9_5.x86_64 libmpc-1.2.1-4.el9.x86_64 libpkgconf-1.7.3-10.el9.x86_64 libstdc++-devel-11.5.0-2.el9.x86_64 libxcrypt-devel-4.4.18-3.el9.x86_64 make-1:4.3-8.el9.x86_64 pkgconf-1.7.3-10.el9.x86_64 pkgconf-m4-1.7.3-10.el9.noarch pkgconf-pkg-config-1.7.3-10.el9.x86_64 安装完成后,再次执行配置: [root@localhost nginx-1.26.3]# ./configure checking for OS + Linux 5.14.0-503.11.1.el9_5.x86_64 x86_64 checking for C compiler ... found + using GNU C compiler + gcc version: 11.5.0 20240719 (Red Hat 11.5.0-2) (GCC) checking for gcc -pipe switch ... found checking for -Wl,-E switch ... found checking for gcc builtin atomic operations ... found checking for C99 variadic macros ... found ...... ./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre=<path> option. 提示需要安装 pcre 库 [root@localhost nginx-1.26.3]# dnf install pcre pcre-devel -y ...... Installed products updated. Installed: pcre-cpp-8.44-4.el9.x86_64 pcre-devel-8.44-4.el9.x86_64 pcre-utf16-8.44-4.el9.x86_64 pcre-utf32-8.44-4.el9.x86_64 Complete! 然后再次运行配置: ```bash [root@localhost nginx-1.26.3]# ./configure ...... checking for zlib library ... not found ./configure: error: the HTTP gzip module requires the zlib library. You can either disable the module by using --without-http_gzip_module option, or install the zlib library into the system, or build the zlib library statically from the source with nginx by using --with-zlib=<path> option. 提示还需要 zlib 库,我们安装它: [root@localhost nginx-1.26.3]# dnf install zlib zlib-devel -y ....... Installed products updated. Installed: zlib-devel-1.2.11-40.el9.x86_64 Complete! 最后再进行配置: [root@localhost nginx-1.26.3]# ./configure checking for OS + Linux 5.14.0-503.11.1.el9_5.x86_64 x86_64 checking for C compiler ... found + using GNU C compiler + gcc version: 11.5.0 20240719 (Red Hat 11.5.0-2) (GCC) checking for gcc -pipe switch ... found checking for -Wl,-E switch ... found checking for gcc builtin atomic operations ... found ....... creating objs/Makefile Configuration summary + using system PCRE library + OpenSSL library is not used + using system zlib library nginx path prefix: "/usr/local/nginx" nginx binary file: "/usr/local/nginx/sbin/nginx" nginx modules path: "/usr/local/nginx/modules" nginx configuration prefix: "/usr/local/nginx/conf" nginx configuration file: "/usr/local/nginx/conf/nginx.conf" nginx pid file: "/usr/local/nginx/logs/nginx.pid" nginx error log file: "/usr/local/nginx/logs/error.log" nginx http access log file: "/usr/local/nginx/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp" 编译软件 # 执行make命令 [root@localhost nginx-1.26.3]# make ..... objs/src/http/modules/ngx_http_upstream_zone_module.o \ objs/ngx_modules.o \ -lcrypt -lpcre -lz \ -Wl,-E sed -e "s|%%PREFIX%%|/usr/local/nginx|" \ -e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \ -e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \ -e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \ < man/nginx.8 > objs/nginx.8 make[1]: Leaving directory '/root/nginx-1.26.3' 安装软件 [root@localhost nginx-1.26.3]# make install ..... cp conf/uwsgi_params \ '/usr/local/nginx/conf/uwsgi_params.default' test -f '/usr/local/nginx/conf/scgi_params' \ || cp conf/scgi_params '/usr/local/nginx/conf' cp conf/scgi_params \ '/usr/local/nginx/conf/scgi_params.default' test -f '/usr/local/nginx/conf/nginx.conf' \ || cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf' cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default' test -d '/usr/local/nginx/logs' \ || mkdir -p '/usr/local/nginx/logs' test -d '/usr/local/nginx/logs' \ || mkdir -p '/usr/local/nginx/logs' test -d '/usr/local/nginx/html' \ || cp -R html '/usr/local/nginx' test -d '/usr/local/nginx/logs' \ || mkdir -p '/usr/local/nginx/logs' make[1]: Leaving directory '/root/nginx-1.26.3' 启动 [root@localhost nginx-1.26.3]# /usr/local/nginx/sbin/nginx [root@localhost nginx-1.26.3]# ss -lntup | grep 80 tcp LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=18639,fd=6),("nginx",pid=18638,fd=6)) [root@localhost nginx-1.26.3]# systemctl stop firewalld [root@localhost nginx-1.26.3]# setenforce 0 [root@localhost nginx-1.26.3]# curl 192.168.72.135 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> 停止 ```bash [root@localhost nginx-1.26.3]# /usr/local/nginx/sbin/nginx -s stop [root@localhost nginx-1.26.3]# ss -lntup | grep 80 删除安装目录 [root@localhost nginx-1.26.3]# rm -rf /usr/local/nginx/ [root@localhost nginx-1.26.3]# cd .. [root@localhost ~]# ls nginx-1.26.3 nginx-1.26.3.tar.gz [root@localhost ~]# rm -rf nginx-1.26.3 - Linux 5.14.0-503.11.1.el9_5.x86_64 x86_64