实验背景:
Linux是个多用户多任务系统,出于安全等原因,对不同的用户我们常常做一定的权限管理。空间配额限制就是其中一个需要考虑的点。配额是基于分区配置的,避免用户数据占用太多磁盘空间,我们常常把home目录单独划分一个分区,然后对不同用户设定配额。
实验环境:
CentOS7.3
实验内容:
一、迁移home到独立分区
二、对用户设定配额
实验命令:
- lsblk、fdisk、mkfs.ext4、blkid、cp、du、vim、mount
- setenforce、quotaon、edquota、su、dd
一、迁移home到独立分区
1、划分分区
先用lsblk命令看一下当前机器的硬盘使用情况:
[root@CentOS7 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 97.7G 0 part /
├─sda3 8:3 0 48.8G 0 part /app
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 2G 0 part [SWAP]
sr0 11:0 1 7.7G 0 rom
从上面可以看到,磁盘还有大概50G的空间未使用。可以利用这50G添加一个新分区。这里我划分一个10G的新分区作为home的独立分区:
[root@CentOS7 ~]#fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
All primary partitions are in use
Adding logical partition 6
First sector (313497600-419430399, default 313497600):
Using default value 313497600
Last sector, +sectors or +size{K,M,G} (313497600-419430399, default 419430399): +10G
Partition 6 of type Linux and of size 10 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
这里有个warming。我们按提示 使用partprobe /dev/sda
命令刷新一下内存的分区表。
2、格式化新分区
[root@CentOS7 ~]#mkfs.ext4 /dev/sda6
mke2fs 1.42.9 (28-Dec-2013)
...
[root@CentOS7 ~]#blkid
/dev/sda1: UUID="4e02b896-3f74-4592-a44f-43a79a74627d" TYPE="xfs"
/dev/sda2: UUID="3b254514-1df1-4322-bf30-d14075937249" TYPE="xfs"
/dev/sda3: UUID="6b05345c-d2b7-42ed-bc15-411ffc962fe3" TYPE="xfs"
/dev/sda5: UUID="52074f6b-cdf3-49fd-bea2-09c0d5c92793" TYPE="swap"
/dev/sr0: UUID="2016-12-05-13-52-39-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/sda6: UUID="4530b0f0-a686-4dbe-80a1-5d425c40f3b9" TYPE="ext4"
新分区/dev/sda6,UUID="4530b0f0-a686-4dbe-80a1-5d425c40f3b9"
3、备份/home目录
备份/home目录到/app
[root@CentOS7 ~]#cp -a /home /app
4、确认一下备份数据
[root@CentOS7 ~]#du -sh /home /app/home
3.7M /home
3.7M /app/home
[root@CentOS7 ~]#ls /app/home
hyma luo
[root@CentOS7 ~]#ls /home
hyma luo
5、删除/home目录数据
[root@CentOS7 ~]#rm -rf /home
rm: cannot remove ‘/home’: Device or resource busy
删除/home目录时提示目录在使用中。这种情况我们可以切换到单用户模式操作。
注意!单用户模式会中断网络连接的终端,只能在机器前操作,请慎用。
6、配置/etc/fstab
[root@CentOS7 ~]#vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed May 17 14:26:36 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=3b254514-1df1-4322-bf30-d14075937249 / xfs defaults 0 0
UUID=6b05345c-d2b7-42ed-bc15-411ffc962fe3 /app xfs defaults 0 0
UUID=4e02b896-3f74-4592-a44f-43a79a74627d /boot xfs defaults 0 0
UUID=52074f6b-cdf3-49fd-bea2-09c0d5c92793 swap swap defaults 0 0
UUID=4e02b896-3f74-4592-a44f-43a79a74627d /boot xfs defaults 0 0
UUID=4530b0f0-a686-4dbe-80a1-5d425c40f3b9 /home ext4 defaults 0 0
7、挂载/dev/sda6到/home目录下
[root@CentOS7 ~]#mount -a
[root@CentOS7 ~]#df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 102350000 4563984 97786016 5% /
devtmpfs 485296 0 485296 0% /dev
tmpfs 499968 4 499964 1% /dev/shm
tmpfs 499968 13524 486444 3% /run
tmpfs 499968 0 499968 0% /sys/fs/cgroup
/dev/sda3 51175000 36776 51138224 1% /app
/dev/sda1 1038336 172120 866216 17% /boot
tmpfs 99996 8 99988 1% /run/user/0
/dev/sr0 8086368 8086368 0 100% /run/media/root/CentOS 7 x86_64
/dev/sda6 10190100 36888 9612540 1% /home
8、把备份在/app/home的文件拷贝到/home目录下
[root@CentOS7 ~]#cp -a /app/home/* /home
二、对用户设定配额
1、使能分区quota
我们通过配置/etc/fstab实现,格式如下:
UUID=4530b0f0-a686-4dbe-80a1-5d425c40f3b9 /home ext4 defaults,usrquota,grpquota 0 0
[luo@CentOS7 ~]$vim /etc/fstab
# /etc/fstab
# Created by anaconda on Wed May 17 14:26:36 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=3b254514-1df1-4322-bf30-d14075937249 / xfs defaults 0 0
UUID=6b05345c-d2b7-42ed-bc15-411ffc962fe3 /app xfs defaults 0 0
UUID=4e02b896-3f74-4592-a44f-43a79a74627d /boot xfs defaults 0 0
UUID=52074f6b-cdf3-49fd-bea2-09c0d5c92793 swap swap defaults 0 0
UUID=4e02b896-3f74-4592-a44f-43a79a74627d /boot xfs defaults 0 0
UUID=4530b0f0-a686-4dbe-80a1-5d425c40f3b9 /home ext4 defaults,usrquota,grpquota 0 0
2、重新挂载磁盘
[root@CentOS7 ~]#mount -o remount /home
3、创建磁盘quota数据库
[root@CentOS7 ~]#setenforce 0
[root@CentOS7 ~]#quotacheck -cug /home
4、激活/home目录配额功能
[root@CentOS7 ~]#quotaon /home
[root@CentOS7 ~]#quotaon -p /home
group quota on /home (/dev/sda6) is on
user quota on /home (/dev/sda6) is on
5、设定用户配额
这里我对用户luo设定配额,设定配额空间100M
[root@CentOS7 ~]#edquota luo
Disk quotas for user luo (uid 1001):
Filesystem blocks soft hard inodes soft hard
/dev/sda6 52 0 100000 13 0 0
6、验证
请注意切换成用户luo,在/home目录下创建文件
[root@CentOS7 ~]#su - luo
Last login: Sat Jun 24 09:09:08 CST 2017 on pts/0
[luo@CentOS7 ~]$dd if=/dev/zero of=/home/luo/quotatest bs=1M count=50
50+0 records in
50+0 records out
52428800 bytes (52 MB) copied, 0.0537142 s, 976 MB/s
[luo@CentOS7 ~]$dd if=/dev/zero of=/home/luo/quotatest bs=1M count=110
sda6: write failed, user block limit reached.
dd: error writing ‘/home/luo/quotatest’: Disk quota exceeded
98+0 records in
97+0 records out
102346752 bytes (102 MB) copied, 1.56307 s, 65.5 MB/s
当用户luo写入50M时,写入正常
当用户luo写入100M时,提示“write failed, user block limit reached.”