서버/Linux II 2016. 2. 23. 15:18

Linux II - 08. 디스크 Quota

 

 

 

login as: root
root@192.168.1.7's password:
Last login: Tue Feb 23 14:49:30 2016 from 192.168.1.1
[root@CentOS2 /root]#

 

 

 

1. Quota

 

Quota란 사용자 또는 그룹별로 디스크 사용량, 파일 갯수를 제한하는 기능을 말한다. 이 기능을 이용하면 특정 사용자가 디스크 공간을 무제한으로 사용하는 것을 방지할 수 있다. 먼저, 현재 CentOS에 Quoat가 설치되어 있는지 'rpm' 명령어를 통해서 확인한다.

 

[root@CentOS2 /root]# rpm -qa | grep quota
quota-3.17-21.el6_5.x86_64

 

 

만약, Quota가 설치가 안되어 있다면, 다음과 같이 'yum' 명령어를 이용하여 설치한다.


[root@CentOS2 /root]# yum install quota


[root@CentOS2 /root]# rpm -qa | grep quota
quota-3.17-23.el6.x86_64

 

 

 

2. Quota 구성 단계

 

 1) 용량을 제한할 파티션을 선정한다.

 2) /etc/fstab에 용량을 제한할 파티션을 등록하고 Quota 옵션을 설정한다.

 3) 재부팅하거나, 또는 /etc/fstab에서 다시 읽을 수 있도록 다시 마운트를 실시한다.

 4) Quota DB 파일을 생성한다.

 5) Quota 활성화를 실시한다.

 6) 사용자별 Quota 설정을 실시한다.

 

 

 

Ex1) 사용자별 Quota 구성

 

 1) 용량을 제한할 파티션을 선정한다.

 

 - /dev/sdc/ 파티션 생성 및 파일 시스템 생성

 

[root@CentOS2 /root]# fdisk /dev/sdc

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

 

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-204, default 1): 1
Last cylinder, +cylinders or +size{K,M,G} (1-204, default 204): 204

 

Command (m for help): w
The partition table has been altered!

 

Calling ioctl() to re-read partition table.
Syncing disks.

[root@CentOS2 /root]# mkfs.ext4 /dev/sdc1 > /dev/null
mke2fs 1.41.12 (17-May-2010)

 

 

 - '/newhome' 디렉토리 생성 및 마운트 실시, 계정 추가


[root@CentOS2 /root]# mkdir /newhome


[root@CentOS2 /root]# mount /dev/sdc1 /newhome

[root@CentOS2 /root]# useradd -d /newhome/ccna ccna
[root@CentOS2 /root]# useradd -d /newhome/ccnp ccnp
[root@CentOS2 /root]# useradd -d /newhome/ccie ccie

[root@CentOS2 /root]# ls /newhome
ccie  ccna  ccnp  lost+found

 

 

 

 2) /etc/fstab에 용량을 제한할 파티션을 등록하고 Quota 옵션을 설정한다.

 

[root@CentOS2 /root]# vi /etc/fstab

 

  1
  2 #
  3 # /etc/fstab
  4 # Created by anaconda on Wed Feb  3 01:40:21 2016
  5 #
  6 # Accessible filesystems, by reference, are maintained under '/dev/disk'
  7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
  8 #
  9 UUID=7c533aa0-0a72-4a94-b076-382622ad8486 /                       ext4    defaults        1 1
 10 UUID=f55d38f2-1434-4107-8aab-3f9ac3cba3b5 /home                   ext4    defaults        1 2
 11 UUID=78b6f307-7ca2-4ae7-b2e5-866f01d64a15 swap                    swap    defaults        0 0
 12 tmpfs                   /dev/shm                tmpfs   defaults        0 0
 13 devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
 14 sysfs                   /sys                    sysfs   defaults        0 0
 15 proc                    /proc                   proc    defaults        0 0
 16 /dev/sdc1               /newhome                ext4    defaults,usrquota,grpquota      1 2

 

: wq!

 

 

 

 

3) 재부팅하거나, 또는 /etc/fstab에서 다시 읽을 수 있도록 다시 마운트를 실시한다.

 

 - 마운트 해지 및 다시 마운트 실시

 

[root@CentOS2 /root]# umount /newhome
[root@CentOS2 /root]# mount -a

[root@CentOS2 /root]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        13G  4.4G  7.3G  38% /
tmpfs           495M   72K  495M   1% /dev/shm
/dev/sda3       477M  2.3M  449M   1% /home
/dev/sdc1       194M  1.8M  182M   1% /newhome

 


 

 4) Quota DB 파일을 생성한다.

 

 - 'selinux' 가 활성화되어 있다면, Quota DB 생성 에러가 발생 (SElinux : 리눅스 취약점을 보완하기 위한 리눅스 기능)

 

[root@CentOS2 /root]# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /selinux
Current mode:                   enforcing
Mode from config file:          enforcing
Policy version:                 24
Policy from config file:        targeted


[root@CentOS2 /root]# quotacheck -ug /newhome
quotacheck: Cannot create new quotafile /newhome/aquota.user.new: 허가 거부
quotacheck: Cannot initialize IO on new quotafile: 허가 거부
quotacheck: Cannot create new quotafile /newhome/aquota.group.new: 허가 거부
quotacheck: Cannot initialize IO on new quotafile: 허가 거부

 

 

 - 다음 명령어를 실행한 이후,  Quota DB 생성 실시


[root@CentOS2 /root]# chcon --reference /var /newhome

 

[root@CentOS2 /root]# quotacheck -ug /newhome              u : 계정 Quota, g : 그룹 Quota
[root@CentOS2 /root]# ls -l /newhome
합계 34
-rw-------. 1 root root  7168 2016-02-26 17:04 aquota.group
-rw-------. 1 root root  7168 2016-02-26 17:04 aquota.user
drwx------. 4 ccie ccie  1024 2016-02-26 16:37 ccie
drwx------. 4 ccna ccna  1024 2016-02-26 16:37 ccna
drwx------. 4 ccnp ccnp  1024 2016-02-26 16:37 ccnp
drwx------. 2 root root 12288 2016-02-26 16:36 lost+found

[root@CentOS2 /root]# ls -l /newhome/*quota*
-rw-------. 1 root root 7168 2016-02-26 17:04 /newhome/aquota.group
-rw-------. 1 root root 7168 2016-02-26 17:04 /newhome/aquota.user

 

 

 5) Quota 활성화를 실시한다.

 

 - Quota를 활성화 하지 않으면, Quota가 적용되지 않기 때문에 활성화 실시


[root@CentOS2 /root]# quotaon -v /newhome                       비활성화 : quotaoff -v /newhome
/dev/sdc1 [/newhome]: group quotas turned on
/dev/sdc1 [/newhome]: user quotas turned on

 

 

 

 

 6) 사용자별 Quota 설정을 실시한다.

 

 - EDITOR 환경 변수가 설정안되있기 때문에, default로 vi 편집기에서 설정 실시

 

[root@CentOS2 /root]# echo $EDITOR

 

 

[root@CentOS2 /root]# edquota -u ccna

 

      1 Disk quotas for user ccna (uid 502):
      2   Filesystem                   blocks       soft       hard     inodes     soft     hard
      3   /dev/sdc1                        16          0          0          8        0        0
 

 

 

 - 테스트를 하기 위해서 다음과 같이 수정한다.

 

      1 Disk quotas for user ccna (uid 502):
      2   Filesystem                   blocks       soft       hard     inodes     soft     hard
      3   /dev/sdc1                        16       10000      15000       9        0        0

: wq!

 

 soft limit  : 디스크 사용량 제한(10000 = 10MB)

 hard limit : soft 값보다 크게 설정한다. (15MB)

 inodes : 저장할 수 있는 파일 갯수 제한 (0이면 무제한)

 

 

 - Quota 구성 내용 확인

 

[root@CentOS2 /root]# repquota -u /newhome
*** Report for user quotas on device /dev/sdc1
Block grace time: 7days; Inode grace time: 7days
                             Block limits                       File limits
User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      --      14       0       0              2     0     0
ccna      --      16   10000   15000              8     0     0
ccnp      --      16       0       0              8     0     0
ccie      --      16       0       0              8     0     0

 

 

 - ccna 계정으로 접속하여 Quota 테스트 실시

 

[root@CentOS2 /root]# su - ccna
[ccna@CentOS2 ~]$ mkdir test
[ccna@CentOS2 ~]$ cd test
[ccna@CentOS2 test]$
[ccna@CentOS2 test]$ pwd
/newhome/ccna/test

[ccna@CentOS2 test]$ dd if=/dev/zero of=./testfile1 bs=1024k count=2
2+0 records in
2+0 records out
2097152 bytes (2.1 MB) copied, 0.0181934 s, 115 MB/s

[ccna@CentOS2 test]$ ls
testfile1


[ccna@CentOS2 test]$ cp testfile1 testfile2
[ccna@CentOS2 test]$ cp testfile1 testfile3
[ccna@CentOS2 test]$ cp testfile1 testfile4
[ccna@CentOS2 test]$ cp testfile1 testfile5
sdc1: warning, user block quota exceeded.

[ccna@CentOS2 test]$ cp testfile1 testfile6
[ccna@CentOS2 test]$ cp testfile1 testfile7
[ccna@CentOS2 test]$ cp testfile1 testfile8
sdc1: write failed, user block limit reached.
cp: writing `testfile8': 디스크 할당량이 초과됨

[ccna@CentOS2 test]$ ls -lh testfile*
-rw-rw-r--. 1 ccna ccna 2.0M 2016-02-26 17:16 testfile1
-rw-rw-r--. 1 ccna ccna 2.0M 2016-02-26 17:16 testfile2
-rw-rw-r--. 1 ccna ccna 2.0M 2016-02-26 17:16 testfile3
-rw-rw-r--. 1 ccna ccna 2.0M 2016-02-26 17:16 testfile4
-rw-rw-r--. 1 ccna ccna 2.0M 2016-02-26 17:16 testfile5
-rw-rw-r--. 1 ccna ccna 2.0M 2016-02-26 17:16 testfile6
-rw-rw-r--. 1 ccna ccna 2.0M 2016-02-26 17:16 testfile7
-rw-rw-r--. 1 ccna ccna 636K 2016-02-26 17:16 testfile8


 

[ccna@CentOS2 test]$
[ccna@CentOS2 test]$ exit
logout
[root@CentOS2 /root]#

 

 

 - Quota 보고서 확인


[root@CentOS2 /root]# repquota /newhome | grep ccna
ccna      +-   14999   10000   15000  6days      18     0     0

 

ccna 계정은 0Byte 파일 이외에는 저장할 수 없는 상태이다. 그리고 soft limit를 초과하는 용량에 대해서는 6일간 유효하며, 6일이 경과되기 이전에 용량은 10MB 이하로 되어야하는 상태이다.

 

 

 

 

Ex2) 그룹별 Quota 구성

 

 - 그룹 및 사용자 계정 추가

 

[root@CentOS2 /root]# groupadd qtgroup


[root@CentOS2 /root]# useradd -g qtgroup -d /newhome/qtuser1 qtuser1
[root@CentOS2 /root]# useradd -g qtgroup -d /newhome/qtuser2 qtuser2
[root@CentOS2 /root]# useradd -g qtgroup -d /newhome/qtuser3 qtuser3

[root@CentOS2 /root]# groups qtuser1 qtuser2 qtuser3
qtuser1 : qtgroup
qtuser2 : qtgroup
qtuser3 : qtgroup

 

 

[root@CentOS2 /root]# edquota -g qtgroup

      1 Disk quotas for group qtgroup (gid 505):
      2   Filesystem                   blocks       soft       hard     inodes     soft     hard
      3   /dev/sdc1                        48          0          0         24        0        0

 

 

 - 테스트를 하기 위해서 다음과 같이 수정한다.

 

      1 Disk quotas for group qtgroup (gid 505):
      2   Filesystem                   blocks       soft       hard     inodes     soft     hard
      3   /dev/sdc1                        72        30000     40000         18         0        0
 

: wq!

 

 

 - Quota 구성 내용 확인

 

[root@CentOS2 /root]# repquota -g /newhome
*** Report for group quotas on device /dev/sdc1
Block grace time: 7days; Inode grace time: 7days
                              Block limits                       File limits
Group           used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      --      14       0       0              2     0     0
ccna      --   15000       0       0             18     0     0
ccnp      --      16       0       0              8     0     0
ccie      --      16       0       0              8     0     0
qtgroup   --      48   30000   40000             24     0     0

 

 

[root@CentOS2 /root]# repquota -ug /newhome
*** Report for user quotas on device /dev/sdc1
Block grace time: 7days; Inode grace time: 7days
                             Block limits                      File limits
User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      --      14       0       0              2     0     0
ccna      +-   15000   10000   15000  6days      18     0     0
ccnp      --      16       0       0              8     0     0
ccie      --      16       0       0              8     0     0
qtuser1   --      16       0       0              8     0     0
qtuser2   --      16       0       0              8     0     0
qtuser3   --      16       0       0              8     0     0


*** Report for group quotas on device /dev/sdc1
Block grace time: 7days; Inode grace time: 7days
                        Block limits                File limits
Group           used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      --      14       0       0              2     0     0
ccna      --   15000       0       0             18     0     0
ccnp      --      16       0       0              8     0     0
ccie      --      16       0       0              8     0     0
qtgroup   --      48   30000   40000             24     0     0

 


qtuser1, qtuser2, qtuser3 사용자들은 합쳐서 최대 40MB까지만 사용할 수 있다.

 

 

 

 

Ex3) Quota 삭제

 

[root@CentOS2 /root]# quotaoff -v /newhome
/dev/sdc1 [/newhome]: group quotas turned off
/dev/sdc1 [/newhome]: user quotas turned off


[root@CentOS2 /root]# ls /newhome
aquota.group  aquota.user  ccie  ccna  ccnp  lost+found  qtuser1  qtuser2  qtuser3


[root@CentOS2 /root]# rm /newhome/aquota.user
rm: remove 일반 파일 `/newhome/aquota.user'? y


[root@CentOS2 /root]# rm /newhome/aquota.group
rm: remove 일반 파일 `/newhome/aquota.group'? y


[root@CentOS2 /root]# ls /newhome
ccie  ccna  ccnp  lost+found  qtuser1  qtuser2  qtuser3

 

 

 

 

 - 환경 초기화


[root@CentOS2 /root]# userdel -r ccna
[root@CentOS2 /root]# userdel -r ccnp
[root@CentOS2 /root]# userdel -r ccie
[root@CentOS2 /root]# userdel -r qtuser1
[root@CentOS2 /root]# userdel -r qtuser2
[root@CentOS2 /root]# userdel -r qtuser3

[root@CentOS2 /root]# groupdel qtgroup


[root@CentOS2 /root]# umount /newhome
[root@CentOS2 /root]# rm -rf /newhome

 

 

[root@CentOS2 /root]# vi /etc/fstab

  1
  2 #
  3 # /etc/fstab
  4 # Created by anaconda on Wed Feb  3 01:40:21 2016
  5 #
  6 # Accessible filesystems, by reference, are maintained under '/dev/disk'
  7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
  8 #
  9 UUID=7c533aa0-0a72-4a94-b076-382622ad8486 /                       ext4    defaults        1 1
 10 UUID=f55d38f2-1434-4107-8aab-3f9ac3cba3b5 /home                   ext4    defaults        1 2
 11 UUID=78b6f307-7ca2-4ae7-b2e5-866f01d64a15 swap                    swap    defaults        0 0
 12 tmpfs                   /dev/shm                tmpfs   defaults        0 0
 13 devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
 14 sysfs                   /sys                    sysfs   defaults        0 0
 15 proc                    /proc                   proc    defaults        0 0
 16 /dev/sdc1               /newhome                ext4    defaults,usrquota,grpquota      1 2   <- 삭제
 

: wq!

 


[root@CentOS2 /root]# mount -a

[root@CentOS2 /root]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        13G  4.4G  7.3G  38% /
tmpfs           495M   72K  495M   1% /dev/shm
/dev/sda3       477M  2.3M  449M   1% /home

'서버 > Linux II' 카테고리의 다른 글

Linux II - 10. Linux 부팅 과정  (0) 2016.03.03
Linux II - 09. Linux Shell  (0) 2016.02.26
Linux II - 07. RAID 관리  (0) 2016.02.23
Linux II - 06. LVM (Logical Volume Manager)  (0) 2016.02.19
Linux II - 05. 파일 시스템 점검 및 복구  (0) 2016.02.17
Posted by 김정우 강사(카카오톡 : kim10322)
,


Q