You are on page 1of 31

# linux 基础

## 1、常用命令

### 1.1、sort

sort 将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按 ASCII 码值进行比较,最后


将他们按升序输出。

```shell
-n 按照数字大小排序
-k 按照指定列数排序
-t 指定分隔符来划分列数
-u 在输出行中去除重复行
-r 排序方式改为降序
-o 排序结果输入到源文件
```

```shell
[root@centos-base ~]# cat test.txt
test 30
Hello 95
Linux 85
[root@centos-base ~]# sort test.txt
Hello 95
Linux 85
test 30
[root@centos-base ~]# sort test.txt -o test.txt
[root@centos-base ~]# cat test.txt
Hello 95
Linux 85
test 30
[root@centos-base ~]# sort -k 2nr test.txt
Hello 95
Linux 85
test 30
[root@centos-base ~]# sort -k 2n test.txt
test 30
Linux 85
Hello 95
```

其他选项

-f 将小写字母都转换为大写字母来进行比较,亦即忽略大小写

-b 忽略每一行前面的所有空白部分,从第一个可见字符开始比较。

[详细-k 参数参照](https://www.cnblogs.com/51linux/archive/2012/05/23/2515299.html)

### 1.2、uniq

去除文件重复行
```shell
-i 忽略大小写字符的不同;
-c 进行计数
-u 只显示唯一的行
```

```shell
[root@centos-base ~]# sort test.txt | uniq
Hello 95
Linux 85
test 30
test 30Hello 95
[root@centos-base ~]# sort test.txt | uniq -c
2 Hello 95
3 Linux 85
2 test 30
1 test 30Hello 95
[root@centos-base ~]# sort test.txt | uniq -cu
1 test 30Hello 95
[root@centos-base ~]# sort test.txt | uniq -cui
1 test 30Hello 95
[root@centos-base ~]# cat test.txt
Hello 95
Linux 85
test 30
Hello 95
Linux 85
test 30Hello 95
Linux 85
test 30
```

### 1.3、cut

从一个文本文件或者文本流中提取文本列

```shell
-d :后面接分隔字符。与 -f 一起使用;
-f :依据 -d 的分隔字符将一段信息分割成为数段,用 -f 取出第几段的意思,使用“,”分开可以指定多个列;
-c :以字符 (characters) 的单位取出固定字符区间;
```

```shell
[root@centos-base ~]# cat test.txt

Hello 95 aaaa
Linux 85 11122
test 30 22334
Hello 95 aassdd
Linux 85 bbccdd
test 30Hello 95
Linux 85 fffddd
test 30 fffgghh
[root@centos-base ~]# cat test.txt | cut -d " " -f 3

aaaa
22334

95
fffddd
fffgghh
[root@centos-base ~]# cat test.txt | cut -d " " -f 2

95
85
30
95
85
30Hello
85
30
[root@centos-base ~]# cat test.txt | cut -d " " -f 2-

95 aaaa
85 11122
30 22334
95 aassdd
85 bbccdd
30Hello 95
85 fffddd
30 fffgghh
```

### 1.4、tar

```shell
三种文件类型
-z .tar.gz == tgz
-j .tar.bz2 == tbz、tb2
-J .tar.xz == txz

-c 创建压缩包
-C 指定解压路径
-x 解压文件
-v 显示详情
-t 查看内容

-r 向压缩包末尾追加文件
-u 替换压缩包内指定文件

-f 使用压缩包名称

[root@centos-base ~]# tar -cvzf test.tar.gz 10.4.7.21.txt 10.4.7.22.txt


10.4.7.21.txt
10.4.7.22.txt
[root@centos-base ~]# ls
10.4.7.21.txt anaconda-ks.cfg iftop-1.0-0.21.pre4.el7.x86_64.rpm original-ks.cfg
test.txt
10.4.7.22.txt expect.sh ip.txt test.tar.gz
[root@centos-base ~]# tar -rf test.tar.gz ip.txt
tar: Cannot update compressed archives
tar: Error is not recoverable: exiting now

## tar 包追加、更新只支持不带压缩类型的 tar 包。(即打包时不指定 -z -j -J 选项


小结
1、*.tar 用 tar –xvf 解压
2、*.gz 用 gzip -d 或者 gunzip 解压
3、*.tar.gz 和*.tgz 用 tar –xzf 解压
4、*.bz2 用 bzip2 -d 或者用 bunzip2 解压
5、*.tar.bz2 用 tar –xjf 解压
6、*.Z 用 uncompress 解压
7、*.tar.Z 用 tar –xZf 解压
8、*.rar 用 unrar e 解压
9、*.zip 用 unzip 解压
```

### 1.5、软链接

```shell
ln -s [源文件目录] [目标文件目录]
# 修改软链接
ln -snf [新的源文件目录] [目标文件]
# 删除软链接
rm -rf 目标文件 # 注意:此处末尾加/会删除源文件。 rm -rf 目标文件/

```

### 1.6、硬链接

```shell
ln 源文件 目标文件 # 硬链接不支持目录
```

软硬链接区别

1、软链接以存放另一个文件的路径的形式存在,硬链接以文件副本的形式存在;
2、软链接可以跨不同的文件系统而链接,硬链接不可以;
3、软链接可以对目录进行链接,而硬链接不可以;
4、软链接可以对一个不存在的文件名进行链接,硬链接必须要有源文件。
5、删除软链接并不影响被指向的文件,但若被指向的原文件被删除,则相关软连接就变成了死链接。删除硬链接
的话,只要索引节点的个数不为零,则不会对原始文件造成任何影响;

### 1.7、lsof

```shell
[root@centos-base ~]# lsof -h
lsof 4.87
latest revision: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/
latest FAQ: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/FAQ
latest man page: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/lsof_man
usage: [-?abhKlnNoOPRtUvVX] [+|-c c] [+|-d s] [+D D] [+|-f[gG]] [+|-e s]
[-F [f]] [-g [s]] [-i [i]] [+|-L [l]] [+m [m]] [+|-M] [-o [o]] [-p s]
[+|-r [t]] [-s [p:s]] [-S [t]] [-T [t]] [-u s] [+|-w] [-x [fl]] [--] [names]
Defaults in parentheses; comma-separated set (s) items; dash-separated ranges.
-?|-h list help -a AND selections (OR) -b avoid kernel blocks
-c c cmd c ^c /c/[bix] +c w COMMAND width (9) +d s dir s files
-d s select by FD set +D D dir D tree *SLOW?* +|-e s exempt s *RISKY*
-i select IPv[46] files -K list tasKs (threads) -l list UID numbers
-n no host names -N select NFS files -o list file offset
-O no overhead *RISKY* -P no port names -R list paRent PID
-s list file size -t terse listing -T disable TCP/TPI info
-U select Unix socket -v list version info -V verbose search
+|-w Warnings (+) -X skip TCP&UDP* files -Z Z context [Z]
-- end option scan
+f|-f +filesystem or -file names +|-f[gG] flaGs
-F [f] select fields; -F? for help
+|-L [l] list (+) suppress (-) link counts < l (0 = all; default = 0)
+m [m] use|create mount supplement
+|-M portMap registration (-) -o o o 0t offset digits (8)
-p s exclude(^)|select PIDs -S [t] t second stat timeout (15)
-T qs TCP/TPI Q,St (s) info
-g [s] exclude(^)|select and print process group IDs
-i i select by IPv[46] address: [46][proto][@host|addr][:svc_list|port_list]
+|-r [t[m<fmt>]] repeat every t seconds (15); + until no files, - forever.
An optional suffix to t is m<fmt>; m must separate t from <fmt> and
<fmt> is an strftime(3) format for the marker line.
-s p:s exclude(^)|select protocol (p = TCP|UDP) states by name(s).
-u s exclude(^)|select login|UID set s
-x [fl] cross over +d|+D File systems or symbolic Links
names select named files or files on named file systems
Anyone can list all files; /dev warnings disabled; kernel ID check disabled.

# 常用的参数列表:
lsof filename 显示打开指定文件的所有进程
lsof -a 表示两个参数都必须满足时才显示结果
lsof -c string 显示 COMMAND 列中包含指定字符的进程所有打开的文件
lsof -u username 显示所属 user 进程打开的文件
lsof -g gid 显示归属 gid 的进程情况
lsof +d /DIR/ 显示目录下被进程打开的文件
lsof +D /DIR/ 同上,但是会搜索目录下的所有目录,时间相对较长
lsof -d FD 显示指定文件描述符的进程
lsof -n 不将 IP 转换为 hostname,缺省是不加上-n 参数
lsof -i 用以显示符合条件的进程情况
lsof -i[46] [protocol][@hostname|hostaddr][:service|port]
46 --> IPv4 or IPv6
protocol --> TCP or UDP
hostname --> Internet host name
hostaddr --> IPv4 地址
service --> /etc/service 中的 service name (可以不只一个)
port --> 端口号 (可以不只一个)
```

```shell
[root@centos-base ~]# lsof -i :22,323
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
chronyd 3957 chrony 1u IPv4 24987 0t0 UDP localhost:323
chronyd 3957 chrony 2u IPv6 24988 0t0 UDP localhost:323
sshd 4231 root 3u IPv4 27207 0t0 TCP *:ssh (LISTEN)
sshd 4231 root 4u IPv6 27216 0t0 TCP *:ssh (LISTEN)
sshd 13955 root 3u IPv4 36375 0t0 TCP centos-base.shared:ssh-
>10.4.7.2:49611 (ESTABLISHED)

[root@centos-base ~]# lsof -i udp


COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
chronyd 3957 chrony 1u IPv4 24987 0t0 UDP localhost:323
chronyd 3957 chrony 2u IPv6 24988 0t0 UDP localhost:323
dhclient 3990 root 6u IPv4 26119 0t0 UDP *:bootpc

[root@centos-base ~]# lsof -i :22


COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 4231 root 3u IPv4 27207 0t0 TCP *:ssh (LISTEN)
sshd 4231 root 4u IPv6 27216 0t0 TCP *:ssh (LISTEN)
sshd 13955 root 3u IPv4 36375 0t0 TCP centos-base.shared:ssh-
>10.4.7.2:49611 (ESTABLISHED)
[root@centos-base ~]# lsof /etc/chrony.conf
[root@centos-base ~]# lsof -a -u root -i :22
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 4231 root 3u IPv4 27207 0t0 TCP *:ssh (LISTEN)
sshd 4231 root 4u IPv6 27216 0t0 TCP *:ssh (LISTEN)
sshd 13955 root 3u IPv4 36375 0t0 TCP centos-base.shared:ssh-
>10.4.7.2:49611 (ESTABLISHED)

[root@centos-base ~]# lsof -c chrony


COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
chronyd 3957 chrony cwd DIR 253,0 4096 2 /
chronyd 3957 chrony rtd DIR 253,0 4096 2 /
chronyd 3957 chrony txt REG 253,0 261024 928456
/usr/sbin/chronyd
chronyd 3957 chrony mem REG 253,0 68192 921873
/usr/lib64/libbz2.so.1.0.6
chronyd 3957 chrony mem REG 253,0 157424 921867
/usr/lib64/liblzma.so.5.2.2
chronyd 3957 chrony mem REG 253,0 90248 921139
/usr/lib64/libz.so.1.2.7
chronyd 3957 chrony mem REG 253,0 100008 921882
/usr/lib64/libelf-0.172.so
chronyd 3957 chrony mem REG 253,0 88720 929021
/usr/lib64/libgcc_s-4.8.5-20150702.so.1
....

[root@centos-base ~]# lsof | grep test.txt


vim 24459 root 4u REG 253,0 12288 524304
/root/.test.txt.swp

# 恢复删除的文件
[root@centos-base ~]# lsof | grep message

rsyslogd 4233 root 6w REG 253,0 533247


264907 /var/log/messages
in:imjour 4233 4238 root 6w REG 253,0 533247
264907 /var/log/messages
rs:main 4233 4239 root 6w REG 253,0 533247
264907 /var/log/messages
[root@centos-base ~]# tail /proc/4233/fd/6
May 15 03:59:30 centos-base NetworkManager[3926]: <info> [1652601570.6338] dhcp4
(eth0): domain name 'localdomain'
May 15 03:59:30 centos-base NetworkManager[3926]: <info> [1652601570.6338] dhcp4
(eth0): state changed bound -> bound
May 15 03:59:30 centos-base dbus[3921]: [system] Activating via systemd: service
name='org.freedesktop.nm_dispatcher' unit='dbus-org.freedesktop.nm-
dispatcher.service'
May 15 03:59:30 centos-base systemd: Starting Network Manager Script Dispatcher
Service...
May 15 03:59:30 centos-base dhclient[3990]: bound to 10.4.7.16 -- renewal in 847
seconds.
May 15 03:59:30 centos-base dbus[3921]: [system] Successfully activated service
'org.freedesktop.nm_dispatcher'
May 15 03:59:30 centos-base systemd: Started Network Manager Script Dispatcher
Service.
May 15 03:59:30 centos-base nm-dispatcher: req:1 'dhcp4-change' [eth0]: new request
(3 scripts)
May 15 03:59:30 centos-base nm-dispatcher: req:1 'dhcp4-change' [eth0]: start
running ordered scripts...
May 15 04:01:01 centos-base systemd: Started Session 6 of user root.
[root@centos-base ~]# tail /var/log/messages
May 15 03:59:30 centos-base NetworkManager[3926]: <info> [1652601570.6338] dhcp4
(eth0): domain name 'localdomain'
May 15 03:59:30 centos-base NetworkManager[3926]: <info> [1652601570.6338] dhcp4
(eth0): state changed bound -> bound
May 15 03:59:30 centos-base dbus[3921]: [system] Activating via systemd: service
name='org.freedesktop.nm_dispatcher' unit='dbus-org.freedesktop.nm-
dispatcher.service'
May 15 03:59:30 centos-base systemd: Starting Network Manager Script Dispatcher
Service...
May 15 03:59:30 centos-base dhclient[3990]: bound to 10.4.7.16 -- renewal in 847
seconds.
May 15 03:59:30 centos-base dbus[3921]: [system] Successfully activated service
'org.freedesktop.nm_dispatcher'
May 15 03:59:30 centos-base systemd: Started Network Manager Script Dispatcher
Service.
May 15 03:59:30 centos-base nm-dispatcher: req:1 'dhcp4-change' [eth0]: new request
(3 scripts)
May 15 03:59:30 centos-base nm-dispatcher: req:1 'dhcp4-change' [eth0]: start
running ordered scripts...
May 15 04:01:01 centos-base systemd: Started Session 6 of user root.

[root@centos-base ~]# ll -h /var/log/messages


-rw-------. 1 root root 521K May 15 16:01 /var/log/messages
[root@centos-base ~]# rm -rf /var/log/messages
[root@centos-base ~]# tail /var/log/messages
tail: cannot open ‘/var/log/messages’ for reading: No such file or directory
[root@centos-base ~]# lsof | grep message
rsyslogd 4233 root 6w REG 253,0 533316
264907 /var/log/messages (deleted)
in:imjour 4233 4238 root 6w REG 253,0 533316
264907 /var/log/messages (deleted)
rs:main 4233 4239 root 6w REG 253,0 533316
264907 /var/log/messages (deleted)
[root@centos-base ~]# cat /proc/4233/fd/6 > /var/log/messages
[root@centos-base ~]# ll -h /var/log/messages
-rw-r--r-- 1 root root 521K May 15 16:06 /var/log/messages
```
## 2、网络监控

### 2.1、iftop 工具

[安装包下载地址](https://pkgs.org/download/iftop)

![Snip20220406_1](https://raw.githubusercontent.com/chengxinmu/Figure-bed/main/
linux_base/linux_baseSnip20220406_1-20220423215231470-20220423215420671.png)

```shell
界面上面显示的是类似刻度尺的刻度范围,为显示流量图形的长条作标尺用的。
中间的<= =>这两个左右箭头,表示的是流量的方向。
TX:发送流量
RX:接收流量
TOTAL:总流量
Cumm:运行 iftop 到目前时间的总流量
peak:流量峰值
rates:分别表示过去 2s 10s 40s 的平均流量
```

## 3、tcp 协议

tcp 状态总结

| 状态 | 描述 |
| ----------- | ------------------------------------------------------------ |
| CLOSED | 初始状态,表示 TCP 连接是”关闭着的”或”未打开的”,该状态下无法通信。 |
| LISTEN | 表示服务器端的某个 socket 处于监听状态,可以接受客户端的连接。 |
| SYN_RCVD | 表示服务器接收到了来自客户端请求连接的 SYN 报文。这个状态是在服务端的,但是它是
一个中间状态,很短暂,平常我们用 netstat 或 ss 的时候,不太容易看到这种状态,但是遇到 SYN flood
之类的 SYN 攻击时,会出现大量的这种状态,即收不到三次握手最后一个客户端发来的 ACK,所以一直是这个
状态,不会转换到 `ESTABLISHED` 状态。 |
| SYN_SENT | 这个状态与 `SYN_RCVD` 状态相呼应,它是 TCP 连接客户端的状态,当客户端 socket
执行 connect() 进行连接时,它首先发送 SYN 报文,然后随即进入到 `SYN_SENT` 状态,并等待服务端的
SYN 和 ACK,该状态表示客户端的 SYN 已发送。 |
| ESTABLISHED | 表示 TCP 连接已经成功建立,开始传输数据。 |
| FIN_WAIT_1 | 这个状态在实际工作中很少能看到,当客户端想要主动关闭连接时,它会向服务端发送
FIN 报文,此时 TCP 状态就进入到 `FIN_WAIT_1` 的状态,而当服务端回复 ACK,确认关闭后,则客户端
进入到 `FIN_WAIT_2` 的状态,也就是只有在没有收到服务端 ACK 的情况下,`FIN_WAIT_1` 状态才能看
到,然后长时间收不到 ACK,通常会在默认超时时间 60s(由内核参数 tcp_fin_timeout 控制)后,直接进
入 `CLOSED` 状态。 |
| FIN_WAIT_2 | 这个状态相比较常见,也是需要注意的一个状态,`FIN_WAIT_1` 在接收到服务端 ACK
之后就进入到 `FIN_WAIT_2` 的状态,然后等待服务端发送 FIN,所以在收到对端 FIN 之前,TCP 都会处
于 `FIN_WAIT_2` 的状态,也就是,在主动断开的一端发现大量的 `FIN_WAIT_2` 状态时,需要注意,可能
时网络不稳定或程序中忘记调用连接关闭,`FIN_WAIT_2` 也有超时时间,也是由内核参数
tcp_fin_timeout 控制,当 `FIN_WAIT_2` 状态超时后,连接直接销毁。 |
| CLOSE_WAIT | 表示正在等待关闭,该状态只在被动端出现,即当主动断开的一端调用 close() 后发送
FIN 报文给被动端,被动端必然会回应一个 ACK(这是由 TCP 协议层决定的),这个时候,TCP 连接状态就
进入到 `CLOSE_WAIT` 状态。 |
| LAST_ACT | 当被动关闭的一方在发送 FIN 报文后,等待对方的 ACK 报文的时候,就处于
`LAST_ACK` 的状态,当收到对方的 ACK 之后,就进入到 `CLOSED` 状态了。 |
| TIME_WAIT | 该状态是最常见的状态,主动方在收到对方 FIN 后,就由 `FIN_WAIT_2` 状态进入到
`TIME_WAIT` 状态。 |
| CLOSING | 这个状态是一个比较特殊的状态,也比较少见,正常情况下不会出现,但是当双方同时都作
为主动的一方,调用 close() 关闭连接的时候,两边都进入 `FIN_WAIT_1`的状态,此时期望收到的是 ACK
包,进入 `FIN_WAIT_2` 的状态,但是却先收到了对方的 FIN 包,这个时候,就会进入到 `CLOSING` 的状
态,然后给对方一个 ACK,接收到 ACK 后直接进入到 `CLOSED` 状态。 |

![tcp 状态转换](https://raw.githubusercontent.com/chengxinmu/Figure-bed/main/
linux_base/tcp%E7%8A%B6%E6%80%81%E8%BD%AC%E6%8D%A2.png)

[参考链接](https://getiot.tech/tcpip/tcp-connection-states.html)

[参考链接 2](https://hit-alibaba.github.io/interview/basic/network/TCP.html)

## 4、OSI 七层模型

![OSI 协议参考](https://raw.githubusercontent.com/chengxinmu/Figure-bed/main/
linux_base/OSI%E5%8D%8F%E8%AE%AE%E5%8F%82%E8%80%83.png)

![osi 七层](https://raw.githubusercontent.com/chengxinmu/Figure-bed/main/
linux_base/osi%E4%B8%83%E5%B1%82.jpeg)

**1.报文(message)**
报文包含了应用层的完整的数据信息。

**2.数据段(segment)**
数据段是传输层的信息单元。

**3.数据报(datagram)**

数据报是面向无连接的数据传输。采用数据报方式传输时,被传输的分组称为数据报。如传输层 TCP 的分组叫做


数据段,UDP 的叫做数据报。

还有一种说法是数据报是数据包的分组,一个完整的数据包由一个或多个数据报组成。(待确认)

**4.数据包(packet)**

数据包是网络层传输的数据单元。也称为 IP 包,包中带有足够寻址信息(IP 地址),可独立地从源主机传输到目


的主机
还有一种说法是数据报是网络层的传输基本单位,数据包是 IP 协议中完整的数据单元,由一个或多个数据报组成。
(待确认)

**5.帧(frame)**
帧是数据链路层的传输单元。它将上层传入的数据添加一个头部和尾部,组成了帧,帧根据 MAC 地址寻址。

**6.bit 流(bit)**
bit 是在物理层的介质上直接实现无结构 bit 流传送的。

## 5、磁盘

### 5.1、parted

优势:可划分单个分区大于 2T 的磁盘。主要采用 GPT 分区格式。最多支持 128 主分区。

```shell
parted [选项]… [设备 [命令 [参数]…]…]
```

```shell
选项 描述
-h –help 显示此求助信息
-l –list 列出所有设别的分区信息
-i –interactive 在必要时,提示用户
-s –script 从不提示用户
-v –version 显示版本
```

```shell
parted 交互命令 说 明
check NUMBER 做一次简单的文件系统检测
mklabel,mktable LABEL-TYPE 创建新的磁盘卷标(分区表)
mkfs NUMBER FS-TYPE 在分区上建立文件系统
mkpart PART-TYPE [FS-TYPE] START END 创建一个分区 # 暂时不支持 ext4、xfs 格式创建
move NUMBER START END 移动分区
name NUMBER NAME 给分区命名
print [devices|free|list,all|NUMBER] 显示分区表、活动设备、空闲空间、所有分区
quit 退出
rescue START END 修复丢失的分区
resizepart NUMBER START END 修改分区大小
rm NUMBER 删除分区
select DEVICE 选择需要编辑的设备
set NUMBER FLAG STATE 改变分区标记
toggle [NUMBER [FLAG]] 切换分区表的状态
unit UNIT 设置默认的单位
Version 显示版本

```

```shell
# 指定分区表类型
[root@centos-base ~]# parted -s /dev/sdb mklabel gpt
[root@centos-base ~]# parted /dev/sdb print
Model: ATA CentOS Linux-1 S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags

# 建立分区
[root@centos-base ~]# parted -s /dev/sdb mkpart primary xfs 0% 100%
[root@centos-base ~]# parted /dev/sdb print
Model: ATA CentOS Linux-1 S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags


1 1049kB 10.7GB 10.7GB primary

# 重命名
[root@centos-base ~]# parted -s /dev/sdb name 1 test1
[root@centos-base ~]# parted /dev/sdb print
Model: ATA CentOS Linux-1 S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags


1 1049kB 10.7GB 10.7GB test1

# 删除
[root@centos-base ~]# parted -s /dev/sdb rm 1
[root@centos-base ~]# parted /dev/sdb print
Model: ATA CentOS Linux-1 S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags

# 扩容分区
[root@centos-base ~]# parted -s /dev/sdb mkpart primary xfs 0% 10%
[root@centos-base ~]# parted /dev/sdb print
Model: ATA CentOS Linux-1 S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags


1 1049kB 1074MB 1073MB primary
[root@centos-base ~]# parted -s /dev/sdb resizepart 1 20%
[root@centos-base ~]# parted /dev/sdb print
Model: ATA CentOS Linux-1 S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags


1 1049kB 2147MB 2146MB primary

# 格式化
[root@centos-base ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=131008 blks
= sectsz=4096 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=524032, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=4096 sunit=1 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0

# 挂载
[root@centos-base ~]# mount /dev/sdb1 /data
[root@centos-base ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root ext4 50G 1.6G 46G 4% /
devtmpfs devtmpfs 906M 0 906M 0% /dev
tmpfs tmpfs 917M 0 917M 0% /dev/shm
tmpfs tmpfs 917M 9.0M 908M 1% /run
tmpfs tmpfs 917M 0 917M 0% /sys/fs/cgroup
/dev/sda1 ext4 477M 114M 334M 26% /boot
/dev/mapper/VolGroup-lv_home ext4 12G 41M 11G 1% /home
tmpfs tmpfs 184M 0 184M 0% /run/user/0
/dev/sdb1 xfs 2.0G 33M 2.0G 2% /data

# 扩容文件系统
安装 growpart 工具
# yum -y install cloud-utils-growpart

[root@centos-base ~]# umount /dev/sdb1


[root@centos-base ~]# parted -s /dev/sdb resizepart 1 40%
[root@centos-base ~]# mount /dev/sdb1 /data
[root@centos-base ~]# xfs_growfs /dev/sdb1 #resize2fs 适用 ext4、ext3 格式
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=131008 blks
= sectsz=4096 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=524032, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=4096 sunit=1 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 524032 to 1048320
[root@centos-base ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 50G 1.6G 46G 4% /
devtmpfs 906M 0 906M 0% /dev
tmpfs 917M 0 917M 0% /dev/shm
tmpfs 917M 9.0M 908M 1% /run
tmpfs 917M 0 917M 0% /sys/fs/cgroup
/dev/sda1 477M 114M 334M 26% /boot
/dev/mapper/VolGroup-lv_home 12G 41M 11G 1% /home
tmpfs 184M 0 184M 0% /run/user/0
/dev/sdb1 4.0G 33M 4.0G 1% /data

```

### 5.2、e2label 使用

```shell
[root@centos-base ~]# parted -s /dev/sdb mkpart primary 40% 50%
[root@centos-base ~]# parted -s /dev/sdb print
Model: ATA CentOS Linux-1 S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags


1 1049kB 4295MB 4294MB xfs primary
2 4295MB 5369MB 1074MB primary
[root@centos-base ~]# mkfs.ext4 /dev/sdb2
mke2fs 1.42.9 (28-Dec-2013)
Discarding device blocks: done
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376

Allocating group tables: done


Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
[root@centos-base ~]# e2label /dev/sdb2 disk1
[root@centos-base ~]# e2label /dev/sdb2
disk1
[root@centos-base ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Wed Sep 23 16:17:24 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root / ext4 defaults 1 1
UUID=afc30963-55e3-49ef-8ca1-326d582ad306 /boot ext4 defaults
1 2
/dev/mapper/VolGroup-lv_home /home ext4 defaults 1 2
/dev/mapper/VolGroup-lv_swap swap swap defaults 0 0
LABEL=disk1 /data1 ext4 defaults,noatime,nodiratime,delalloc 0 0
[root@centos-base ~]# mount -a
[root@centos-base ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root ext4 50G 1.6G 46G 4% /
devtmpfs devtmpfs 906M 0 906M 0% /dev
tmpfs tmpfs 917M 0 917M 0% /dev/shm
tmpfs tmpfs 917M 9.0M 908M 1% /run
tmpfs tmpfs 917M 0 917M 0% /sys/fs/cgroup
/dev/sda1 ext4 477M 114M 334M 26% /boot
/dev/mapper/VolGroup-lv_home ext4 12G 41M 11G 1% /home
tmpfs tmpfs 184M 0 184M 0% /run/user/0
/dev/sdb1 xfs 4.0G 33M 4.0G 1% /data
/dev/sdb2 ext4 976M 2.6M 907M 1% /data1
```

### 5.3、交换分区(待补充)

### 5.4、smartctl(待补充)

```shell
[root@centos-base ~]# yum -y install smartmontools

[root@centos-base ~]# smartctl -i /dev/sdb


smartctl 7.0 2018-12-30 r4883 [x86_64-linux-3.10.0-957.el7.x86_64] (local build)
Copyright (C) 2002-18, Bruce Allen, Christian Franke, www.smartmontools.org

=== START OF INFORMATION SECTION ===


Device Model: CentOS Linux-1 SSD
Serial Number: E2DC030BV6H4XB4XK15M
Firmware Version: F.AFEZM4
User Capacity: 10,737,418,240 bytes [10.7 GB]
Sector Sizes: 512 bytes logical, 4096 bytes physical
Rotation Rate: Solid State Device
Device is: Not in smartctl database [for details use: -P showall]
ATA Version is: ATA8-ACS, ATA/ATAPI-5 T13/1321D revision 1
SATA Version is: SATA 2.6, 3.0 Gb/s
Local Time is: Mon Apr 25 04:19:10 2022 EDT
SMART support is: Unavailable - device lacks SMART capability.
```

### 5.5、磁盘读写速度

#### 5.5.1、iostat

iostat 是 I/O statistics(输入/输出统计)的缩写,可以用来统计 CPU、网卡、tty 设备、磁盘、CD-ROM


等等设备的活动情况, 负载信息。
缺点是无法对某个进程深入分析。

```shell
iostat [参数] [时间] [次数]
```

```shell
-C 显示 CPU 使用情况
-d 显示磁盘使用情况
-k 以 KB 为单位显示
-m 以 M 为单位显示
-N 显示磁盘阵列(LVM) 信息
-n 显示 NFS 使用情况
-p [磁盘] 显示磁盘和分区的情况
-t 显示终端和 CPU 的信息
-x 显示详细信息
-V 显示版本信息
```

![iostat-1](https://raw.githubusercontent.com/chengxinmu/Figure-bed/main/
linux_base/iostat-1.png)

**cpu 属性值说明:**

%user:CPU 处在用户模式下的时间百分比。

%nice:CPU 处在带 NICE 值的用户模式下的时间百分比。

%system:CPU 处在系统模式下的时间百分比。

%iowait:CPU 等待输入输出完成时间的百分比。

%steal:管理程序维护另一个虚拟处理器时,虚拟 CPU 的无意识等待时间百分比。

%idle:CPU 空闲时间百分比。

备注:如果%iowait 的值过高,表示硬盘存在 I/O 瓶颈,%idle 值高,表示 CPU 较空闲,如果%idle 值高但系


统响应慢时,有可能是 CPU 等待分配内存,此时应加大内存容量。%idle 值如果持续低于 10,那么系统的 CPU
处理能力相对较低,表明系统中最需要解决的资源是 CPU。

**disk 属性值说明:**

rrqm/s: 每秒进行 merge 的读操作数目。即 rmerge/s

wrqm/s: 每秒进行 merge 的写操作数目。即 wmerge/s

r/s: 每秒完成的读 I/O 设备次数。即 rio/s

w/s: 每秒完成的写 I/O 设备次数。即 wio/s

rsec/s: 每秒读扇区数。即 rsect/s


wsec/s: 每秒写扇区数。即 wsect/s

rkB/s: 每秒读 K 字节数。是 rsect/s 的一半,因为每扇区大小为 512 字节。

wkB/s: 每秒写 K 字节数。是 wsect/s 的一半。

avgrq-sz: 平均每次设备 I/O 操作的数据大小 (扇区)。

avgqu-sz: 平均 I/O 队列长度。

await: 平均每次设备 I/O 操作的等待时间 (毫秒)。

svctm: 平均每次设备 I/O 操作的服务时间 (毫秒)。

%util: 一秒中有百分之多少的时间用于 I/O 操作,即被 io 消耗的 cpu 百分比

备注:如果 %util 接近 100%,说明产生的 I/O 请求太多,I/O 系统已经满负荷,该磁盘可能存在瓶颈。如果


svctm 比较接近 await,说明 I/O 几乎没有等待时间;如果 await 远大于 svctm,说明 I/O 队列太长,
io 响应太慢,则需要进行必要优化。如果 avgqu-sz 比较大,也表示有当量 io 在等待。

[参考链接](https://www.cnblogs.com/wade-luffy/p/6756881.html)

#### 5.5.2、dd

```shell
# 写入测试
[root@centos-base ~]# time dd if=/dev/sdb2 of=/test.disk bs=8k count=2000
2000+0 records in
2000+0 records out
16384000 bytes (16 MB) copied, 0.0156005 s, 1.1 GB/s

real 0m0.017s
user 0m0.001s
sys 0m0.015s

# 读取测试
[root@centos-base ~]# time dd if=/test.disk of=/dev/null bs=8k
2000+0 records in
2000+0 records out
16384000 bytes (16 MB) copied, 0.00417164 s, 3.9 GB/s

real 0m0.005s
user 0m0.000s
sys 0m0.005s

# 读写测试
[root@centos-base ~]# time dd if=/test.disk of=/tmp/test.disk2 bs=8k
2000+0 records in
2000+0 records out
16384000 bytes (16 MB) copied, 0.0108234 s, 1.5 GB/s

real 0m0.012s
user 0m0.001s
sys 0m0.010s
```

#### 5.5.3、iotop

![iotop-1](https://raw.githubusercontent.com/chengxinmu/Figure-bed/main/
linux_base/iotop-1.png)

#### 5.5.4、hdparm

```shell
-t 普通读取
-T 快读
```

```shell
[root@centos-base ~]# hdparm /dev/sdb

/dev/sdb:
multcount = 128 (on)
IO_support = 1 (32-bit)
readonly = 0 (off)
readahead = 8192 (on)
geometry = 1305/255/63, sectors = 20971520, start = 0

[root@centos-base ~]# hdparm -t /dev/sdb

/dev/sdb:
Timing buffered disk reads: 956 MB in 3.00 seconds = 318.39 MB/sec
[root@centos-base ~]# hdparm -T /dev/sdb

/dev/sdb:
Timing cached reads: 27704 MB in 1.99 seconds = 13897.71 MB/sec

```

### 5.6、lvm

PE(物理块) PV(物理卷) VG(卷组) LV(逻辑卷)

缺点:windows 不支持

[参考文章](https://blog.csdn.net/lilygg/article/details/84035315)

#### 5.6.1、常规使用

```shell
# 创建物理卷
[root@centos-base ~]# pvcreate /dev/sdb1 /dev/sdb2
WARNING: xfs signature detected on /dev/sdb1 at offset 0. Wipe it? [y/n]: y
Wiping xfs signature on /dev/sdb1.
WARNING: ext4 signature detected on /dev/sdb2 at offset 1080. Wipe it? [y/n]: y
Wiping ext4 signature on /dev/sdb2.
Physical volume "/dev/sdb1" successfully created.
Physical volume "/dev/sdb2" successfully created.
[root@centos-base ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 VolGroup lvm2 a-- <63.51g 0
/dev/sdb1 lvm2 --- <4.00g <4.00g
/dev/sdb2 lvm2 --- 1.00g 1.00g

# 创建卷组
[root@centos-base ~]# vgcreate vg0 /dev/sdb1 /dev/sdb2
Volume group "vg0" successfully created
[root@centos-base ~]# vgs
VG #PV #LV #SN Attr VSize VFree
VolGroup 1 3 0 wz--n- <63.51g 0
vg0 2 0 0 wz--n- 4.99g 4.99g

# 修改 PE 大小
[root@centos-base ~]# vgchange -s 1 vg0
Volume group "vg0" successfully changed

# 创建逻辑卷
[root@centos-base ~]# lvcreate -l 100 -n lvm0 vg0
Logical volume "lvm0" created.
[root@centos-base ~]# lvcreate -L 100M -n lvm1 vg0
Logical volume "lvm1" created.
[root@centos-base ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync
Convert
lv_home VolGroup -wi-ao---- <11.54g
lv_root VolGroup -wi-ao---- 50.00g
lv_swap VolGroup -wi-ao---- <1.97g
lvm0 vg0 -wi-a----- 100.00m
lvm1 vg0 -wi-a----- 100.00m

# 格式化文件系统
[root@centos-base ~]# mkfs.xfs /dev/vg0/lvm0
meta-data=/dev/vg0/lvm0 isize=512 agcount=4, agsize=6400 blks
= sectsz=4096 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=25600, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=1605, version=2
= sectsz=4096 sunit=1 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@centos-base ~]# mount /dev/vg0/lvm0 /data
[root@centos-base ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root ext4 50G 1.6G 46G 4% /
devtmpfs devtmpfs 906M 0 906M 0% /dev
tmpfs tmpfs 917M 0 917M 0% /dev/shm
tmpfs tmpfs 917M 9.0M 908M 1% /run
tmpfs tmpfs 917M 0 917M 0% /sys/fs/cgroup
/dev/sda1 ext4 477M 114M 334M 26% /boot
/dev/mapper/VolGroup-lv_home ext4 12G 41M 11G 1% /home
tmpfs tmpfs 184M 0 184M 0% /run/user/0
/dev/mapper/vg0-lvm0 xfs 94M 5.3M 89M 6% /data

# 扩大逻辑卷
[root@centos-base ~]# lvextend -L 200M /dev/vg0/lvm0
Size of logical volume vg0/lvm0 changed from 100.00 MiB (100 extents) to 200.00
MiB (200 extents).
Logical volume vg0/lvm0 successfully resized.
[root@centos-base ~]# xfs_growfs /dev/vg0/lvm0
meta-data=/dev/mapper/vg0-lvm0 isize=512 agcount=4, agsize=6400 blks
= sectsz=4096 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=25600, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=1605, version=2
= sectsz=4096 sunit=1 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 25600 to 51200
[root@centos-base ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root ext4 50G 1.6G 46G 4% /
devtmpfs devtmpfs 906M 0 906M 0% /dev
tmpfs tmpfs 917M 0 917M 0% /dev/shm
tmpfs tmpfs 917M 9.0M 908M 1% /run
tmpfs tmpfs 917M 0 917M 0% /sys/fs/cgroup
/dev/sda1 ext4 477M 114M 334M 26% /boot
/dev/mapper/VolGroup-lv_home ext4 12G 41M 11G 1% /home
tmpfs tmpfs 184M 0 184M 0% /run/user/0
/dev/mapper/vg0-lvm0 xfs 194M 5.5M 189M 3% /data
```

#### 5.6.2、xfs 文件系统缩容逻辑卷

```shell
[root@centos-base ~]# yum install -y xfsdump

[root@centos-base ~]# df -Th


Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root ext4 50G 1.6G 46G 4% /
devtmpfs devtmpfs 906M 0 906M 0% /dev
tmpfs tmpfs 917M 0 917M 0% /dev/shm
tmpfs tmpfs 917M 9.0M 908M 1% /run
tmpfs tmpfs 917M 0 917M 0% /sys/fs/cgroup
/dev/sda1 ext4 477M 114M 334M 26% /boot
/dev/mapper/VolGroup-lv_home ext4 12G 41M 11G 1% /home
tmpfs tmpfs 184M 0 184M 0% /run/user/0
/dev/mapper/vg0-lvm0 xfs 194M 5.5M 189M 3% /data

# 备份挂载路径下的文件
[root@centos-base ~]# xfsdump -f /opt/home.dump /data
xfsdump: using file dump (drive_simple) strategy
xfsdump: version 3.1.7 (dump format 3.0) - type ^C for status and control

============================= dump label dialog ==============================

please enter label for this dump session (timeout in 300 sec)
-> test1
session label entered: "test1"

--------------------------------- end dialog ---------------------------------

xfsdump: level 0 dump of centos-base:/data


xfsdump: dump date: Mon Apr 25 07:36:28 2022
xfsdump: session id: 5ed9fa82-d7df-49cc-a1ca-d159109f97a2
xfsdump: session label: "test1"
xfsdump: ino map phase 1: constructing initial dump list
xfsdump: ino map phase 2: skipping (no pruning necessary)
xfsdump: ino map phase 3: skipping (only one dump stream)
xfsdump: ino map construction complete
xfsdump: estimated dump size: 32512 bytes
xfsdump: /var/lib/xfsdump/inventory created

============================= media label dialog =============================

please enter label for media in drive 0 (timeout in 300 sec)


-> test2
media label entered: "test2"

--------------------------------- end dialog ---------------------------------

xfsdump: creating dump session media file 0 (media 0, file 0)


xfsdump: dumping ino map
xfsdump: dumping directories
xfsdump: dumping non-directory files
xfsdump: ending media file
xfsdump: media file size 25464 bytes
xfsdump: dump size (non-dir files) : 1088 bytes
xfsdump: dump complete: 28 seconds elapsed
xfsdump: Dump Summary:
xfsdump: stream 0 /opt/home.dump OK (success)
xfsdump: Dump Status: SUCCESS
[root@centos-base ~]# ls /opt/home.dump
/opt/home.dump
[root@centos-base ~]# umount /data

# 缩容操作
[root@centos-base ~]# lvresize -L 100M /dev/vg0/lvm0
WARNING: Reducing active logical volume to 100.00 MiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg0/lvm0? [y/n]: y
Size of logical volume vg0/lvm0 changed from 200.00 MiB (200 extents) to 100.00
MiB (100 extents).
Logical volume vg0/lvm0 successfully resized.

# 重写文件系统
[root@centos-base ~]# mkfs.xfs -f /dev/vg0/lvm0
meta-data=/dev/vg0/lvm0 isize=512 agcount=4, agsize=6400 blks
= sectsz=4096 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=25600, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=1605, version=2
= sectsz=4096 sunit=1 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@centos-base ~]# mount -a

# 重新导入数据
[root@centos-base ~]# xfsrestore -f /opt/home.dump /data
xfsrestore: using file dump (drive_simple) strategy
xfsrestore: version 3.1.7 (dump format 3.0) - type ^C for status and control
xfsrestore: searching media for dump
xfsrestore: examining media file 0
xfsrestore: dump description:
xfsrestore: hostname: centos-base
xfsrestore: mount point: /data
xfsrestore: volume: /dev/mapper/vg0-lvm0
xfsrestore: session time: Mon Apr 25 07:36:28 2022
xfsrestore: level: 0
xfsrestore: session label: "test1"
xfsrestore: media label: "test2"
xfsrestore: file system id: e5f633a9-809e-4caa-b23f-5b1d83bb21ca
xfsrestore: session id: 5ed9fa82-d7df-49cc-a1ca-d159109f97a2
xfsrestore: media id: f55cb92a-9b99-4293-9492-e199a336f097
xfsrestore: using online session inventory
xfsrestore: searching media for directory dump
xfsrestore: reading directories
xfsrestore: 10 directories and 11 entries processed
xfsrestore: directory post-processing
xfsrestore: restoring non-directory files
xfsrestore: restore complete: 0 seconds elapsed
xfsrestore: Restore Summary:
xfsrestore: stream 0 /opt/home.dump OK (success)
xfsrestore: Restore Status: SUCCESS
[root@centos-base ~]# ls /data
1 2 3 4 5 6 7 8 9
```

#### 5.6.3、ext4 文件系统缩容逻辑卷

```shell
[root@centos-base ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root ext4 50G 1.6G 46G 4% /
devtmpfs devtmpfs 906M 0 906M 0% /dev
tmpfs tmpfs 917M 0 917M 0% /dev/shm
tmpfs tmpfs 917M 9.0M 908M 1% /run
tmpfs tmpfs 917M 0 917M 0% /sys/fs/cgroup
/dev/sda1 ext4 477M 114M 334M 26% /boot
/dev/mapper/VolGroup-lv_home ext4 12G 41M 11G 1% /home
tmpfs tmpfs 184M 0 184M 0% /run/user/0
/dev/mapper/vg0-lvm0 xfs 94M 5.4M 89M 6% /data
/dev/mapper/vg0-lvm1 ext4 93M 1.6M 85M 2% /data1

[root@centos-base data1]# ls /data1


1 2 3 4 5 6 7 8 9 lost+found

[root@centos-base ~]# umount /data1

# 先缩小文件系统
[root@centos-base ~]# e2fsck -f /dev/vg0/lvm1
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg0/lvm1: 22/25688 files (4.5% non-contiguous), 8907/102400 blocks
[root@centos-base ~]# resize2fs /dev/vg0/lvm1 50M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg0/lvm1 to 51200 (1k) blocks.
The filesystem on /dev/vg0/lvm1 is now 51200 blocks long.

[root@centos-base ~]# mount /dev/vg0/lvm1 /data1


[root@centos-base ~]# ls /data1
1 2 3 4 5 6 7 8 9 lost+found
[root@centos-base ~]# ls /data1/1
1.txt
[root@centos-base ~]# cat /data1/1/1.txt
1
```

#### 5.6.4、创建镜像备份

```shell
[root@centos-base ~]# lvcreate -L 10M -n data1_backup -s /dev/mapper/vg0-lvm1
Logical volume "data1_backup" created.
[root@centos-base ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy
%Sync Convert
lv_home VolGroup -wi-ao---- <11.54g
lv_root VolGroup -wi-ao---- 50.00g
lv_swap VolGroup -wi-ao---- <1.97g
data1_backup vg0 swi-a-s--- 10.00m lvm1 0.12
lvm0 vg0 -wi-ao---- 100.00m
lvm1 vg0 owi-aos--- 100.00m
[root@centos-base ~]# cd /data1
[root@centos-base data1]# ls
1 2 3 4 5 6 7 8 9 lost+found
[root@centos-base data1]# rm -rf 1 2 3 4 5
[root@centos-base data1]# ls
6 7 8 9 lost+found
[root@centos-base ~]# umount /data1
[root@centos-base ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy
%Sync Convert
lv_home VolGroup -wi-ao---- <11.54g
lv_root VolGroup -wi-ao---- 50.00g
lv_swap VolGroup -wi-ao---- <1.97g
data1_backup vg0 swi-a-s--- 10.00m lvm1 0.62
lvm0 vg0 -wi-ao---- 100.00m
lvm1 vg0 owi-a-s--- 100.00m
[root@centos-base ~]# mount /dev/vg0/data1_backup /data1
[root@centos-base ~]# ls /data1
1 2 3 4 5 6 7 8 9 lost+found
```

#### 5.6.5、文件系统卷标设置

```shell
[root@centos-base ~]# xfs_admin -L disk1 /dev/vg0/lvm0
writing all SBs
new label = "disk1"

[root@centos-base ~]# xfs_admin -l /dev/vg0/lvm0


label = "disk1"
# 格式化时设置卷标
[root@centos-base ~]# mkfs.xfs -L disk1 -f /dev/vg0/lvm0
```

```shell
# 查看 ext4 文件系统卷标
[root@centos-base ~]# e2label /dev/vg0/lvm1 disk2

[root@centos-base ~]# e2label /dev/vg0/lvm1


disk2

# 格式化时设置卷标
[root@centos-base ~]# mkfs.ext4 -L disk2 -f /dev/vg0/lvm1
```

#### 5.6.6、ext4 逻辑卷根空间扩容

```shell
[root@centos_kvm ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root ext4 16G 1.4G 14G 10% /
devtmpfs devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs tmpfs 3.9G 9.1M 3.9G 1% /run
tmpfs tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda1 ext4 477M 114M 334M 26% /boot
/dev/mapper/VolGroup-lv_home ext4 1.6G 4.8M 1.5G 1% /home
tmpfs tmpfs 782M 0 782M 0% /run/user/0

# 安装 growpart 工具,扩容至根空间对应磁盘
[root@centos_kvm ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 64G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 19.5G 0 part
├─VolGroup-lv_root 253:0 0 16G 0 lvm /
├─VolGroup-lv_swap 253:1 0 2G 0 lvm [SWAP]
└─VolGroup-lv_home 253:2 0 1.6G 0 lvm /home
sr0 11:0 1 128.6M 0 rom
sr1 11:1 1 918M 0 rom

[root@centos_kvm ~]# growpart /dev/sda 2


CHANGED: partition=2 start=1026048 old: size=40916992 end=41943040 new:
size=133191647 end=134217695

[root@centos_kvm ~]# lsblk


NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 64G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 63.5G 0 part
├─VolGroup-lv_root 253:0 0 16G 0 lvm /
├─VolGroup-lv_swap 253:1 0 2G 0 lvm [SWAP]
└─VolGroup-lv_home 253:2 0 1.6G 0 lvm /home
sr0 11:0 1 128.6M 0 rom
sr1 11:1 1 918M 0 rom

# 扩容至物理卷
[root@centos_kvm ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 VolGroup lvm2 a-- <19.51g 0
[root@centos_kvm ~]# pvresize /dev/sda2
Physical volume "/dev/sda2" changed
1 physical volume(s) resized or updated / 0 physical volume(s) not resized
[root@centos_kvm ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 VolGroup lvm2 a-- <63.51g 44.00g
[root@centos_kvm ~]# vgs
VG #PV #LV #SN Attr VSize VFree
VolGroup 1 3 0 wz--n- <63.51g 44.00g

# 扩容至逻辑卷
[root@centos_kvm ~]# lvextend -L 58G /dev/VolGroup/lv_root
Size of logical volume VolGroup/lv_root changed from 15.98 GiB (4091 extents) to
58.00 GiB (14848 extents).
Logical volume VolGroup/lv_root successfully resized.

[root@centos_kvm ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 16G 1.4G 14G 10% /
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 3.9G 9.1M 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda1 477M 114M 334M 26% /boot
/dev/mapper/VolGroup-lv_home 1.6G 4.8M 1.5G 1% /home
tmpfs 782M 0 782M 0% /run/user/0
[root@centos_kvm ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync
Convert
lv_home VolGroup -wi-ao---- <1.56g
lv_root VolGroup -wi-ao---- 58.00g
lv_swap VolGroup -wi-ao---- <1.97g

# 扩容至根空间
[root@centos_kvm ~]# resize2fs /dev/mapper/VolGroup-lv_root
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/mapper/VolGroup-lv_root is mounted on /; on-line resizing
required
old_desc_blocks = 2, new_desc_blocks = 8
The filesystem on /dev/mapper/VolGroup-lv_root is now 15204352 blocks long.

[root@centos_kvm ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 57G 1.4G 54G 3% /
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 3.9G 9.1M 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda1 477M 114M 334M 26% /boot
/dev/mapper/VolGroup-lv_home 1.6G 4.8M 1.5G 1% /home
tmpfs 782M 0 782M 0% /run/user/0
```

## 6、时间同步配置
闰秒配置参照

[红帽闰秒](https://access.redhat.com/zh_CN/articles/1422013#understanding)

<!--当一台服务器和外部时钟源同步时间正常并且此服务器作为时钟源服务器对其他机器提供时钟源服务时,其
他机器可以直接获取到外部时钟源的时间,不论此服务本身时间正确与否。-->

```shell
# 配置文件指定时间同步服务器
[root@centos-base ~]# cat /etc/chrony.conf | grep -Pv "^#|^$"
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
driftfile /var/lib/chrony/drift

# 允许执行立即手动同步
# makestep 1.0 3

rtcsync
allow 10.4.7.0/24

# 即使 server 指令中时间服务器不可用,也允许将本地时间作为标准时间授时给其它客户端
local stratum 10

logdir /var/log/chrony

# 闰秒配置
leapsecmode slew # 客户端服务端都需要添加此行,意为使用 chrony 对闰秒进行 slew 处理
maxslewrate 1000 # 同步步长,当参数设置为 10000 时,大约每 10 分钟可以追平 1 秒钟
smoothtime 400 0.001 leaponly
```

```shell
[root@centos-base ~]# systemctl start chronyd
[root@centos-base ~]# systemctl enable chronyd

[root@centos-base ~]# chronyc sources -v


210 Number of sources = 4

.-- Source mode '^' = server, '=' = peer, '#' = local clock.
/ .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| / '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
|| .- xxxx [ yyyy ] +/- zzzz
|| Reachability register (octal) -. | xxxx = adjusted offset,
|| Log2(Polling interval) --. | | yyyy = measured offset,
|| \ | | zzzz = estimated error.
|| | | \
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^+ ip-186.108.123.103.unima> 2 8 367 143 +55ms[ +52ms] +/- 178ms
^* 202-65-114-202.jogja.cit> 2 8 377 15 -7015us[ -10ms] +/- 74ms
^+ ntp.uii.net.id 2 7 377 15 +1512us[+1512us] +/- 134ms
^+ time4.unima.ac.id 2 8 377 140 +28ms[ +24ms] +/- 97ms
# 客户端配置将 server 指定为服务端地址即可

[root@docker ~]# chronyc tracking


Reference ID : 1FD155F3 (ntp2.lwlcom.net) # 表示本机同步的时间服务器
Stratum : 2
Ref time (UTC) : Mon May 29 19:47:40 2023
System time : 0.001361879 seconds slow of NTP time # 表示本机和 ntp 服务器时差
Last offset : -0.000407948 seconds
RMS offset : 0.002705266 seconds
Frequency : 13.250 ppm slow
Residual freq : -0.126 ppm
Skew : 3.092 ppm
Root delay : 0.042235982 seconds
Root dispersion : 0.002892133 seconds
Update interval : 1031.3 seconds
Leap status : Normal

chronyc -a makestep 立即手工同步


accheck 检查是否对特定主机可访问当前服务器
activity 显示有多少 NTP 源在线/离线
sources [-v] 显示当前时间源的同步信息
sourcestats [-v] 显示当前时间源的同步统计信息
add server 手动添加一台新的 NTP 服务器
clients 报告已访问本服务器的客户端列表
delete 手动移除 NTP 服务器或对等服务器
settime 手动设置守护进程时间
tracking 显示系统时间信息
```

## 7、ssh 转发

[参考](https://zhuanlan.zhihu.com/p/148825449)

| hostname | client | server | jumpserver |


| :------- | --------- | ------------- | ------------- |
| 互联 ip | 10.4.7.26 | 10.4.7.22 | 10.4.7.21 |
| 内网 ip | | 192.168.1.101 | 192.168.1.100 |

```shell
[root@server ~]# cat /etc/ssh/sshd_config | grep -i gateway
GatewayPorts yes

# 此参数相当于 -g 选项,意为允许其他主机访问。不加此参数、不加-g 选项只能本地访问。


```

### 7.1、本地转发(需要在 client 配置)

```shell
[root@client ~]# ssh -L 2002:192.168.1.101:22 -fN 10.4.7.21
[root@client ~]# ssh localhost -p 2002
root@localhost's password:
Last login: Fri Jul 15 14:45:05 2022 from 10.4.7.2

# 只可以在本地使用。其他主机无法通过此端口连接
[root@jumpserver ~]# ssh 10.4.7.26 -p 2002
ssh: connect to host 10.4.7.26 port 2002: Connection refused
```

### 7.2、远程转发(在 server\jumpserver 配置)

```shell
[root@server ~]# ssh -R 2006:192.168.1.101:22 -fN 10.4.7.21

[root@jumpserver ~]# ssh -R 2007:192.168.1.101:22 -fN 10.4.7.21

#client 本地可以登录。其他主机也可以登录

[root@jumpserver ~]# ssh localhost -p 2006


root@localhost's password:
Last login: Fri Jun 2 00:45:32 2023 from jumpserver.192
[root@server ~]#

[root@client ~]# ssh 10.4.7.21 -p 2007


root@10.4.7.21's password:
Last login: Fri Jun 2 00:37:51 2023 from 192.168.1.100
[root@server ~]#

[root@client ~]# ssh 10.4.7.21 -p 2006


root@10.4.7.21's password:
Last login: Fri Jun 2 00:44:31 2023 from 192.168.1.100
[root@server ~]#

```

### 7.3、GatewayPorts 参数不允许修改情况下的转发配置

```shell
# 在客户端如下配置

[root@client ~]# cat .ssh/config


Host jumpserver
Hostname 10.4.7.21
User root
Port 22
Host server
Hostname 127.0.0.1
User root
Port 2006
ProxyCommand ssh -q -x -W %h:%p jumpserver

[root@client ~]# ssh server


root@10.4.7.21's password:
root@127.0.0.1's password:
Last login: Fri Jun 2 01:01:01 2023 from jumpserver.192
[root@server ~]#
```
### 7.4、动态转发

动态转发可以将 ssh 隧道的数据转发到本地的对应端口,配合**SwitchyOmega**等代理工具可以实现 client


端直接访问 server 端的对应服务

```shell
# 以 7.3 配置为前提
[root@client ~]# ssh -D 2003 server
root@10.4.7.21's password:
root@127.0.0.1's password:
Last login: Fri Jun 2 01:01:11 2023 from jumpserver.192
[root@server ~]#
```

![Omega](https://gitee.com/mu_cheng_xin/Figure-bed/raw/master/linux_base/Omega.png)

## 8、expect 用法

准备 2 台主机作为客户端,一台为服务端

| mysql | docker-1 | docker-2 |


| --------- | --------- | --------- |
| 10.4.7.29 | 10.4.7.31 | 10.4.7.32 |

```shell
[root@mysql ~]# cat expect.sh
for i in `cat hostname.txt`
do
/usr/bin/expect << EOF
set timeout 3
spawn ssh root@$i
expect {
"yes/no" { send "yes\r" }
"password" { send "123456\r" }
}
interact
expect "*]*" { send "lsblk >> ${i}.txt\r" }
expect "*]*" { send "lscpu | grep 'Thread(s) per core' >>${i}.txt\r" }
expect "*]*" { send "exit\r" }
expect eof
EOF

/usr/bin/expect << EOF


spawn scp root@$i:/root/${i}.txt .
expect "password" { send "123456\r" }
expect eof
EOF

done

[root@mysql ~]# ls docker*


docker-1.txt docker-2.txt
```

## 9、免密配置
### 9.1、常规情况

```shell
[root@client ~]# ssh-keygen -t rsa -b 2048 -f .ssh/id_rsa -N ''
Generating public/private rsa key pair.
Your identification has been saved in .ssh/id_rsa.
Your public key has been saved in .ssh/id_rsa.pub.
The key fingerprint is:
SHA256:jEADDWFv1+M9O2XdsY5yf6ZJagy1r8/flW0EPifM8sM root@client
The key's randomart image is:
+---[RSA 2048]----+
| ==o |
| . o.. . |
| + . o .. |
| . o + o .= oo|
| . S o.+.B.+|
| .=.+o=o|
| o+ +Eo+|
| .=+o+=|
| .oo==+|
+----[SHA256]-----+

# 公私钥权限需注意,否则无法使用
[root@client ~]# ls -l .ssh/
total 16
-rw-r--r-- 1 root root 173 Jun 2 01:00 config
-rw------- 1 root root 1675 Jun 2 02:43 id_rsa
-rw-r--r-- 1 root root 393 Jun 2 02:43 id_rsa.pub
-rw-r--r-- 1 root root 705 Jun 2 01:00 known_hosts

[root@client ~]# ssh-copy-id root@10.4.7.21


/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed:
"/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out
any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted
now it is to install the new keys
root@10.4.7.21's password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'root@10.4.7.21'"


and check to make sure that only the key(s) you wanted were added.

[root@client ~]# ssh root@10.4.7.21


Last login: Fri Jun 2 01:54:21 2023 from 10.4.7.2
```

### 9.2、ssh-copy-id 不可用

```shell
[root@client ~]# cat .ssh/id_rsa.pub | ssh root@10.4.7.22 "cat - >>
/root/.ssh/authorized_keys"
The authenticity of host '10.4.7.22 (10.4.7.22)' can't be established.
ECDSA key fingerprint is SHA256:LLZmq/kbXzgEbtE0Jn/pKA9Wnewv3GfAW8qlscMJ8RQ.
ECDSA key fingerprint is MD5:08:3e:1a:d4:a3:ca:63:a1:6b:e0:1c:30:d8:74:03:61.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.4.7.22' (ECDSA) to the list of known hosts.
root@10.4.7.22's password:
[root@client ~]# ssh root@10.4.7.22
Last login: Fri Jun 2 01:54:28 2023 from 10.4.7.2
[root@server ~]#
```

### 9.3、ssh 使用的端口非常规 22

```shell
[root@server ~]# cat /etc/ssh/sshd_config | grep Port
Port 33

# -i 指定对应的公钥

[root@client ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.4.7.22 -p 33


/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed:
"/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out
any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted
now it is to install the new keys
root@10.4.7.22's password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh -p '33' 'root@10.4.7.22'"
and check to make sure that only the key(s) you wanted were added.

[root@client ~]# ssh root@10.4.7.22 -p 33


Last login: Fri Jun 2 02:50:09 2023 from 10.4.7.2
[root@server ~]# exit
logout
Connection to 10.4.7.22 closed.

```

### 9.4、根据私钥生成公钥

```shell
[root@client .ssh]# ssh-keygen -y -f id_rsa > id_rsa.pub
```

You might also like