Linux限制磁盘与内存配额【超详细】


大家好,我是早九晚十二,目前做linux相关工作。公众号主要分享日常的学习与工作遇到的知识点,

Linux工作者,不定时更新Linux相关知识,如有问题请留言。

9篇原创内容


Linux内存主要用来存储系统和应用程序的指令,数据,缓存等。磁盘是主要的存储介质,可以存储大量的二进制数据,并且断电后也能保持数据不丢失。在一个linux操作系统中,内存和磁盘并不是无限制的,经常会遇到磁盘或者硬盘不足,影响了服务器功能。所以大家会尝试添加各种报警、监控等用于及时地了解到服务器的状况。

那么有没有办法,可以限制磁盘和硬盘的使用量呢?答案是有的。

Linux限制磁盘用量的方式 什么是磁盘配额 磁盘配额即为quota,表示对用户和用户组使用磁盘空间和文件个数的限制,但是仅限定普通用户,对管理员无效。

磁盘配额的条件内核必须支持磁盘配合,且安装quota管理工具。查看内核是否支持磁盘配额,可以用grep命令[root@test ~]# grep QUOTA /boot/config-3.10.0-1160.el7.x86_64 CONFIG_NETFILTER_XT_MATCH_QUOTA=mCONFIG_XFS_QUOTA=yCONFIG_QUOTA=yCONFIG_QUOTA_NETLINK_INTERFACE=yCONFIG_PRINT_QUOTA_WARNING=y# CONFIG_QUOTA_DEBUG is not setCONFIG_QUOTA_TREE=yCONFIG_QUOTACTL=yCONFIG_QUOTACTL_COMPAT=y当grep出来的结果有CONFIG_XFS_QUOTA=y,即代表当前内核支持磁盘配额

安装quota工具检查是否安装rpm -qa|grep quota,有输出结果代表已经安装。未安装的话使用yum命令yum -y install quota repquota(report quota),检查磁盘空间限制的状态。 edquota,是编辑用户或群组的quota。

配额步骤新建一个磁盘分区[root@test ~]# fdisk  /dev/sda欢迎使用 fdisk (util-linux 2.23.2)。更改将停留在内存中,直到您决定将更改写入磁盘。使用写入命令前请三思。命令(输入 m 获取帮助):nPartition type:   p   primary (2 primary, 0 extended, 2 free)   e   extendedSelect (default p): p分区号 (1,2,默认 1):起始 扇区 (104857600-209715199,默认为 104857600):将使用默认值 104857600Last 扇区, +扇区 or +size{K,M,G} (104857600-209715199,默认为 209715199):将使用默认值 209715199分区 1 已设置为 Linux 类型,大小设为 50 GiB命令(输入 m 获取帮助):wThe partition table has been altered!Calling ioctl() to re-read partition table.WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.The kernel still uses the old table. The new table will be used atthe next reboot or after you run partprobe(8) or kpartx(8) 

新建/data目录并挂载[root@test ~]# mkdir /data[root@test ~]# mount /dev/vda1  /data/[root@test ~]# df -h /data/Filesystem      Size  Used Avail Use% Mounted on/dev/vda1       296G   17G  267G   6% /dat

新建临时用户,用于配置限额[root@test ~]# useradd test[root@test ~]# passwd testChanging password for user test.New password: BAD PASSWORD: The password is shorter than 8 charactersRetype new password: passwd: all authentication tokens updated successfully.[root@test ~]# cat /etc/passwd|grep testtest:x:1000:1000::/home/test:/bin/bash

分区开启配额临时开启[root@test ~]# mount -o remount,usrquota,grpquota /data/永久开启(需重启机器)[root@test ~]# echo "/dev/vda1 /data ext4 defaultsusrquota,grpquota 0 0 " >> /etc/fstab

对磁盘配额创建容量和文件数限制        [root@test ~]# xfs_quota -x -c 'limit -u bsoft=10M bhard=20M isoft=6 ihard=8 test'  /data仅创建磁盘容量限制[root@test ~]# xfs_quota -x -c 'limit -u bsoft=10M bhard=20M' /data仅创建磁盘文件数限制[root@test ~]# xfs_quota -x -c 'limit -u isoft=6 ihard=8 test' /data       ###bsoft和isoft是限制容量   isoft和ihard是限制文件数

 -x:表示启动专家模式,在当前模式下已允许对配额系统进行修改的所有管理命令可用

        -c:表示直接调用管理命令

       -u:指定用户账号对象

        -g:指定组账号对象`在这里插入代码片`

        bsoft:设置磁盘容量的软件限制数值

        bhard:设置磁盘容量的硬限制数值

        isoft:设置磁盘文件数的软限制数值

        ihard:设置磁盘文件数的硬限制数值

查看磁盘配额查看test磁盘:容量限制

[root@test ~]# xfs_quota  -c  'quota  -uv test' /data#查看test磁盘:文件数限制[root@test ~]#     xfs_quota -c ’quota -uv test‘ /data查看全部[root@test ~]#   xfs_quota -x -c "report -aibh"

内存限制方法 编辑limits.conf配置文件

[root@test data]# vim /etc/security/limits.conf #        - priority - the priority to run user process with#        - locks - max number of file locks the user can hold#        - sigpending - max number of pending signals#        - msgqueue - max memory used by POSIX message queues (bytes)#        - nice - max nice priority allowed to raise to values: [-20, 19]#        - rtprio - max realtime priority##<domain>      <type>  <item>         <value>##*               soft    core            0#*               hard    rss             10000#@student        hard    nproc           20#@faculty        soft    nproc           20#@faculty        hard    nproc           50#ftp             hard    nproc           0#@student        -       maxlogins       4# End of fileroot soft nofile 65535root hard nofile 65535* soft nofile 65535* hard nofile 65535例如限制每个用户的内存使用都不超过**10**G* hard rss 10000000

centos限制内存使用但是不kill进程 linux限制内存_sed

限制test用户内存使用不超过10G@test hard rss 10000000

centos限制内存使用但是不kill进程 linux限制内存_服务器_02


(1) 加*号表示对所有用户起作用,加@test表示只对某个名叫test的用户起作用。

(2) hard说明是硬上限,你也可以改成soft,也即软上限。

(3) rss表示我们限制的是内存的使用量。

(4) 10000000(单位KB)表明我们限制的量大概是10GB。退出终端重新登录查看结果

[root@test ~]# su test[test@test root]$ ulimit  -acore file size          (blocks, -c) 0data seg size           (kbytes, -d) unlimitedscheduling priority             (-e) 0file size               (blocks, -f) unlimitedpending signals                 (-i) 31200max locked memory       (kbytes, -l) 64max memory size         (kbytes, -m) 10000000open files                      (-n) 65535pipe size            (512 bytes, -p) 8POSIX message queues     (bytes, -q) 819200real-time priority              (-r) 0stack size              (kbytes, -s) 8192cpu time               (seconds, -t) unlimitedmax user processes              (-u) 4096virtual memory          (kbytes, -v) unlimitedfile locks                      (-x) unlimited

centos限制内存使用但是不kill进程 linux限制内存_服务器_03

此时可以看到最大内存改成了我们设置的10000000KB,至此配置完成。码字不易。