1.磁盘分区步骤
fdisk -l ##列出本机磁盘所有基本信息
fdisk /dev/vdb ##对/dev/vdb进行磁盘分区
The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
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 ##修改分区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 ##新建
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p ##主分区
Partition number (1-4, default 1): 1 ##选择存储分区的块数
First sector (2048-20971519, default 2048): ##该分区起始字节
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +100M ##增大分区内容为100M
Partition 1 of type Linux and of size 100 MiB is set
Command (m for help): p ##显示所有分区信息
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x7b09b5b0
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
当磁盘已有3个主分区时,必须先建立扩展分区
Command (m for help): n
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e): ##默认建立扩展分区
Using default response e
Selected partition 4
First sector (821248-20971519, default 821248):
Using default value 821248
Last sector, +sectors or +size{K,M,G} (821248-20971519, default 20971519):
Using default value 20971519
Partition 4 of type Extended and of size 9.6 GiB is set
注:若当退出时出现读写分区错误时,可以先cat /proc/partitions观察是否读取该分区表,若没有命令:partprobe,读取分区表。例如以下情况:
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
2.读取分区信息
可以将分区挂载到某一个文件夹里,但是此时的分区多半是不可挂载的,所以要加上一个分区的管理。代码如下:
fdisk -l ##将磁盘信息详细列出
mount /dev/vdb3 /mnt ##将分区挂载到/mnt
mkfs.xfs /dev/vdb3 ##给/dev/vdb3加上管理器
blkid ##查看电脑实际上正在挂载的信息
mount /dev/vdb3 /mnt ##挂载/dev/vdb3
3.swap分区管理
swapon -s ##查看系统swap分区
建立swap分区:
方法一:
mkswap /dev/vdb1 ##把/dev/vdb1/格式化为swap格式
swapon -a /dev/vdb1 ##激活
vim /etc/fstab ##开机自动激活
/dev/vdb1 swap swap defaults 0 0
方法二:
swapon -s ##查看有无swap分区
fdisk /dev/vdb ##进行分区操作
t 修改分区
选择修改分区块:3
l 查看修改内容:找到linux swap分区编号(82)
输入linux swap 类型编号(82)
wq 退出保存
mkswap /dev/vdb1 ##把/dev/vdb1/格式化为swap格式
swapon -a /dev/vdb1 ##激活
关闭swap分区:
删除/etc/fstab中的swap自动激活条目
swapoff /dev/vdb1 ##关闭激活的swap分区
swapon -s ##查看系统swap分区
swapoff /dev/vdb1 ##关闭激活的swap分区
实验步骤:
swapon -s ##查看有无swap分区
fdisk /dev/vdb ##进行分区操作
t 修改分区
选择修改分区块:5
l 查看修改内容:找到linux swap分区编号
输入linux swap 类型编号
wq 退出保存
如果出现错误警告,无法读取分区表,则使用命令:partprobe自动读取分区表
cat /proc/partitions 查看是否显示swap分区编号
blkid 查看已经挂载使用的分区
4.分区配额
分区配额是对于用户的给某个用户多大的空间来存储数据。例如给student用户分配200M的空间配额。
实验步骤:
mount -o usrquota /dev/vdb6 /mnt
mkfs.xfs /dev/vdb6 ###在这个分区中添加文件管理软件
chmod 777 /mnt
vim /etc/fstab:
dev/vdb2 /mnt xfs defaults,usrquota 0 0
quotaon -uv /dev/vdb6
edquota -u student ##修改hard的值 给用户配额空间
测试:
切换到student用户下:
dd if=/dev/zero of=/mnt/file bs=1M count=21
注:若显示空间已满,则应该在该分区的空间允许下,修改hard的值,增大空间。
否则就会如上图所示。
5.磁盘加密
在linux环境下为了保证磁盘信息的安全性,通常加入磁盘加密功能。
cryptsetup luksFormat /dev/vdb1 ##给设备加密
cryptsetup open /dev/vdb1 westos ##将设备打开到westos容器
mkfs.xfs /dev/mapper/westos ##格式化设备,添加软件管理
mount /dev/mapper/westos /mnt ##将设备挂载到/mnt
touch /mnt/file{1..5}
umount /mnt/ ##卸载
cryptsetup close westos ##关闭容器
cryptsetup open /dev/vdb1 westos ##打开容器
mount /dev/mapper/westos /mnt ##挂载
注:在设置密码时一定要注意回显,看清是否已经设置成功。
加密磁盘开机自动挂载
由于我们的命令只是暂时的,所以如过想要永久生效就需要写道加密配置文件中去。
vim /etc/fstab ##编辑配置开机自动挂载信息文件
/dev/mapper/westos /mnt xfs defaults 0 0
vim /etc/crypttab ##编辑开机自动读取加密设备与对应钥匙位置文件
westos /dev/vdb1 /root/dispass
vim /root/dispass ##编辑对应钥匙文件
redhat2019
chmod 600 /root/dispass ##修改密码文件权限
cryptsetup luksAddKey /dev/vdb1 /root/dispass ##建立钥匙文件与加密信息的关联
重启reboot
查看df命令有无挂载,挂载则成功。