Linux磁盘管理
任务一 查看磁盘或者目录的容量
-
1.1 命令 : df
“df” 查看已挂载磁盘的总容量、使用容量、剩余容量等,可以不加任何参数,默认是按k为单位显示的;[root@localhost ~]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/centos-root 18307072 874472 17432600 5% / devtmpfs 490220 0 490220 0% /dev tmpfs 500664 0 500664 0% /dev/shm tmpfs 500664 6844 493820 2% /run tmpfs 500664 0 500664 0% /sys/fs/cgroup /dev/sda1 508588 127092 381496 25% /boot tmpfs 100136 0 100136 0% /run/user/0
"df" 常用选项有 “-i”、"-h"、"-k"、"-m"等
使用“-i” 查看inodes使用状况;
[root@localhost ~]# df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/centos-root 18317312 23669 18293643 1% /
devtmpfs 122555 386 122169 1% /dev
tmpfs 125166 1 125165 1% /dev/shm
tmpfs 125166 480 124686 1% /run
tmpfs 125166 13 125153 1% /sys/fs/cgroup
/dev/sda1 512000 330 511670 1% /boot
tmpfs 125166 1 125165 1% /run/user/0
“-h” 使用合适的单位显示;
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 18G 854M 17G 5% /
devtmpfs 479M 0 479M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.7M 483M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 497M 125M 373M 25% /boot
tmpfs 98M 0 98M 0% /run/user/0
“-k"和”-m" 分别以K, M 为单位显示;
[root@localhost ~]# df -k
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/centos-root 18307072 874472 17432600 5% /
devtmpfs 490220 0 490220 0% /dev
tmpfs 500664 0 500664 0% /dev/shm
tmpfs 500664 6844 493820 2% /run
tmpfs 500664 0 500664 0% /sys/fs/cgroup
/dev/sda1 508588 127092 381496 25% /boot
tmpfs 100136 0 100136 0% /run/user/0
[root@localhost ~]# df -m
Filesystem 1M-blocks Used Available Use% Mounted on
/dev/mapper/centos-root 17878 854 17025 5% /
devtmpfs 479 0 479 0% /dev
tmpfs 489 0 489 0% /dev/shm
tmpfs 489 7 483 2% /run
tmpfs 489 0 489 0% /sys/fs/cgroup
/dev/sda1 497 125 373 25% /boot
tmpfs 98 0 98 0% /run/user/0
其中显示的内容:第一列是分区的名字,第二列为该分区总共的容量,第三列为已经使用了多少,第四列为还剩下多少,第五列为已经使用百分比,如果这个数值到达90%以上,那么你就应该关注了,磁盘分区满了可不是什么好事情,会引起系统崩溃的。最后一列为挂载点。
-
1.2 命令 : du
“du” 用来查看某个目录或文件所占空间大小;语法 : du [-abckmsh] [文件或者目录名]如果不加任何选项和参数只列出目录(包含子目录)大小
[root@localhost opt]# du centos/ 2503 centos/EFI/BOOT/fonts 6014 centos/EFI/BOOT 6017 centos/EFI 85649 centos/images/pxeboot 91920 centos/images 86004 centos/isolinux 286187 centos/LiveOS 3826375 centos/Packages 13220 centos/repodata 4309750 centos/
“-a” 全部文件与目录大小都列出来,这里通过‘|’管道符只输出前10行数据;
[root@localhost opt]# du -a centos/ | head 1 centos/CentOS_BuildTag 1266 centos/EFI/BOOT/BOOTX64.EFI 1 centos/EFI/BOOT/fonts/TRANS.TBL 2501 centos/EFI/BOOT/fonts/unicode.pf2 2503 centos/EFI/BOOT/fonts 2 centos/EFI/BOOT/grub.cfg 986 centos/EFI/BOOT/grubx64.efi 1254 centos/EFI/BOOT/MokManager.efi 2 centos/EFI/BOOT/TRANS.TBL 6014 centos/EFI/BOOT
如果du不指定单位的话,默认显示单位为K,习惯用 du -sh filename 这样的形式。
“-b” 列出的值以bytes为单位输出;
“-k” 以KB为单位输出,和默认不加任何选项的输出值是一样的;
“-m” 以MB为单位输出;
“-h” 系统自动调节单位;
“-c” 最后加总;
“-s” 只列出总和;[root@localhost opt]# du -b /etc/passwd 1040 /etc/passwd [root@localhost opt]# du -k /etc/passwd 4 /etc/passwd [root@localhost opt]# du -m /etc/passwd 1 /etc/passwd [root@localhost opt]# du -h /etc/passwd 4.0K /etc/passwd [root@localhost opt]# du -c /etc/passwd 4 /etc/passwd 4 total [root@localhost opt]# du -s /etc/passwd 4 /etc/passwd
任务二 磁盘分区格式化
在正式介绍Linux下分区工具之前,需要先给虚拟机添加一块磁盘,以便于我们做后续的实验,
1.先关闭正在运行的Linux系统 init 0.
2.到vmware的Linux虚拟机界面,点 “Edit virtual machine settings”, 点一下左侧靠下面的 “Add…” 按钮。
3.在左侧选中 “Hard Disk” 默认就是这一行,点右下角的 “Next”, 继续点 “Next”.
4.“Virtual disk type” 选择 IDE, 点 “Next”
5.继续点 “Next”, “Disk size” 默认即可,最后点 “Finish”.
-
2.1命令 : fdisk
fdisk 是Linux下硬盘的分区工具,是一个非常实用的命令,但是fdisk只能划分小于2T的分区。语法 : fdisk [-l ] [设备名称] 选项只有一个。“-l” 后边不跟设备名会直接列出系统中所有的磁盘设备以及分区表,加上设备名会列出该设备的分区表
[root@localhost ~]# fdisk -l Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000320d3 Device Boot Start End Blocks Id System /dev/sda1 * 2048 1026047 512000 83 Linux /dev/sda2 1026048 41943039 20458496 8e Linux LVM Disk /dev/mapper/centos-root: 18.8 GB, 18756927488 bytes, 36634624 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes [root@localhost ~]# fdisk -l /dev/sda Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000320d3 Device Boot Start End Blocks Id System /dev/sda1 * 2048 1026047 512000 83 Linux /dev/sda2 1026048 41943039 20458496 8e Linux LVM
“fdisk” 如果不加 “-l” 则进入另一个模式,在该模式下,可以对磁盘进行分区操作;
输入 ‘m’ 会列出常用的命令Command (m for help): m Command action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition g create a new empty GPT partition table G create an IRIX (SGI) partition table l list known partition types m print this menu n add a new partition o create a new empty DOS partition table p print the partition table q quit without saving changes s create a new empty Sun disklabel t change a partition's system id u change display/entry units v verify the partition table w write table to disk and exit x extra functionality (experts only)
“p” 打印当前磁盘的分区情况
Command (m for help): p Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x3318e46e Device Boot Start End Blocks Id System
‘n’ 建立一个新的分区;
Command (m for help): n //提示是要 ‘e’ (扩展分区) 还是 ‘p’ (主分区) Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p //选择是 ‘p’ (主分区), 输入 ‘p’ 回车 Partition number (1-4, default 1): 1 //"1"是第一个分区 First sector (2048-104857599, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599): +5G //分区大小为5G Partition 1 of type Linux and of size 5 GiB is set Command (m for help): p //用 ‘p’ 查看已经多出了一个分区 Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 10485