1.df命令查看已挂载磁盘的总容量,使用容量,剩余容量
[root@zjz ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 17930012 2137956 14874604 13% /
tmpfs 502056 0 502056 0% /dev/shm
/dev/sda1 499656 33728 439716 8% /boot
/ /boot 是我们在装系统时划分的 /dev/shm 为内存分区,默认大小是内存大小的一半,如果把文件放到这个分区下,相当于放到了内存中,读写速度快,但是重启系统时会丢失,相当于一个临时存放的
它有很多选项 -i -h -k和-m
-i表示查看inodes的使用状况 ,如果使用100%,即使磁盘空间有剩余,也会提示磁盘空间以满
[root@zjz ~]# df -i |grep -v tmpfs //grep -v 表示过滤掉包含tmpfs字符串的行
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda3 1146880 68479 1078401 6% /
/dev/sda1 32768 38 32730 1% /boot
-h表示使用合适的单位显示
[root@zjz ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 2.1G 15G 13% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 488M 33M 430M 8% /boot
-k -m 分别表示以kb和mb为单位表示
-T 表示显示出分区的文件系统格式显示出来 也可以用mount
[root@zjz ~]# df -T
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/sda3 ext4 17930012 2137964 14874596 13% /
tmpfs tmpfs 502056 0 502056 0% /dev/shm
/dev/sda1 ext4 499656 33728 439716 8% /boot
[root@zjz ~]# mount
/dev/sda3 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
2.du用来查看某个目录或文件所占空间大小,命令为:du -abckmsh 文件或目录名
-a表示全部文件或目录的大小全部列出来,如果后面不加选项和参数,只会列出目录的大小,包含子目录,不指定单位的话,默认以kb显示
[root@zjz ~]# du
72 .
[root@zjz ~]# cd /tmp
[root@zjz tmp]# ls -a
. .. .ICE-unix yum.log
[root@zjz tmp]# touch a.txt
[root@zjz tmp]# ls -a
. .. a.txt .ICE-unix yum.log
[root@zjz tmp]# cd
[root@zjz ~]# du /tmp/a.txt
0 /tmp/a.txt
[root@zjz ~]# du -a /tmp/a.txt
0 /tmp/a.txt
[root@zjz ~]# du -a /tmp
0 /tmp/a.txt
0 /tmp/yum.log
4 /tmp/
-b表示列出的值以B为单位输出
[root@zjz ~]# du -b /tmp
4096 /tmp/.ICE-unix
8192 /tmp
-k表示以kb为单位输出
-m表示以mb为单位输出
-h表示系统自动调节单位
[root@zjz ~]# du -h /etc/passwd
4.0K /etc/passwd
-c表示最后加总
[root@zjz ~]# du -c /etc/passwd
4 /etc/passwd
4 总用量
-s表示只列出总和
[root@zjz ~]# du -s /etc/passwd
4 /etc/passwd
3.磁盘的分区和格式化
增加虚拟磁盘
走默认,然后在指定磁盘容量时,改成5GB 最后重启一下
fdisk命令
fdisk是linux下硬盘的分区工具,只能划分小于2TB的分区
命令为:fdisk -l 设备名称 若不加设备名称,会直接列出系统中所有的磁盘设备以及分区表,加上设备名称,只会列出该设备的分区表
[root@zjz ~]# fdisk -l
Disk /dev/sda: 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: 0x00092baf
Device Boot Start End Blocks Id System
/dev/sda1 * 1 66 524288 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 66 327 2097152 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 327 2611 18349056 83 Linux
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0x00000000
[root@zjz ~]# fdisk -l /dev/sdb //新增的磁盘
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0x00000000
fdisk后面如果不加-l会进入一个特殊的模式,在该模式下可以对磁盘分区进行分区操作
输入m会列出常用命令,具体解释见课本88页
下面先建立第1个分区
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
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)
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-652, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652): +1G
Command (m for help): p
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0xc57945a5
Device Boot Start End Blocks Id System
/dev/sdb1 1 132 1060258+ 83 Linux
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (133-652, default 133):
Using default value 133
Last cylinder, +cylinders or +size{K,M,G} (133-652, default 652): +1G
Command (m for help): p
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0xc57945a5
Device Boot Start End Blocks Id System
/dev/sdb1 1 132 1060258+ 83 Linux
/dev/sdb2 133 264 1060290 83 Linux
Command (m for help): n
Command action
e extended
p primary partition (1-4)
3
Invalid partition number for type `3'
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (265-652, default 265):
Using default value 265
Last cylinder, +cylinders or +size{K,M,G} (265-652, default 652): +1G
Command (m for help): p
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0xc57945a5
Device Boot Start End Blocks Id System
/dev/sdb1 1 132 1060258+ 83 Linux
/dev/sdb2 133 264 1060290 83 Linux
/dev/sdb3 265 396 1060290 83 Linux
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Selected partition 4
First cylinder (397-652, default 397): 4
Value out of range.
First cylinder (397-652, default 397):
Using default value 397
Last cylinder, +cylinders or +size{K,M,G} (397-652, default 652): +1G
Command (m for help): p
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0xc57945a5
Device Boot Start End Blocks Id System
/dev/sdb1 1 132 1060258+ 83 Linux
/dev/sdb2 133 264 1060290 83 Linux
/dev/sdb3 265 396 1060290 83 Linux
/dev/sdb4 397 528 1060290 83 Linux
到此建立了4个分区
要想在建立扩展分区,先删除掉一个分区
Command (m for help): d
Partition number (1-4): 4
Command (m for help): p
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0xc57945a5
Device Boot Start End Blocks Id System
/dev/sdb1 1 132 1060258+ 83 Linux
/dev/sdb2 133 264 1060290 83 Linux
/dev/sdb3 265 396 1060290 83 Linux
Command (m for help): n
Command action
e extended
p primary partition (1-4)
e
Selected partition 4
First cylinder (397-652, default 397):
Using default value 397
Last cylinder, +cylinders or +size{K,M,G} (397-652, default 652): +3G
Value out of range.
Last cylinder, +cylinders or +size{K,M,G} (397-652, default 652):
Using default value 652
Command (m for help): p
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0xc57945a5
Device Boot Start End Blocks Id System
/dev/sdb1 1 132 1060258+ 83 Linux
/dev/sdb2 133 264 1060290 83 Linux
/dev/sdb3 265 396 1060290 83 Linux
/dev/sdb4 397 652 2056320 5 Extended
Command (m for help): n
First cylinder (397-652, default 397):
Using default value 397
Last cylinder, +cylinders or +size{K,M,G} (397-652, default 652): +1G
Command (m for help): P
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0xc57945a5
Device Boot Start End Blocks Id System
/dev/sdb1 1 132 1060258+ 83 Linux
/dev/sdb2 133 264 1060290 83 Linux
/dev/sdb3 265 396 1060290 83 Linux
/dev/sdb4 397 652 2056320 5 Extended
/dev/sdb5 397 528 1060258+ 83 Linux //397-528 这是个逻辑分区 在4分区的下面 ,删除4这个也就没了
Command (m for help): d
Partition number (1-5): 1
Command (m for help): d
Partition number (1-5): 2
Command (m for help): d
Partition number (1-5): 4
Command (m for help): p
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0xc57945a5
Device Boot Start End Blocks Id System
/dev/sdb3 265 396 1060290 83 Linux
Command (m for help): d
Selected partition 3
Command (m for help): 3
3: unknown command
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
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)
Command (m for help): p
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0xc57945a5
Device Boot Start End Blocks Id System
全部删完后 新建逻辑分区
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (1-652, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652): +1G
Command (m for help): p
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0xc57945a5
Device Boot Start End Blocks Id System
/dev/sdb1 1 652 5237158+ 5 Extended
/dev/sdb5 1 132 1060227 83 Linux //从5开始 234是留给主分区的
w保存退出
格式化:命令mke2fs mkfs.ext2 mkfs.ext3 mkfs.ext4 mkfs.xfs
前四种命令是同一个类型,以第一个为例
-b:表示分区时设定每个数据区块所占空间的大小
-i:表示设定inode的大小
-N:设定inode的数量
-c:在格式化前先进行检测磁盘是否有问题
-L:表示预设该分区的标签
-j:表示建立ext3格式的分区。
-t:用来指定文件系统的类型,可以是ext2 3 4
命令:
mke2fs -t ext2 /dev/sdb5 等价于 mkfs.ext2 /dev/sdb5
4.挂载,卸载磁盘
mount 可以查看当前系统已经挂载的所有分区,分区文件系统的类型,挂载点及一些选型等信息。
mount -a 是把所有的磁盘分区挂载上
mount -t 用来指定挂载分区类型
mount -o 用来指定挂载的分区有哪些特征
blkid 可以查看未挂载
这个命令后面可以指定查询哪个分区:blkid /dev/sdb5
mkdir /new
mount /dev/sdb5 /new/
df
/etc/fstab配置文件
[root@zjz ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Apr 3 03:44:39 2018
#
# 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
#
UUID=5836a0d7-474c-4537-afa2-6e098734717b / ext4 defaults 1 1
UUID=dab11e9b-8442-464d-acb9-d8f2a40d6e91 /boot ext4 defaults 1 2
UUID=e6e14f85-a9d1-4b87-bb7b-210eafcf1561 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
第一列是分区的标识 第二列是挂载点 第三列是分区的格式 第四列是mount的一些挂载参数,一般情况下,写defaults即可
第五列表示是否被dump备份,1表示备份,0表示不备份 第六列表示开机时是否自检磁盘,12表示检测,0表示不检测,检测时,先检测1 在检测2
在CentOS7中,所有分区中该列的值都是0