磁盘配额实现

磁盘配额实现

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

创建一个新的分区

#同步分区表

[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

相关推荐

  • 文本查找与文件查找

    咬文嚼字,颠倒糊涂,善哉!善哉? 念头通达,思虑有感,言简明,意通神 文件是什么?文本是什么?文本文件是什么?      好想来碗正常的豆腐脑,可惜每天早上也只能是豆腐花。吃不到,想想还是可以的,豆腐脑是“文件”,吃豆腐脑是“看文本”,找到地,点了豆腐脑,就是“查找文件”,符合个人口味的,加盐,加咸菜,加黄豆…

    2017-06-04
  • issue设置和定时关机练习

    /etc/issue内的代码意义 \l 显示第几个终端机接口(tty)\m 显示硬件的等级 (x86_64)\n 显示主机的网络名称 (zcl-domain)\r 操作系统的版本 (2.6.32-642.e16)\t 显示本地端时间的时间(20:22:12) \d显示本地端的日期 (2016-8-5)\s 操作系统的名称(linux)  练习 1,…

    Linux干货 2016-08-05
  • 几个常用命令、inode及第二周作业

    一、cd命令 1、功能 用来进行不同目录间的切换,属于内部命令。 2、语法 cd  [-L|-P]  [dir] -L : 如果要切换到的目录是一个符号链接,就直接切换到符号链接名表示的目录(默认)。 -P : 如果要切换到的目录是一个符号链接,直接接切换到符号链接指向的目标目录。 3、用法说明 cd 或 cd ~ :进入当前用户的主目录…

    Linux干货 2016-08-02
  • nginx+keepalived构建负载均衡代理服务器

    实验环境: 1、centos7.3 centos6.82、两台VS提供nginx代理和keepalived3、两台RS提供httpd服务并部署wordpress4、另外一台提供Mysql数据库服务 实验拓扑图: 拓扑图 实验步骤 1、在vs1(172.18.251.4)上配置,用yum安装keepalived和nginx yum install -y kee…

    Linux干货 2017-05-21
  • 用户及用户组管理使用的练习

    用户及用户组管理使用的练习 1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 先使用who得出登陆用户,通过uniq去重。 [root@mail ~]# who | grep -E -o “^[[:alnum:]]+” | uniq root 2、取出最后登录到当前系统的用户的相关信息。 思路:首先使用who取得最后…

    Linux干货 2018-03-18
  • Nginx配置进阶

    目录 ngx_http_rewrite_module模块 ngx_http_gzip_module模块 ngx_http_fastcgi_module模块 ngx_http_ssl_module模块 ngx_http_referer_module模块 ngx_http_rewrite_module模块 将用户某一次请求的URI当中的字符串是不是能够被我们给出…

    Linux干货 2016-11-05