磁盘配额实现

磁盘配额实现

磁盘配额要求必须是独立的分区

创建一个新的分区

#同步分区表

[root@localhost ~]# partx -a /dev/sda

[root@localhost ~]# mkfs.ext4 /dev/sda6 -L /home

[root@localhost ~]# blkid

/dev/sda6: LABEL=”/home” UUID=”49a738a9-05f5-46ba-bd46-37998b8bb4c3″ TYPE=”ext4″

 

目录迁移:注意防止用户访问造成数据丢失

[root@localhost ~]# mkdir /mnt/home

[root@localhost ~]# mount /dev/sda6 /mnt/home/

[root@localhost ~]# cp -av /home/* /mnt/home/

[root@localhost ~]# du -sh /home/

736K /home/

[root@localhost ~]# du -sh /mnt/home/

748K /mnt/home/

[root@localhost ~]# rm -rf /home/*

#且到单用户模式,只能在机器跟前操作了,,小心使用

[root@localhost ~]# init 1

 

修改开机挂载配置文件

[root@localhost ~]# vim /etc/fstab

UUID=49a738a9-05f5-46ba-bd46-37998b8bb4c3       /home   ext4    defaults 0 0

[root@localhost ~]# mount -a

[root@localhost ~]# df

Filesystem     1K-blocks    Used Available Use% Mounted on

/dev/sda6       20510716   45720  19416328   1% /mnt/home

/dev/sda6       20510716   45720  19416328   1% /home

[root@localhost ~]# umount /mnt/home/

家目录迁移完毕

 

[root@localhost ~]# su – ding

[ding@localhost ~]$ pwd

/home/ding

[ding@localhost ~]$ touch f1

 

实现配额

 

#1. 修改配置文件启用磁盘配额功能

[root@localhost ~]# vim /etc/fstab

UUID=49a738a9-05f5-46ba-bd46-37998b8bb4c3       /home   ext4    usrquota,grpquota 0 0

usrquota:限定用户

grpquota:限定组

[root@localhost ~]# mount -o remount /home

[root@localhost ~]# mount

/dev/sda6 on /home type ext4 (rw,usrquota,grpquota)

 

#在磁盘配额所在分区的根上,创建磁盘配额数据库,永来限定每个用户使用磁盘空间的配置信息

#2. 创建磁盘配额数据库

禁用selinux

[root@localhost home]# ll /etc/selinux/config /etc/sysconfig/selinux

-rw-r–r–. 1 root root 459 Dec  5 16:18 /etc/selinux/config

lrwxrwxrwx. 1 root root  17 Nov  7 15:36 /etc/sysconfig/selinux -> ../selinux/config

[root@localhost home]# cat /etc/selinux/config

[root@localhost home]# cat /etc/sysconfig/selinux

[root@localhost ~]# getenforce

Enforcing

[root@localhost ~]# setenforce 0

 

[root@localhost ~]# getenforce

Permissive

[root@localhost ~]# cd /home/

#-cug是对应着/etc/fstab中的usrquoat和grpquota的,如果只在配置文件中写的是usrquota,使用 -cu就好了

[root@localhost home]# quotacheck -cug /home/

[root@localhost home]# ls

aquota.group  aquota.user  ding  lost+found

 

 

#3. 启用磁盘配额数据库

#查看磁盘配额数据库是否启用

[root@localhost home]# quotaon -p /home/

group quota on /home (/dev/sda6) is off

user quota on /home (/dev/sda6) is off

#启用磁盘配额数据库

[root@localhost home]# quotaon /home/

[root@localhost home]# quotaon -p /home/

group quota on /home (/dev/sda6) is on

user quota on /home (/dev/sda6) is on

 

#4. 指定用户的空间限制

Disk quotas for user ding (uid 500):

Filesystem blocks soft hard inodes soft hard

/dev/sda6 748 0 0 155 0 0

 

Filesystem:设置磁盘配额启动的分区,有多个会都列出来

blocks soft hard 关心空间大小,限定了指定用户最多用多大空间

inodes soft hard 节点个数,限定了指定用户可以建立的文件个数

blocks:已经使用的空间

inodes:当前用户已经建立了多少文件

soft:警告值,超过就会报警,但是不限制用户

hard:不能操作

bloke单位为:1k

Disk quotas for user ding (uid 500):

Filesystem                   blocks       soft       hard     inodes     soft     hard

/dev/sda6                       748          0          0        155        0        0

 

Disk quotas for user ding (uid 500):

Filesystem                   blocks       soft       hard     inodes     soft     hard

/dev/sda6                       748       800000     1000000  155        160      170

 

 

#5. 测试

 

注意:使用指定的用户去测,在家目录测试

[ding@localhost ~]$ dd if=/dev/zero of=~/f1 bs=1M count=830

sda6: warning, user block quota exceeded.

830+0 records in

830+0 records out

870318080 bytes (870 MB) copied, 25.87 s, 33.6 MB/s

 

[ding@localhost ~]$ dd if=/dev/zero of=~/bigfile bs=1M count=780

[ding@localhost ~]$ dd if=/dev/zero of=~/bigfile2 bs=1M count=21

sda6: warning, user block quota exceeded.

21+0 records in

21+0 records out

22020096 bytes (22 MB) copied, 0.0183497 s, 1.2 GB/s

[ding@localhost ~]$ dd if=/dev/zero of=~/bigfile3 bs=1M count=300

sda6: write failed, user block limit reached.

dd: writing `/home/ding/bigfile3′: Disk quota exceeded

 

不是所有的命令都可以看到报警提示,有时候只是执行失败,但是不提示为什么错

[ding@localhost ~]$ echo >> f1

-bash: echo: write error: Disk quota exceeded

 

 

#超出指定后创建文件失败

[ding@localhost ~]$ touch f{f..k}

sda6: write failed, user file limit reached.

touch: cannot touch `fg’: Disk quota exceeded

#有警告提示,但文件创建成功

[ding@localhost ~]$ touch f11q

sda6: warning, user file quota exceeded.

-rw-rw-r–. 1 ding  ding         0 Dec  5 17:03 f11q

 

#达到最大值创建失败

[ding@localhost ~]$ touch {a..k}

sda6: write failed, user file limit reached.

touch: cannot touch `j’: Disk quota exceeded

 

 

根据什么判断用户占用的空间?

把用户家目录中的文件移走,并不能减少限制,删除可以,更改所有则所属组可以。文件占用的空间和它放哪没关系,和文件的所有者有关系

 

#文件的所有者是谁,空间就算在谁头上

[root@localhost ding]# chown mayun bigfile4

[root@localhost ding]# quota ding

Disk quotas for user ding (uid 500):

Filesystem  blocks   quota   limit   grace   files   quota   limit   grace

/dev/sda6  385596  800000 1000000             154     160     170

 

 

复制有个用户的权限设置给另一个用户

[root@localhost ding]# edquota -p ding mayun

[root@localhost ding]# edquota mayun

复制之后可以在改改

 

统一设置磁盘配额

[root@localhost ding]# useradd xm; setquota xm 10000 20000 100 200 /home

[root@localhost ding]# quota xm

Disk quotas for user xm (uid 502):

Filesystem  blocks   quota   limit   grace   files   quota   limit   grace

/dev/sda6      32   10000   20000               8     100     200

 

[root@localhost ding]# edquota xm

Disk quotas for user xm (uid 502):

Filesystem                   blocks       soft       hard     inodes     soft     hard

/dev/sda6                        32      10000      20000          8      100      200

 

 

grace: 警告可以超过,limit不能超过,grace是允许暂时超,7天之内可以达到警告超了没事,7天之后,soft也是不能超的了。再创建新的就不让创建了,只能减少不能增加了。只是给了一个宽限期而已

 

[root@localhost ding]# repquota /home/

*** Report for user quotas on device /dev/sda6

Block grace time: 7days; Inode grace time: 7days

Block limits                File limits

User            used    soft    hard  grace    used  soft  hard  grace

———————————————————————-

root      —      20       0       0              2     0     0

ding      -+  385596  800000 1000000            170   160   170  6days

mayun     —  614436  800000 1000000              9   160   170

xm        —      32   10000   20000              8   100   200

 

 

 

组配额的问题

[root@localhost ~]# setquota –help

setquota: Usage:

setquota [-u|-g] [-rm] [-F quotaformat] <user|group>

-u, –user                 set limits for user

-g, –group                set limits for group

-a, –all                  set limits for all filesystems

 

 

[root@localhost ding]# useradd xh

[root@localhost ding]# groupadd sales

[root@localhost ding]# usermod -G sales xh

[root@localhost ding]# usermod -G sales xm

 

设置组的磁盘配额

[root@localhost ding]# edquota -g sales

 

Disk quotas for group sales (gid 504):

Filesystem                   blocks       soft       hard     inodes     soft     hard

/dev/sda6                         0          0          0          0        0        0

 

测试:

[root@localhost ~]# su – xm

直接测试会无效,因为创建的文件的所有者是小明,和组没关系

 

[xm@localhost ~]$ newgrp sales

[xm@localhost ~]$ dd if=/dev/zero of=f1 bs=1M count=200

sda6: warning, group block quota exceeded.

200+0 records in

200+0 records out

209715200 bytes (210 MB) copied, 1.90333 s, 110 MB/s

[xm@localhost ~]$ ll

total 204800

-rw-r–r–. 1 xm sales 209715200 Dec  5 17:37 f1

 

组中的用户数据总和超过设定值

 

取消磁盘配额

 

[root@localhost ding]# quotaoff /home/

[root@localhost ding]# quotaon -p /home/

group quota on /home (/dev/sda6) is off

user quota on /home (/dev/sda6) is off

是二进制文件

[root@localhost home]# ls

aquota.group  aquota.user …………

 

[root@localhost home]# rm -rf aquota.*

[root@localhost home]# vim /etc/fstab

UUID=1e33b24a-4a67-4ea4-889b-ce5c553b0bb6 /home ext4    defaults        0       0

 

[root@localhost home]# mount -o remount /home/

[root@localhost home]# mount

/dev/sda6 on /home type ext4 (rw)

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/89707

(0)
无言胜千言无言胜千言
上一篇 2017-12-09 12:27
下一篇 2017-12-09 19:27

相关推荐

  • DNS原理详解

    1、DNS概述   domain name service  区域名称服务或者domain named system 区域名称系统,是互联网最基础的服务,分为正向域名解析(将域名解析为IP地址)和反向域名解析(将IP地址解析为域名)两部分。 2、bind   bind(Berkeley Internet Name Domain)…

    Linux干货 2016-08-26
  • Linux基础 文件权限

    概述 从接触linux第一天开始,Linux一切皆文件的哲学思想就深植于每个linuxer的心中,因此,实现Linux的系统安全必然绕不开文件权限。文件的权限是建立在用户的基础上的,脱离了用户,文件权限也就变得毫无意义了。用户、组和文件权限一起铸就了linux的系统安全模型。本文主要从文件权限方面了解Linux的安全模型。 文中主要讲解了: 1. 文件权限r…

    Linux干货 2016-08-05
  • 进入linux世界的初步认识

    1、 描述计算机的组成及其功能.
    2、 按系列罗列Linux的发行版,并描述不同发行版之间的联系与区别.
    3、 描述Linux的哲学思想,并按照自己的理解对其进行解释性描述.
    4、 说明Linux系统上命令的使用格式;详细介绍ifconfig、echo、tty、startx、export、pwd、history、shutdown、poweroff、reboot、hwclock、date命令的使用,并配合相应的示例来阐述.
    5、 如何在Linux系统上获取命令的帮助信息,请详细列出,并描述man文档的章节是如何划分的.
    6、 请罗列Linux发行版的基础目录名称命名法则及功用规定.

    Linux干货 2018-02-25
  • 文件查找命令find

      在文件系统上常常需要根据文件的各种属性去查找符合条件的文件。在Linux下也有相当优异的查找命令。今天只要将find和locate。find通常不常用,因为速度慢!通常我们先使用locate来进行模糊查找,如果真找不到了,才以find来查找。为什么呢?因为locate是利用数据库来查找数据,所以速度非常快,而且并没有实际查询硬盘比较省时间。 l…

    Linux干货 2016-08-22
  • Linux常用命令

    马哥教育网络班22期第四周课程练习 linux常用命令介绍(2) linux权限及命令组合使用示例 1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 ~]# cp -r /etc/skel /home/tuser1 &amp…

    Linux干货 2016-09-19