
Linux磁盘配额
1、什么是逻辑卷?PV?VG?LV?PE?LE?
LVM
磁盘动态的扩缩容
PV:物理卷
VG:卷组
LV:逻辑卷
PE:块的大小 默认4M
LE:块数量
2、在/opt下生成一个大小为500M的文件bigfile
dd if=/dev/zero of=/opt/bigfile bs=50M count=10
3、创建和删除LVM的流程?
1)创建磁盘
2)添加到物理卷
3)创建卷组
4)创建逻辑卷
5)创建文件系统 格式化
6)创建挂载点
7)挂载使用
8)配置开机自挂载
4、创建逻辑卷并挂载使用
pv: 5个,大小分别是300M,1G,200M,3G,2G
pvcreate /dev/sdb{1,2,3,4,5}
vg: 3个卷组,PE=10M,分别是vg01,vg02
vgcreate -s 10M vg01 /dev/sdb{1,2}
lv: 3个逻辑卷,分别是lv01,lv02,lv03,分别加到vg01,vg02
lvcreate -n lv01 -L 500M vg01
lvcreate -n lv02 -L 600M vg02
挂载点: 3个挂载点,分别是/opt/lv1,/tmp/lv2,配置开机自挂载
mkfs.ext4 /dev/vg01/lv01
mkfs.ext3 /dev/vg02/lv02
mkdir -p /opt/lv1 /tmp/lv2
mount /dev/vg01/lv01 /opt/lv1
mount /dev/vg02/lv02 /tmp/lv2
echo "/dev/vg01/lv01 /opt/lv1 ext4 defaults 0 0" >> /etc/fstab
echo "/dev/vg02/lv02 /tmp/lv2 ext3 defaults 0 0" >> /etc/fstab
扩容:将lv01容量增加2G,将lv02容量增加1G
lvextend -L 2G -r /dev/vg01/lv01
lvextend -L 1G -r /dev/vg02/lv02
缩容:将lv01容量减少到1G
Linux文件系统类型:
ext4、xfs
不支持缩容xfs
lvreduce -L -1G -r /dev/vg01/lv01
5、创建一个交换分区,大小2G,配置开机自挂载
交换分区:内存不足的时候缓冲
2种方法:
1) 基于分区
2) 基于文件
基于分区:
分区:/dev/sdc 大小5G
parted /dev/sdc mklabel gpt
parted /dev/sdc mkpart part01 0 2G
/dev/sdc1
mkswap /dev/sdc1
swapon -a
free -h
- 权限复习+高级权限ACL
磁盘的配额
inode ---- 限制用户创建文件的个数
block ---- 限制用户能够使用的磁盘空间的大小
1. 添加一块硬盘,进行热加载
alias scandisk='echo - - - > /sys/class/scsi_host/host0/scan;echo - - - > /sys/class/scsi_host/host1/scan;echo - - - > /sys/class/scsi_host/host2/scan'
scandisk
2. 创建分区,创建了一个1G的分区
[root@centos7 ~]#fdisk /dev/sdb
[root@centos7 ~]#lsblk
sdb 8:16 0 2G 0 disk
└─sdb1 8:17 0 1G 0 part
3. 创建文件系统 格式化
[root@centos7 ~]#mkfs -t xfs /dev/sdb1
[root@centos7 ~]#blkid
4. 检查内核是否支持磁盘配额 quota
[root@centos7 ~]#grep -i quota /boot/config-3.10.0-1160.el7.x86_64
5. 创建挂载点,并进行挂载
[root@centos7 ~]#mkdir /mnt/sdb1
[root@centos7 ~]#mount /dev/sdb1 /mnt/sdb1/
[root@centos7 ~]#df -h | grep sdb1
/dev/sdb1 1014M 33M 982M 4% /mnt/sdb1
6. 查看文件系统是否支持磁盘配额
[root@centos7 ~]#mount | grep sdb1
/dev/sdb1 on /mnt/sdb1 type xfs (rw,relatime,attr2,inode64,noquota)
7. 配置文件系统支持磁盘配额
[root@centos7 ~]#umount /dev/sdb1
[root@centos7 ~]#mount -o uquota,gquota /dev/sdb1 /mnt/sdb1/
[root@centos7 ~]#mount | grep sdb1
/dev/sdb1 on /mnt/sdb1 type xfs (rw,relatime,attr2,inode64,usrquota,grpquota)
8. 配置开机自挂载
[root@centos7 ~]#echo "/dev/sdb1 /mnt/sdb1 xfs defaults,uquota,gquota 0 0" >> /etc/fstab
[root@centos7 ~]#tail -1 /etc/fstab
/dev/sdb1 /mnt/sdb1 xfs defaults,uquota,gquota 0 0
9. 创建用户并设置
[root@centos7 ~]#useradd quota1
[root@centos7 ~]#useradd quota2
[root@centos7 ~]#useradd quota3
[root@centos7 ~]#echo 1 | passwd --stdin quota1
[root@centos7 ~]#echo 1 | passwd --stdin quota2
[root@centos7 ~]#echo 1 | passwd --stdin quota3
10. 查看 /mnt/sdb1 目录磁盘配额的信息
[root@centos7 ~]#xfs_quota -x -c 'report -uibh' /mnt/sdb1/
User quota on /mnt/sdb1 (/dev/sdb1)
Blocks Inodes
User ID Used Soft Hard Warn/Grace Used Soft Hard Warn/Grace
---------- --------------------------------- ---------------------------------
root 0 0 0 00 [------] 3 0 0 00 [------]
11. 限制quota1用户只能在/mnt/sdb1目录内创建10个文件
[root@centos7 ~]#xfs_quota -x -c 'limit -u isoft=15 ihard=20 quota1' /mnt/sdb1/
12. 查看设置的配额
[root@centos7 ~]#xfs_quota -x -c 'report -ubih' /mnt/sdb1/
User quota on /mnt/sdb1 (/dev/sdb1)
Blocks Inodes
User ID Used Soft Hard Warn/Grace Used Soft Hard Warn/Grace
---------- --------------------------------- ---------------------------------
root 0 0 0 00 [------] 3 0 0 00 [------]
quota1 0 0 0 00 [------] 0 15 20 00 [------]
13. 测试配额
[root@centos7 ~]#chmod 777 /mnt/sdb1/
[root@centos7 ~]#chmod o+t /mnt/sdb1/
[root@centos7 ~]#su - quota1
[quota1@centos7 ~]$ cd /mnt/sdb1/
创建:
[quota1@centos7 /mnt/sdb1]$ touch f{1..15}
查看:
[root@centos7 ~]#xfs_quota -x -c 'report -ubih' /mnt/sdb1/
[quota1@centos7 /mnt/sdb1]$touch demo
touch: 无法创建"demo": 超出磁盘限额
针对用户
- 针对用户使用磁盘的容量进行设置
[root@centos7 ~]#xfs_quota -x -c 'limit -u bsoft=100M bhard=150M quota2' /mnt/sdb1/
- 查看磁盘配额信息
[root@centos7 ~]#xfs_quota -x -c 'report -ubih' /mnt/sdb1/
- 测试配额
[root@centos7 ~]#su - quota2
[quota2@centos7 ~]$cd /mnt/sdb1/
[quota2@centos7 /mnt/sdb1]$dd if=/dev/zero of=./quota2file bs=101M count=1
[root@centos7 ~]#xfs_quota -x -c 'report -ubih' /mnt/sdb1/
[quota2@centos7 /mnt/sdb1]$dd if=/dev/zero of=./quota2file2 bs=49M count=1
[root@centos7 ~]#xfs_quota -x -c 'report -ubih' /mnt/sdb1/
[quota2@centos7 /mnt/sdb1]$dd if=/dev/zero of=./quota2file3 bs=1M count=1
dd: 打开"./quota2file3" 失败: 超出磁盘限额
针对组
1. 针对组设置磁盘配置之容量大小,先确认用户
[root@centos7 ~]#tail -3 /etc/passwd
quota1:x:1000:1000::/home/quota1:/bin/bash
quota2:x:1001:1001::/home/quota2:/bin/bash
quota3:x:1002:1002::/home/quota3:/bin/bash
2. 设置用户组的磁盘配额
[root@centos7 ~]#xfs_quota -x -c 'limit -g bsoft=200M bhard=300M quota1' /mnt/sdb1/
3. 查看磁盘配额信息
[root@centos7 ~]#xfs_quota -x -c 'report -gbih' /mnt/sdb1/
4. 测试磁盘配额
[quota1@centos7 /mnt/sdb1]$dd if=/dev/zero of=./g1 bs=300M count=1
[quota1@centos7 /mnt/sdb1]$ll -h g1
-rw-rw-r-- 1 quota1 quota1 300M 7月 26 11:12 g1
[root@centos7 ~]#xfs_quota -x -c 'report -gbih' /mnt/sdb1/
[quota1@centos7 /mnt/sdb1]$dd if=/dev/zero of=./g2 bs=1M count=1
dd: 打开"./g2" 失败: 超出磁盘限额
5. 普通用户进行磁盘配额
[root@centos7 ~]#su - quota3
[quota3@centos7 ~]$cd /mnt/sdb1/
[quota3@centos7 /mnt/sdb1]$dd if=/dev/zero of=./g2 bs=301M count=1
记录了1+0 的读入
记录了1+0 的写出
315621376字节(316 MB)已复制,5.1011 秒,61.9 MB/秒
[quota3@centos7 /mnt/sdb1]$ll -h g2
-rw-rw-r-- 1 quota3 quota3 301M 7月 26 11:13 g2
[quota3@centos7 /mnt/sdb1]$rm -rf g2
[root@centos7 ~]#ll -d /mnt/sdb1/
drwxrwxrwt 2 root root 53 7月 26 11:13 /mnt/sdb1/
[root@centos7 ~]#rm -rf /mnt/sdb1/*
6. 将 quota3用户加入到quota1组中
[root@centos7 ~]#cat /etc/group | grep quota1
quota1:x:1000:
[root@centos7 ~]#usermod -aG quota1 quota3
[root@centos7 ~]#cat /etc/group | grep quota1
quota1:x:1000:quota3
7. 测试组中成员是否可以进行磁盘配额限制
[root@centos7 ~]#su - quota3
上一次登录:三 7月 26 11:13:17 CST 2023pts/0 上
[quota3@centos7 ~]$cd /mnt/sdb1/
[quota3@centos7 /mnt/sdb1]$ll
总用量 460800
-rw-rw-r-- 1 quota1 quota1 314572800 7月 26 11:12 g1
-rw-rw-r-- 1 quota2 quota2 105906176 7月 26 11:04 quota2file
-rw-rw-r-- 1 quota2 quota2 51380224 7月 26 11:06 quota2file2
[quota3@centos7 /mnt/sdb1]$ll -h
总用量 450M
-rw-rw-r-- 1 quota1 quota1 300M 7月 26 11:12 g1
-rw-rw-r-- 1 quota2 quota2 101M 7月 26 11:04 quota2file
-rw-rw-r-- 1 quota2 quota2 49M 7月 26 11:06 quota2file2
[quota3@centos7 /mnt/sdb1]$ls
[quota3@centos7 /mnt/sdb1]$ll
总用量 0
[quota3@centos7 /mnt/sdb1]$dd if=/dev/zero of=./g3 bs=300M count=1
记录了1+0 的读入
记录了1+0 的写出
314572800字节(315 MB)已复制,0.51346 秒,613 MB/秒
[quota3@centos7 /mnt/sdb1]$ll -h g3
-rw-rw-r-- 1 quota3 quota3 300M 7月 26 11:17 g3
[quota3@centos7 /mnt/sdb1]$dd if=/dev/zero of=./g4 bs=1M count=1
记录了1+0 的读入
记录了1+0 的写出
1048576字节(1.0 MB)已复制,0.00232703 秒,451 MB/秒
[quota3@centos7 /mnt/sdb1]$ll -h
总用量 301M
-rw-rw-r-- 1 quota3 quota3 300M 7月 26 11:17 g3
-rw-rw-r-- 1 quota3 quota3 1.0M 7月 26 11:18 g4
- 解决方法
[root@centos7 ~]#chmod o-t /mnt/sdb1/
[root@centos7 ~]#ll -d /mnt/sdb1/
drwxrwxrwx 2 root root 6 7月 26 11:19 /mnt/sdb1/
[quota3@centos7 /mnt/sdb1]$dd if=/dev/zero of=./g3 bs=300M count=1
记录了1+0 的读入
记录了1+0 的写出
314572800字节(315 MB)已复制,0.400394 秒,786 MB/秒
[quota3@centos7 /mnt/sdb1]$ll -h
总用量 300M
-rw-rw-r-- 1 quota3 quota3 300M 7月 26 11:19 g3
[quota3@centos7 /mnt/sdb1]$dd if=/dev/zero of=./g4 bs=1M count=1
记录了1+0 的读入
记录了1+0 的写出
1048576字节(1.0 MB)已复制,0.00248262 秒,422 MB/秒
[quota3@centos7 /mnt/sdb1]$ll -h
总用量 301M
-rw-rw-r-- 1 quota3 quota3 300M 7月 26 11:19 g3
-rw-rw-r-- 1 quota3 quota3 1.0M 7月 26 11:20 g4
[root@centos7 ~]#chgrp quota1 /mnt/sdb1/
[root@centos7 ~]#ll -d /mnt/sdb1/
drwxrwxrwx 2 root quota1 26 7月 26 11:20 /mnt/sdb1/
[root@centos7 ~]#chmod g+s /mnt/sdb1/
[root@centos7 ~]#ll -d /mnt/sdb1/
drwxrwsrwx 2 root quota1 26 7月 26 11:20 /mnt/sdb1/
[quota3@centos7 /mnt/sdb1]$rm -rf *
[quota3@centos7 /mnt/sdb1]$dd if=/dev/zero of=./g3 bs=300M count=1
记录了1+0 的读入
记录了1+0 的写出
314572800字节(315 MB)已复制,0.680313 秒,462 MB/秒
[quota3@centos7 /mnt/sdb1]$dd if=/dev/zero of=./g4 bs=1M count=1
dd: 打开"./g4" 失败: 超出磁盘限额
8. 验证配额信息
[root@centos7 ~]#xfs_quota -x -c 'report -gbih' /mnt/sdb1/
Group quota on /mnt/sdb1 (/dev/sdb1)
Blocks Inodes
Group ID Used Soft Hard Warn/Grace Used Soft Hard Warn/Grace
---------- --------------------------------- ---------------------------------
root 0 0 0 00 [------] 2 0 0 00 [------]
quota1 300M 200M 300M 00 [6 days] 2 0 0 00 [------]
项目配额
1. 开启项目配额
[root@centos7 ~]#umount /dev/sdb1
[root@centos7 ~]#mount -o usrquota,prjquota,grpquota /dev/sdb1 /mnt/sdb1
[root@centos7 ~]#df -h | grep sdb1
/dev/sdb1 1014M 33M 982M 4% /mnt/sdb1
[root@centos7 ~]#mount | grep sdb1
/dev/sdb1 on /mnt/sdb1 type xfs (rw,relatime,attr2,inode64,usrquota,prjquota,grpquota)
2. 设置目录,项目名称及项目ID
设置项目ID和具体的目录一一对应
[root@centos7 ~]#echo 1:/mnt/sdb1 >> /etc/projects
[root@centos7 ~]#cat /etc/projects
1:/mnt/sdb1
设置项目的名称和项目ID一一对应
[root@centos7 ~]#echo aa:1 >> /etc/projid
[root@centos7 ~]#cat /etc/projid
aa:1
3. 初始化项目
[root@centos7 ~]#xfs_quota -x -c "project -s aa"
Setting up project aa (path /mnt/sdb1)...
Processed 1 (/etc/projects and cmdline) paths for project aa with recursion depth infinite (-1).
Setting up project aa (path /mnt/sdb1)...
Processed 1 (/etc/projects and cmdline) paths for project aa with recursion depth infinite (-1).
Setting up project aa (path /mnt/sdb1)...
Processed 1 (/etc/projects and cmdline) paths for project aa with recursion depth infinite (-1).
[root@centos7 ~]#xfs_quota -x -c "report -pbih" /mnt/sdb1/
Project quota on /mnt/sdb1 (/dev/sdb1)
Blocks Inodes
Project ID Used Soft Hard Warn/Grace Used Soft Hard Warn/Grace
---------- --------------------------------- ---------------------------------
#0 0 0 0 00 [------] 2 0 0 00 [------]
aa 0 0 0 00 [------] 1 0 0 00 [------]
4. 对项目设置磁盘配额
[root@centos7 ~]#xfs_quota -x -c "limit bsoft=100M bhard=200M -p aa" /mnt/sdb1/
[root@centos7 ~]#xfs_quota -x -c "report -pbih" /mnt/sdb1/
Project quota on /mnt/sdb1 (/dev/sdb1)
Blocks Inodes
Project ID Used Soft Hard Warn/Grace Used Soft Hard Warn/Grace
---------- --------------------------------- ---------------------------------
#0 0 0 0 00 [------] 2 0 0 00 [------]
aa 0 100M 200M 00 [------] 1 0 0 00 [------]
5. 磁盘配额的测试
[root@centos7 ~]#cd /mnt/sdb1/
[root@centos7 /mnt/sdb1]#dd if=/dev/zero of=./f1 bs=200M count=1
记录了1+0 的读入
记录了1+0 的写出
209715200字节(210 MB)已复制,0.293586 秒,714 MB/秒
[root@centos7 /mnt/sdb1]#ll -h
总用量 200M
-rw-r--r-- 1 root quota1 200M 7月 26 14:36 f1
[root@centos7 /mnt/sdb1]#xfs_quota -x -c "report -pbih" /mnt/sdb1/
Project quota on /mnt/sdb1 (/dev/sdb1)
Blocks Inodes
Project ID Used Soft Hard Warn/Grace Used Soft Hard Warn/Grace
---------- --------------------------------- ---------------------------------
#0 0 0 0 00 [------] 2 0 0 00 [------]
aa 200M 100M 200M 00 [------] 2 0 0 00 [------]
[root@centos7 /mnt/sdb1]#dd if=/dev/zero of=./f2 bs=1M count=1
dd: 打开"./f2" 失败: 超出磁盘限额
1)检查quota状态
[root@centos7 ~]#xfs_quota -x -c "state" /mnt/sdb1/
User quota state on /mnt/sdb1 (/dev/sdb1)
Accounting: ON
Enforcement: ON
Inode: #67 (2 blocks, 2 extents)
Group quota state on /mnt/sdb1 (/dev/sdb1)
Accounting: ON
Enforcement: ON
Inode: #68 (2 blocks, 2 extents)
Project quota state on /mnt/sdb1 (/dev/sdb1)
Accounting: ON
Enforcement: ON
Inode: #68 (2 blocks, 2 extents)
Blocks grace time: [7 days]
Inodes grace time: [7 days]
Realtime Blocks grace time: [7 days]
2)临时禁用
[root@centos7 ~]#xfs_quota -x -c "disable -up" /mnt/sdb1
3)临时启用
[root@centos7 ~]#xfs_quota -x -c "enable -up" /mnt/sdb1
4)完全关闭,完全删除
[root@centos7 ~]#xfs_quota -x -c "off -ugp" /mnt/sdb1/
[root@centos7 ~]#xfs_quota -x -c "report -ubih" /mnt/sdb1/
[root@centos7 ~]#xfs_quota -x -c "report -gbih" /mnt/sdb1/
[root@centos7 ~]#xfs_quota -x -c "report -pbih" /mnt/sdb1/
权限相关
三种用户:
-
u 用户,文件的所有者
-
g 组、属组、用户组
-
o 其他人、陌生人
-
a 包括用户、组和其他人
三种权限:
-
r read 4
-
w write 2
-
x excute 1
-
- 无权限
初始权限
-
对于root用户, umask数值默认是022
-
对于普通用户, umask数值默认是002
-
创建的文本文件默认权限:664
-
创建的文件夹默认权限:775
- 感谢你赐予我前进的力量
赞赏者名单
因为你们的支持让我意识到写文章的价值🙏
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 梦缘羲
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果