1.常用硬盘管理命令
1.1、df命令
[root@localhost ~]# df [参数] [文件名或目录]
参数:
-a 显示特殊文件系统,这些文件系统几乎都是保存在内存中的。如/proc,因为是挂载在内存中,所以占用量都是0
-h 单位不再只用KB,而是换算成习惯单位
-T 多出了文件系统类型一列
1.2、du 命令
[root@localhost ~]# du [参数] [文件名或目录]
参数:
-a 显示每个子文件的磁盘占用量。默认只统计子目录的磁盘占用量
-h 使用习惯单位显示磁盘占用量,如KB,MB或GB等
-s 统计总占用量,而不列出子目录和子文件的占用量
[root@localhost ~]# du -sh /*
#查看各个文件夹占用大小
du与df的区别:du是用于统计文件大小的,统计的文件大小是准确的;df是用于统计空间大小的,统计的剩余空是准确的
1.3、fsck文件系统修复命令
[root@localhost ~]# fsck -y /dev/sdb1
1.4、显示磁盘状态dumpe2fs、xfs_growfs
dumpe2fs是centos6以前文件格式为ext2/ext3/ext4文件系统信息命令
xfs_growfs是centos7文件格式为xfs文件系统信息命令
[root@localhost ~]# xfs_growfs /dev/sda
[root@localhost ~]# dumpe2fs /dev/sda1
1.5、查看文件的详细时间
[root@localhost ~]# stat wh
文件:"wh"
#文件名
大小:4 块:8 IO 块:4096 普通文件
#文件大小 占用块 系统分区块大小
设备:fd00h/64768d Inode:38854513 硬链接:1
#存放文件的设备inode号 #硬链接数
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
#权限 属主 属组
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2020-08-20 05:19:11.486476457 +0800
最近更改:2020-08-20 05:19:09.475476464 +0800
最近改动:2020-08-20 05:19:09.475476464 +0800
创建时间:-
1.6、判断文件类型
#type查看命令信息,判断命令类型
[root@localhost ~]# type [命令名]
#file查看文件信息,判断文件类型
[root@localhost ~]# file [文件名]
2.fdisk命令手工分区
fidsk仅支持MBR分区表,
MPR分区表:支持得最大分区时2TB,最多支持4个主分区,或3个主分区1个扩展分区
(1)查看系统所有硬盘及分区
#查看系统所有硬盘及分区
[root@localhost ~]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders 有255个磁面;63个扇区;2610个磁柱;每个 cylinder(磁柱)的容量是 8225280 bytes=8225.280 K(约为)=8.225280M(约为)
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00020a21
#进行磁盘分区
[root@localhost ~]# fdisk /dev/sdb
常用
m:帮助菜单
n:新建分区
d:删除分区
p: 显示分区列表
q:不保存退出
w:保存退出
l:显示已知的文件系统类型。82位Linux swap分区,83为Linux分区,8e为lvm分区
t:改变一个分区的id
(2)新建分区
[root@localhost ~]# fdisk /dev/sdb
n:#新建分区
Partition type:
p primary (0 primary, 0 extended, 4 free) #主分区
e extended #扩展分区
#建立主分区
Select (default p): n
分区号 (1-4,默认 1):
起始 扇区 (2048-41943039,默认为 2048):2048
Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039):2048000 #大小等于扇区数/2
分区 1 已设置为 Linux 类型,大小设为 999 MiB
命令(输入 m 获取帮助):p
磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x24a43e78
设备 Boot Start End Blocks Id System
/dev/sdb1 2048 2048000 1022976+ 83 Linux
#建立扩展分区
命令(输入 m 获取帮助):n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): e
分区号 (2-4,默认 2):
起始 扇区 (2048001-41943039,默认为 2050048):
将使用默认值 2050048
Last 扇区, +扇区 or +size{K,M,G} (2050048-41943039,默认为 41943039):
将使用默认值 41943039
分区 2 已设置为 Extended 类型,大小设为 19 GiB
命令(输入 m 获取帮助):p
磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x24a43e78
设备 Boot Start End Blocks Id System
/dev/sdb1 2048 2048000 1022976+ 83 Linux
/dev/sdb2 2050048 41943039 19946496 5 Extended
#建立逻辑分区
命令(输入 m 获取帮助):n
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): l
添加逻辑分区 5
起始 扇区 (20484096-41943039,默认为 20484096):
将使用默认值 20484096
Last 扇区, +扇区 or +size{K,M,G} (20484096-41943039,默认为 41943039):
将使用默认值 41943039
分区 5 已设置为 Linux 类型,大小设为 10.2 GiB
命令(输入 m 获取帮助):p
磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x9664b929
#如下图所示以分区完毕
设备 Boot Start End Blocks Id System
/dev/sdb1 2048 20480000 10238976+ 83 Linux
/dev/sdb2 20482048 41943039 10730496 5 Extended
/dev/sdb5 20484096 41943039 10729472 83 Linux
#保存分区
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盘。
(3)格式化分区
[root@localhost ~]# mkfs -t ext4 /dev/sdb1
[root@localhost ~]# mkfs -t ext4 /dev/sdb5
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
640848 inodes, 2559744 blocks
127987 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2151677952
79 block groups
32768 blocks per group, 32768 fragments per group
8112 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
(4)建立挂载点
[root@localhost /]# mkdir disk1
[root@localhost /]# mkdir disk5
(5)挂载
[root@localhost /]# mount /dev/sdb1 /disk1
[root@localhost /]# mount /dev/sdb5 /disk5
(6)查看
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.8M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgrou
/dev/sdb1 9.5G 37M 9.0G 1% /disk1
/dev/sdb5 10G 41M 9.4G 1% /disk5
[root@localhost ~]# mount
/dev/sdb1 on /disk1 type ext4 (rw,relatime,seclabel,data=ordered)
/dev/sdb5 on /disk5 type ext4 (rw,relatime,seclabel,data=ordered)
3.自动挂载
[root@localhost ~]# vi fatab #注意:此文件直接参与系统启动,如果修改错误,系统启动报错
/dev/mapper/centos-root / xfs defaults 0 0
第一列:设备文件名
第二列挂载点
第三列文件系统
第四列挂载选项
第五列1是否可以被备份 0不备份1每天备份2不定期备份
第六列2是否检测磁盘fsck 0不检测1启动时检测2启动后检测
#查看uuid
[root@localhost ~]# ll /dev/disk/by-uuid/
总用量 0
lrwxrwxrwx. 1 root root 9 9月 9 19:11 2018-11-25-23-54-16-00 -> ../../sr0
lrwxrwxrwx. 1 root root 10 9月 9 19:11 2a25f3ae-d23b-42d2-90c5-ef4090dc64b1 -> ../../sda1
lrwxrwxrwx. 1 root root 10 9月 9 19:38 358d18d8-e1c5-45e3-8e3f-ef46ffedba29 -> ../../sdb1
lrwxrwxrwx. 1 root root 10 9月 9 19:11 a1cf4c80-8689-4fcf-bc0b-e13c0543169f -> ../../dm-1
lrwxrwxrwx. 1 root root 10 9月 9 19:11 ace032c3-c677-41aa-b54a-d792fa8da097 -> ../../dm-0
lrwxrwxrwx. 1 root root 10 9月 9 19:39 d1693f5d-8d81-4ee8-8478-7b4876186666 -> ../../sdb5
#修改fstab
[root@localhost ~]# vi fatab
#
# /etc/fstab
# Created by anaconda on Thu Jul 2 01:15:23 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/centos-root / xfs defaults 0 0
UUID=2a25f3ae-d23b-42d2-90c5-ef4090dc64b1 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0a
UUID=d1693f5d-8d81-4ee8-8478-7b4876186666 /disk5 ext4 defaults 1 2
#验证
[root@localhost ~]#mount -a
[root@localhost ~]#reboot
重启后验证
[root@localhost ~]#df -h
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.8M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/mapper/centos-root 17G 5.1G 12G 30% /
/dev/sda1 1014M 163M 852M 16% /boot
/dev/sdb5 10G 41M 9.4G 1% /disk5
sdb5挂载正确表示自动挂载正常
4./etc/fstab系统文件修复
修改fstab之后重启系统启动不了报如下错误
出现如上错误,输入服务器密码,进入系统内将fstab错误数据进行正确编辑保存,之后进行重新重启
5.parted命令分区
parted支持GPT分区表
GPT分区表:支持最大18EB的分区(1EB=1024PB=1024*1024TB);最多支持128个分区,其中1个未系统保留分区,127个用户自定义分区
注意事项:
使用parted命令时一定要把自动挂载/etc/fstab添加的删掉,添加同一块硬盘要把之前fdisk分区删掉,硬盘分区删掉
parted交互模式
[root@localhost ~]# parted /dev/sdb
GNU Parted 3.1
使用 /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help
1.查看分区
(parted) print
Model: VMware, VMware Virtual S (scsi) >>硬盘参数
Disk /dev/sdb: 21.5GB >>硬盘大小
Sector size (logical/physical): 512B/512B >>扇区大小
Partition Table: msdos >>分区表类型,msdos代表MBR分区
Disk Flags:
Number Start End Size Type File system 标志
1 1049kB 10.5GB 10.5GB primary ext4
2 10.5GB 21.5GB 11.0GB extended
5 10.5GB 21.5GB 11.0GB logical ext4
看到了我们使用fdisk分的区,其中1分区已格式化,2分区是扩展分区不能格式化
使用print可以查看分区表信息,包括硬盘参数,硬盘大小,扇区大小,分区表类型和分区信息。分区信息总共七列,分别是:
◇Number:分区号
◇Start:分区起始位置,这里不再像fdisk用柱面表示,而是使用Byte更加直观
◇End:分区结束位置
◇Size:分区大小
◇Type:分区类型
◇Filesystem:文件系统类型。parted不支持ext4文件系统吗?注意,是说parted不能直接把分区直接格式化成ext4文件系统,但是它是可以识别的。
◇标志:Flags,就是分区的标记
2.修改为GPT分区
(parted) mklabel gpt
警告: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to continue?
是/Yes/否/No? yes
再次查看
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt 》》分区类型已修改为GPT
Disk Flags:
Number Start End Size File system Name 标志
3.建立分区
[root@localhost ~]# parted /dev/sdb
GNU Parted 3.1
使用 /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mkpart
分区名称? []? disk1 #分区名称
文件系统类型? [ext2]? #分区格式
起始点? 1MB #起始大小
结束点? 10GB #结束大小
(parted) print #再次查看
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name 标志
1 1049kB 10.0GB 9999MB disk1 #已分区
4.格式化分区
退出parted交互模式
(parted) quit
格式化整个硬盘
[root@localhost ~]# mkfs -t ext4 /dev/sdb1
[root@localhost ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
610800 inodes, 2441216 blocks
122060 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2151677952
75 block groups
32768 blocks per group, 32768 fragments per group
8144 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
5.建立挂载点
[root@localhost /]# mkdir disk1
6.挂载
[root@localhost /]# mount /dev/sdb1 /disk1
7.查看
[root@localhost ~]# df -h
8.自动挂载
[root@localhost ~]# vi fatab #注意:此文件直接参与系统启动,如果修改错误,系统启动报错
/dev/mapper/centos-root / xfs defaults 0 0
第一列:设备文件名
第二列挂载点
第三列文件系统
第四列挂载选项
第五列1是否可以被备份 0不备份1每天备份2不定期备份
第六列2是否检测磁盘fsck 0不检测1启动时检测2启动后检测
#查看uuid
[root@localhost ~]# ll /dev/disk/by-uuid/
总用量 0
lrwxrwxrwx. 1 root root 9 9月 9 19:11 2018-11-25-23-54-16-00 -> ../../sr0
lrwxrwxrwx. 1 root root 10 9月 9 19:11 2a25f3ae-d23b-42d2-90c5-ef4090dc64b1 -> ../../sda1
lrwxrwxrwx. 1 root root 10 9月 9 19:38 451470d4-2225-4074-9654-b760ad26db67 -> ../../sdb1
lrwxrwxrwx. 1 root root 10 9月 9 19:11 a1cf4c80-8689-4fcf-bc0b-e13c0543169f -> ../../dm-1
lrwxrwxrwx. 1 root root 10 9月 9 19:11 ace032c3-c677-41aa-b54a-d792fa8da097 -> ../../dm-0
#修改fstab
[root@localhost ~]# vi fatab
#
# /etc/fstab
# Created by anaconda on Thu Jul 2 01:15:23 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/centos-root / xfs defaults 0 0
UUID=2a25f3ae-d23b-42d2-90c5-ef4090dc64b1 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0a
UUID=451470d4-2225-4074-9654-b760ad26db67 /disk1 ext4 defaults 1 2
#验证
[root@localhost ~]#mount -a
[root@localhost ~]#reboot
重启后验证
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.8M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/mapper/centos-root 17G 5.1G 12G 30% /
/dev/sdb1 9.1G 37M 8.6G 1% /disk
sdb1挂载正确表示自动挂载正常
9.调整分区大小
只适用于GNU Parted 2.0版本,3.0取消此命令
(parted) resize
WARNING: you are attempting to use parted to operate on (resize) a file system.
parted's file system manipulation code is not as robust as what you'll find in
dedicated, file-system-specific packages like e2fsprogs. We recommend
you use parted only to manipulate partition tables, whenever possible.
Support for performing most operations on most types of file systems
will be removed in an upcoming release.
分区编号? 1
起始点? [1049kB]? 1kb
结束点? [10.0GB]? 5gb
错误: 无法满足分区的所有限制。
错误: Can't have overlapping partitions.
(parted) resize
WARNING: you are attempting to use parted to operate on (resize) a file system.
parted's file system manipulation code is not as robust as what you'll find in
dedicated, file-system-specific packages like e2fsprogs. We recommend
you use parted only to manipulate partition tables, whenever possible.
Support for performing most operations on most types of file systems
will be removed in an upcoming release.
分区编号? 1
起始点? [1049kB]?
结束点? [10.0GB]? 5GB
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name 标志
1 1049kB 5000MB 4999MB ext2 disk1
10.删除分区
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name 标志
1 1049kB 5000MB 4999MB ext2 disk1
2 5001MB 7000MB 2000MB disk2
#删除分区命令 rm
(parted) rm 2
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name 标志
1 1049kB 5000MB 4999MB ext2 disk1
6…swap分区
1.查看swap分区大小
[root@localhost yum.repos.d]# free -h
total used free shared buffers cached
Mem: 981M 510M 471M 232K 27M 313M
-/+ buffers/cache: 169M 812M
Swap: 1.9G 0B 1.9G
2.新建分区
[root@localhost yum.repos.d]# fdisk /dev/sdb
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +1G
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x4f264bb2
Device Boot Start End Blocks Id System
/dev/sdb1 1 132 1060258+ 83 Linux
3.分区,并修改swap分区id
[root@localhost yum.repos.d]# fdisk /dev/sdb
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): t >修改分区
Selected partition 1 >分区id
Hex code (type L to list codes): 82 >swap分区id为82
You cannot change a partition into an extended one or vice versa
Delete it first.
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x4f264bb2
Device Boot Start End Blocks Id System
/dev/sdb1 1 132 1060258+ 82 Linux swap / Solaris
#保存退出
Command (m for help): w
4.格式化swap分区
[root@localhost yum.repos.d]# mkswap /dev/sdb1
Setting up swapspace version 1, size = 1060252 KiB
no label, UUID=b048fdac-5ca7-4507-9f86-928d639bb2e
5.使用swap分区
#使用前查看swap分区大小
[root@localhost yum.repos.d]# free -h
total used free shared buffers cached
Mem: 981M 511M 470M 232K 27M 313M
-/+ buffers/cache: 169M 811M
Swap: 1.9G 0B 1.9G
#分配刚才新建的swap分区
[root@localhost yum.repos.d]# swapon /dev/sdb1
#查看分配后swap分区大小
[root@localhost yum.repos.d]# free -h
total used free shared buffers cached
Mem: 981M 511M 469M 232K 27M 313M
-/+ buffers/cache: 170M 810M
Swap: 2.9G 0B 2.9G
swap自动挂载,开机启动
/dev/sdb /swap swap defaults 0 0
used free shared buffers cached
Mem: 981M 511M 470M 232K 27M 313M
-/+ buffers/cache: 169M 811M
Swap: 1.9G 0B 1.9G
#分配刚才新建的swap分区
[root@localhost yum.repos.d]# swapon /dev/sdb1
#查看分配后swap分区大小
[root@localhost yum.repos.d]# free -h
total used free shared buffers cached
Mem: 981M 511M 469M 232K 27M 313M
-/+ buffers/cache: 170M 810M
Swap: 2.9G 0B 2.9G
swap自动挂载,开机启动
/dev/sdb /swap swap defaults 0 0