1.常用硬盘管理命令
1.1、df命令
[root@localhost ~
]
参数:
-a 显示特殊文件系统,这些文件系统几乎都是保存在内存中的。如/proc,因为是挂载在内存中,所以占用量都是0
-h 单位不再只用KB,而是换算成习惯单位
-T 多出了文件系统类型一列
1.2、du 命令
[root@localhost ~
]
参数:
-a 显示每个子文件的磁盘占用量。默认只统计子目录的磁盘占用量
-h 使用习惯单位显示磁盘占用量,如KB,MB或GB等
-s 统计总占用量,而不列出子目录和子文件的占用量
[root@localhost ~
]
du与df的区别:du是用于统计文件大小的,统计的文件大小是准确的;df是用于统计空间大小的,统计的剩余空是准确的
1.3、fsck文件系统修复命令
[root@localhost ~
]
1.4、显示磁盘状态dumpe2fs、xfs_growfs
dumpe2fs是centos6以前文件格式为ext2/ext3/ext4文件系统信息命令
xfs_growfs是centos7文件格式为xfs文件系统信息命令
[root@localhost ~
]
[root@localhost ~
]
1.5、查看文件的详细时间
[root@localhost ~
]
文件:
"wh"
大小:4 块:8 IO 块:4096 普通文件
设备:fd00h/64768d Inode:38854513 硬链接:1
权限:
(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、判断文件类型
[root@localhost ~
]
[root@localhost ~
]
2.fdisk命令手工分区
fidsk仅支持MBR分区表,
MPR分区表:支持得最大分区时2TB,最多支持4个主分区,或3个主分区1个扩展分区
(1)查看系统所有硬盘及分区
[root@localhost ~
]
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 ~
]
常用
m:帮助菜单
n:新建分区
d:删除分区
p: 显示分区列表
q:不保存退出
w:保存退出
l:显示已知的文件系统类型。82位Linux swap分区,83为Linux分区,8e为lvm分区
t:改变一个分区的id
(2)新建分区
[root@localhost ~
]
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
分区 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 ~
]
[root@localhost ~
]
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 /
]
[root@localhost /
]
(5)挂载
[root@localhost /
]
[root@localhost /
]
(6)查看
[root@localhost ~
]
文件系统 容量 已用 可用 已用% 挂载点
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 ~
]
/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 ~
]
/dev/mapper/centos-root / xfs defaults 0 0
第一列:设备文件名
第二列挂载点
第三列文件系统
第四列挂载选项
第五列1是否可以被备份 0不备份1每天备份2不定期备份
第六列2是否检测磁盘fsck 0不检测1启动时检测2启动后检测
[root@localhost ~
]
总用量 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
[root@localhost ~
]
/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 ~
]
[root@localhost ~
]
重启后验证
[root@localhost ~
]
[root@localhost ~
]
文件系统 容量 已用 可用 已用% 挂载点
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 ~
]
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 ~
]
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 ~
]
[root@localhost ~
]
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 /
]
6.挂载
[root@localhost /
]
7.查看
[root@localhost ~
]
8.自动挂载
[root@localhost ~
]
/dev/mapper/centos-root / xfs defaults 0 0
第一列:设备文件名
第二列挂载点
第三列文件系统
第四列挂载选项
第五列1是否可以被备份 0不备份1每天备份2不定期备份
第六列2是否检测磁盘fsck 0不检测1启动时检测2启动后检测
[root@localhost ~
]
总用量 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
[root@localhost ~
]
/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 ~
]
[root@localhost ~
]
重启后验证
[root@localhost ~
]
文件系统 容量 已用 可用 已用% 挂载点
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
(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
]
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
]
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
]
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
]
Setting up swapspace version 1, size
= 1060252 KiB
no label, UUID
=b048fdac-5ca7-4507-9f86-928d639bb2e
5.使用swap分区
[root@localhost yum.repos.d
]
total used
free shared buffers cached
Mem: 981M 511M 470M 232K 27M 313M
-/+ buffers/cache: 169M 811M
Swap: 1.9G 0B 1.9G
[root@localhost yum.repos.d
]
[root@localhost yum.repos.d
]
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
[root@localhost yum.repos.d
]
[root@localhost yum.repos.d
]
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