磁盤配額實現

磁盤配額實現

磁盤配額要求必須是獨立的分區

創建一個新的分區

#同步分區表

[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.www58058.com/89707

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

相關推薦

  • 基礎指令的使用篇3 Linux版

    / /boot /bin /sbin /lib.modules /lib64 /etc/redhat-release /etc/centos-release /home/zczx /mnt /media /misc /proc/meminfo /proc/cpuinfo /proc/partition /sya /opt /tmp /usr/local/ /…

    Linux干貨 2016-08-04
  • Linux下which、locate、find 命令查找文件

    在Linux下我們經常要查找某個文件,但是又不記得文件位置,可以使用一些命令來搜索,目前接觸到的文件命令有以下幾個 which 查看可執行文件的位置 locate 基于數據庫查找文件位置 find 實時搜索文件 1、 Which   語法:which 可執行二進制文件名稱   Which是通過PATH環境變量查找文件的,在PATH 指定的…

    2017-08-14
  • 用戶和組管理 第三周

    1、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。 who | cut -d' ' -f1 | sort -u who:查看當前已經登陸的所有用戶的詳細信息 cut -d ' ' -f1:以空格為界顯示每行第一列 sort -u :在輸出行中去除重復行。 2、取出最后登錄到當前系…

    Linux干貨 2017-02-13
  • 筆記三、如何在VMWare Workstation中安裝CentOS 7

    一、準備工作     1.1 安裝VMWare WorkStatrion     1.2 準備好CentOS 7 鏡像包     http://www.centoscn.com/CentosSoft/iso/    CenOS7下載地址 二…

    2017-02-23
  • 上篇博客簡單提了一下Nginx配置文件的大體組成部分,這次來好好解釋一下這些個配置文件。 一、main配置段 分類:    正常運行必備的配置    優化性能相關的配置    用于調試及定位問題相關的配置   …

    Linux干貨 2016-10-26
  • 8月2日作業

    1、在/testdir/data里創建的新文件自動屬于g1組,組g2的成員如:alice能對這些新文件有讀寫權限,組g3的成員如:tom只能對新文件有讀權限,其它用戶(不屬于g1,g2,g3)不能訪問這個文件夾。 答: ~]# groupadd g1 ~]# groupadd g2 ~]# groupadd g3 ~]# useradd -G g2 alic…

    Linux干貨 2016-08-04
欧美性久久久久