若是在Linux中搭建了FTP服务器,为了安全性,就要考虑磁盘配额,以防服务器磁盘空间被恶意占满。


磁盘配额概述

1.作用范围:只在指定的分区有效。

2.限制对象:主要针对用户、组进行限制,对组账号限制,组内所有用户的使用总和不能超过限制。

3.限制类型:磁盘容量限制(Block),默认单位KB、文件数量限制(Inode)。

4.限制方法:软限制、硬限制。软限制默认7天内允许超过,会有警告。硬限制不允许超过,硬限制应当比软限制大,否者软限制失效。


磁盘配额管理

XFS 文件系统

XFS文件系统通过xfs_quota工具进行管理。

查看xfsprogs软件包的安装情况,列表查看xfsprogs软件包安装的xfs_quota配额管理程序。

[root@localhost ~]# rpm -q xfsprogs
xfsprogs-4.5.0-12.el7.x86_64
[root@localhost ~]# rpm -ql xfsprogs | grep -i "xfs_quota"
/usr/sbin/xfs_quota
/usr/share/man/man8/xfs_quota.8.gz

xfs_quota 等命令介绍

  • mount -o usrquota,grpquota /dev/class/stu01 /mnt/stu01

-o usrquota,grpquota:以支持配额的形式挂载

  • xfs_quota -x -c 'limit -u bsoft=N bhard=N isoft=N ihard=N 用户名' 挂载点

配置磁盘配额

-x:启动专家模式,在当前模式下允许对配额系统进行修改的所有管理命令可用

-c:直接调用管理命令,

管理命令limit后相关:

-u:对用户限制

-g:对组限制

bsoft:磁盘容量软限制

bhard:磁盘容量硬限制

isoft:文件数量软限制

ihard:文件数量硬限制

  • xfs_quota -x -c 'report -abi' 挂载点

查看磁盘配额

管理命令report后相关:

-u:对用户查看

-g:对组查看

-a:查看所有可用分区的配额使用报告

-b:查看磁盘容量

-i:查看文件数

  • dd if=指定输入设备或文件 of=指定输出设备或文件 bs=指定读取block的大小 count=指定读取block的数量。

设备转换和复制命令

XFS 实验过程

1.关闭SELinux,若启用SELinux功能,不是所有的目录都能设定quota,默认quota仅能对/home进行设定。

[root@localhost ~]# setenforce 0

2.为stu01开启磁盘配额功能,并使其永久生效。

[root@localhost ~]# vim /etc/fstab
//修改stu01那行,这里为了方便,直接使用上一篇博客的实验成果。
/dev/class/stu01 /mnt/stu01 xfs defaults,usrquota,grpquota 0 0

3.查看stu01的磁盘配额是否生效。(未生效)

[root@localhost ~]# mount | grep stu01
/dev/mapper/class-stu01 on /mnt/stu01 type xfs (rw,relatime,seclabel,attr2,inode64,noquota)

4.重新挂载stu01,使磁盘配额生效。

[root@localhost ~]# umount /mnt/stu01/
[root@localhost ~]# mount -a
[root@localhost ~]# df -hT | grep stu01
/dev/mapper/class-stu01 xfs 33G 33M 33G 1% /mnt/stu01

5.查看stu01的磁盘配额是否生效。(已生效)

[root@localhost ~]# mount | grep stu01
/dev/mapper/class-stu01 on /mnt/stu01 type xfs (rw,relatime,seclabel,attr2,inode64,usrquota,grpquota)

6.若是不存在名为zhangsan的测试用户,则添加。

[root@localhost ~]# useradd zhangsan && echo "000000" | passwd --stdin zhangsan
Changing password for user zhangsan.
passwd: all authentication tokens updated successfully.

7.设置磁盘配额的数据。

[root@localhost ~]# xfs_quota -x -c 'limit -u bsoft=50M bhard=80M isoft=6 ihard=8 zhangsan' /mnt/stu01/

8.为了方便测试,放开权限。

[root@localhost ~]# chmod 777 /mnt/stu01/

9.切换到zhangsan用户。

[root@localhost ~]# su - zhangsan

10.文件数量配额测试。

[zhangsan@localhost stu01]$ touch test{1..9}.txt
touch: cannot touch ‘test9.txt’: Disk quota exceeded
[zhangsan@localhost stu01]$ ls
test1.txt test2.txt test3.txt test4.txt test5.txt test6.txt test7.txt test8.txt
[zhangsan@localhost stu01]$ rm -f *

11.磁盘空间配额测试。

[zhangsan@localhost stu01]$ dd if=/dev/zero of=/mnt/stu01/test1.txt bs=1M count=100
dd: error writing ‘/mnt/stu01/test1.txt’: Disk quota exceeded
81+0 records in
80+0 records out
83886080 bytes (84 MB) copied, 0.0799075 s, 1.0 GB/s
[zhangsan@localhost stu01]$ ls -lh
total 80M
-rw-rw-r--. 1 zhangsan zhangsan 80M Aug 25 02:47 test1.txt
[zhangsan@localhost stu01]$ touch test2.txt
touch: cannot touch ‘test2.txt’: Disk quota exceeded

12.查看配额使用情况。

[root@localhost ~]# xfs_quota -x -c 'report -abi' /mnt/stu01/
User quota on /mnt/stu01 (/dev/mapper/class-stu01)
Blocks Inodes
User ID Used Soft Hard Warn/Grace Used Soft Hard Warn/ Grace
---------- -------------------------------------------------- --------------------------------------------------
root 0 0 0 00 [--------] 3 0 0 00 [--------]
zhangsan 81920 51200 81920 00 [6 days] 1 6 8 00 [--------] Group quota on /mnt/stu01 (/dev/mapper/class-stu01)
Blocks Inodes
Group ID Used Soft Hard Warn/Grace Used Soft Hard Warn/ Grace
---------- -------------------------------------------------- --------------------------------------------------
root 0 0 0 00 [--------] 3 0 0 00 [--------]
zhangsan 81920 0 0 00 [--------] 1 0 0 00 [--------]

EXT4 文件系统

EXT3/4文件系统通过quota工具进行管理。

查看quota软件包的安装情况。

[root@localhost ~]# rpm -q quota
quota-4.01-17.el7.x86_64

quota 相关命令介绍

  • quotacheck

检测磁盘配额并生成配额文件

-a:检测所有可用的分区

-u:检测用户配额

-g:检测组配额

-c:创建配额数据文件

-v:显示执行过程

quotacheck -ugcv 设备文件名
quotacheck -augcv
  • edquota

编辑用户和组账号的配额设置

-u:修改用户配额,默认KB。

-g:修改组配额,默认KB。

-t:修改宽限时间

edquota -u 用户名
edquota -g 组名
edquota -t
  • quotaon & quotaoff

启动文件系统的磁盘配额功能

-u:用户

-g:组

-v:显示过程

quotaon -ugv 设备文件名或挂载点
quotaoff -ugv 设备文件名或挂载点
  • quota

查看用户、组配额使用情况

-u:用户

-g:组

quota -u 用户名
quota -g 组名
  • repquota

查看分区磁盘配额使用情况

repquota 挂载点

EXT4 实验过程

1.关闭SELinux,若启用SELinux功能,不是所有的目录都能设定quota,默认quota仅能对/home进行设定。

[root@localhost ~]# setenforce 0

2.为stu02开启磁盘配额功能,并使其永久生效。

[root@localhost ~]# vim /etc/fstab
//修改stu02那行,这里为了方便,直接使用上一篇博客的实验成果。
/dev/class/stu02 /mnt/stu02 ext4 defaults,usrquota,grpquota 0 0

3.查看stu02的磁盘配额是否生效。(未生效)

[root@localhost ~]# mount | grep stu02
/dev/mapper/class-stu02 on /mnt/stu02 type ext4 (rw,relatime,seclabel,data=ordered)

4.重新挂载stu02,使磁盘配额生效。

[root@localhost ~]# umount /mnt/stu02/
[root@localhost ~]# mount -a
[root@localhost ~]# df -hT | grep stu02
/dev/mapper/class-stu02 ext4 23G 44M 22G 1% /mnt/stu02

5.查看stu02的磁盘配额是否生效。(已生效)

[root@localhost ~]# mount | grep stu02
/dev/mapper/class-stu02 on /mnt/stu02 type ext4 (rw,relatime,seclabel,quota,usrquota,grpquota,data=ordered)

6.若是不存在名为zhangsan的测试用户,则添加。

[root@localhost ~]# useradd zhangsan && echo "000000" | passwd --stdin zhangsan
Changing password for user zhangsan.
passwd: all authentication tokens updated successfully.

7.检测磁盘配额并生成配额文件。

[root@localhost ~]# ls /mnt/stu02/
lost+found
[root@localhost ~]# quotacheck -ugcv /dev/class/stu02
quotacheck: Your kernel probably supports journaled quota but you are not using it. Consider switching to journaled quota to avoid running quotacheck after an unclean shutdown.
quotacheck: Scanning /dev/mapper/class-stu02 [/mnt/stu02] done
quotacheck: Cannot stat old user quota file /mnt/stu02/aquota.user: No such file or directory. Usage will not be subtracted.
quotacheck: Cannot stat old group quota file /mnt/stu02/aquota.group: No such file or directory. Usage will not be subtracted.
quotacheck: Cannot stat old user quota file /mnt/stu02/aquota.user: No such file or directory. Usage will not be subtracted.
quotacheck: Cannot stat old group quota file /mnt/stu02/aquota.group: No such file or directory. Usage will not be subtracted.
quotacheck: Checked 3 directories and 0 files
quotacheck: Old file not found.
quotacheck: Old file not found.
[root@localhost ~]# ls /mnt/stu02/
aquota.group aquota.user lost+found

8.编辑磁盘配额设置。

[root@localhost ~]# edquota -u zhangsan
Disk quotas for user zhangsan (uid 1000):
Filesystem blocks soft hard inodes soft hard
/dev/mapper/class-stu01 81920 51200 81920 1 6 8
/dev/mapper/class-stu02 0 40960 61440 0 4 6

9.启用磁盘配额功能。

[root@localhost ~]# quotaon -ugv /mnt/stu02/
/dev/mapper/class-stu02 [/mnt/stu02]: group quotas turned on
/dev/mapper/class-stu02 [/mnt/stu02]: user quotas turned on

10.为了方便测试,放开权限。

[root@localhost ~]# chmod 777 /mnt/stu02/

11.切换到zhangsan用户。

[root@localhost ~]# su - zhangsan

12.磁盘空间配额测试。

[zhangsan@localhost stu02]$ dd if=/dev/zero of=/mnt/stu02/test1.txt bs=1M count=50
dm-3: warning, user block quota exceeded.
50+0 records in
50+0 records out
52428800 bytes (52 MB) copied, 0.509643 s, 103 MB/s
[zhangsan@localhost stu02]$ dd if=/dev/zero of=/mnt/stu02/test2.txt bs=1M count=50
dm-3: write failed, user block limit reached.
dd: error writing ‘/mnt/stu02/test2.txt’: Disk quota exceeded
11+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.117714 s, 89.1 MB/s
[zhangsan@localhost stu02]$ ls -l
total 61472
-rw-------. 1 root root 7168 Aug 25 03:01 aquota.group
-rw-------. 1 root root 7168 Aug 25 03:08 aquota.user
drwx------. 2 root root 16384 Aug 25 00:20 lost+found
-rw-rw-r--. 1 zhangsan zhangsan 52428800 Aug 25 03:30 test1.txt
-rw-rw-r--. 1 zhangsan zhangsan 10485760 Aug 25 03:30 test2.txt

13.文件数量配额测试。

[zhangsan@localhost stu02]$ touch test{3..7}.txt
dm-3: warning, user file quota exceeded.
dm-3: write failed, user file limit reached.
touch: cannot touch ‘test7.txt’: Disk quota exceeded
[zhangsan@localhost stu02]$ ls -l
total 61472
-rw-------. 1 root root 7168 Aug 25 03:01 aquota.group
-rw-------. 1 root root 7168 Aug 25 03:08 aquota.user
drwx------. 2 root root 16384 Aug 25 00:20 lost+found
-rw-rw-r--. 1 zhangsan zhangsan 52428800 Aug 25 03:30 test1.txt
-rw-rw-r--. 1 zhangsan zhangsan 10485760 Aug 25 03:30 test2.txt
-rw-rw-r--. 1 zhangsan zhangsan 0 Aug 25 03:33 test3.txt
-rw-rw-r--. 1 zhangsan zhangsan 0 Aug 25 03:33 test4.txt
-rw-rw-r--. 1 zhangsan zhangsan 0 Aug 25 03:33 test5.txt
-rw-rw-r--. 1 zhangsan zhangsan 0 Aug 25 03:33 test6.txt

14.查看用户zhangsan的配额使用情况。

[root@localhost ~]# quota -u zhangsan
Disk quotas for user zhangsan (uid 1000):
Filesystem blocks quota limit grace files quota limit grace
/dev/mapper/class-stu01
81920* 51200 81920 6days 1 6 8
/dev/mapper/class-stu02
61440* 40960 61440 6days 6* 4 6 6days

总结

通过以上实验,可以发现,XFS文件系统的磁盘配额只要文件数量、磁盘空间其中任何一个达到限额,就无法继续写文件;而EXT4文件系统则不同,磁盘空间满了还可以创建空文件,直至数量达到限额,或者数量达到限额,覆盖已有文件创建非空文件,直至磁盘容量达到限额。

Linux 磁盘配额(XFS & EXT4)的更多相关文章

  1. Linux quota命令参数及用法详解---Linux磁盘配额限制设置和查看命令

    功能说明:显示磁盘已使用的空间与限制. 语 法:quota [-quvV][用户名称...] 或 quota [-gqvV][群组名称...] 补充说明:执行quota指令,可查询磁盘空间的限制,并得 ...

  2. quota - linux磁盘配额管理

    磁盘管理系列 linux磁盘管理系列一:磁盘配额管理   http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_linux_040_quota.html l ...

  3. Linux磁盘配额与LVM

    一.LVM概述  逻辑卷管理 Logical Volume Manager二.LVM机制的基本概念三.LVM的管理命令  ① 主要命令  ② ==LVM逻辑卷操作流程==  ③ 举例四.磁盘配额概述  ...

  4. Linux磁盘配额实验

    1.实现磁盘限额的条件 *需要Linux内核支持 *安装quota软件包2.Linux磁盘限额的特点 作用范围:针对指定 文件系统(分区) 限制对象:普通用户帐号.组帐号 限制类型:磁盘容量(默认单位 ...

  5. Linux磁盘配额

    Step1:修改fstab文件,增加磁盘限额用户和用户组信息 # /etc/fstab# Created by anaconda on Sat Dec 29 04:48:18 2018## Acces ...

  6. linux 磁盘配额配置

    1. 添加一块新磁盘 ,分区 .格式化 .(mkfs.etx3 /dev/sdc5/) 2.设置开机自动挂载(vi /etc/fstab) 添加磁盘配额支持 (用户配额usrquota.组配额grpq ...

  7. ★Linux磁盘配额的使用 ★——牛刀小试

    磁盘配额的作用:限制普通用户使用磁盘的空间和创建文件的个数,不至于因为个别人的浪费而影响所有人的使用 需要用户程序quota软件包 #rpm -qa | grep quota  查看quota软件包安 ...

  8. —linux 磁盘配额按用户管理(quota)

    我根据下面的ref链接整理的基本是的按用户额度管理步骤 (按组的额度管理被简化掉) 我在Ubuntu服务器12.04下整理,其他版本的Ubuntu和Linux应该都没有问题的 (有任何错误都指正给我, ...

  9. xfs文件系统磁盘配额

    引言 这篇文章简单介绍一下xfs文件系统的磁盘配额配置. 文章目录 0×1.开启分区磁盘配额 0×2.使用xfs_quota命令配置磁盘配额 0×1.开启分区磁盘配额 对于ext4文件以前的文件系统, ...

随机推荐

  1. ERROR: CAN'T FIND PYTHON EXECUTABLE "PYTHON", YOU CAN SET THE PYTHON ENV VARIABLE.解决办法

    错误原因:Node.js 在安装模块的时候报错,缺少python环境. 解决办法: 第一种方式: 安装Python及环境变量配置 一定要安装python2.7的版本 环境变量安装可以参考:http:/ ...

  2. 禁用 Ubuntu 18.04 Files 的 Type Ahead search 功能

    . . . . . Ubuntu 的文件浏览器(Files)提供了一个搜索的功能,叫做“Type Ahead search”.即我们在文件浏览器中输入某个文件的名字时,Files 并不是将焦点定位在某 ...

  3. 【npm permission denied错误】npm ERR! Error: EACCES: permission denied, access

    在命令前加上 sudo sudo npm install --save-dev grunt 不过这样子可能还是不行,你需要这样: sudo npm install --unsafe-perm=true ...

  4. linux安装jira

    JIRA配置本地MYSQL数据库 https://blog.csdn.net/coin_one/article/details/78376238 jira7.3.6 linux安装及破解 https: ...

  5. 破解FTP登录密码的几种方法

    工具 Hydra X-Hydra Medusa Ncrack Patator Metasploit Hydra hydra -L /root/Desktop/user.txt -P /root/Des ...

  6. 大数据分析常用去重算法分析『Bitmap 篇』

    大数据分析常用去重算法分析『Bitmap 篇』  mp.weixin.qq.com 去重分析在企业日常分析中的使用频率非常高,如何在大数据场景下快速地进行去重分析一直是一大难点.在近期的 Apache ...

  7. ASP.NET-------GridView中的字段居中不了

    在使用Grid View 控件的时候,回合一些css 放在一块使用之后你会发现  字段没有居中 你会发现该什么都不行 比如: HeaderStyle-HorizontalAlign="Cen ...

  8. git 删除本地分支,删除远程分支

    本地分支 git branch -d 分支名 远程分支 git push origin --delete 分支名 查看所有分支 git branch -a

  9. lua【卤鹅】总结

    转自:https://www.cnblogs.com/reblue520/p/10767428.html 编写一个简单的hello world程序 test.lua 如果觉得简单,可以给一个for循环 ...

  10. vps建站施工预告

    作为一个小白,最近几天自己用vps搭了个站点,用来发发博客,偶尔还可以去外面看看.后面几章就来记一下过程吧! 结构极为简单,建站用的WordPress,目前也就只有最基础的发文章功能.不过由于习惯了m ...