九零不老心
发布于 2019-08-11 / 22 阅读 / 0 评论 / 0 点赞

修改linux打开文件描述符最大值或者打开进程数最大值

# 先说一下file-max
    man proc | grep file-max -A 5 -B 5
    /proc/sys/fs/file-max
              This  file defines a system-wide limit on the number of open files for all processes.  (See also setrlimit(2), which can be used by a process to set the per-process limit, RLIMIT_NOFILE, on the number of files it may open.)  If you get lots of error
              messages in the kernel log about running out of file handles (look for "VFS: file-max limit <number> reached"), try increasing this value:

                  echo 100000 > /proc/sys/fs/file-max

              The kernel constant NR_OPEN imposes an upper limit on the value that may be placed in file-max.

              If you increase /proc/sys/fs/file-max, be sure to increase /proc/sys/fs/inode-max to 3-4 times the new value of /proc/sys/fs/file-max, or you will run out of inodes.

              Privileged processes (CAP_SYS_ADMIN) can override the file-max limit.
    # 即file-max是设置系统所有进程一共可以打开的文件数量 。同时一些程序可以通过setrlimit调用,设置每个进程的限制。如果得到大量使用完文件句柄的错误信息,是应该增加这个值。
    # 也就是说file-max这项参数是系统级别的,而ulimit是用户级别的。
    # 显然,对服务器来说,file-max, ulimit都需要设置,否则就可能出现文件描述符用尽的问题

    # 查看当前file-max值
        cat /proc/sys/fs/file-max
            184359
    # 临时修改file-max的值
        echo  6553560 > /proc/sys/fs/file-max
        或者
        sysctl -w "fs.file-max=6553560"
    # 永久修改file-max的值
        vi /etc/sysctl.conf添加
            fs.file-max = 6553560
        # 即时生效
            sysctl --system
    
# 修改打开文件描述符的最大值
    # ulimit -n可以查看linux下打开文件描述符的最大值。
    # 临时修改
        ulimit -n 65535 可即时修改,但重启后就无效了
    # 永久生效(设置所有用户的打开文件描述符的最大值为65536)
        # 修改/etc/security/limits.conf
            cat /etc/security/limits.conf

                *               soft    nofile            65535
                *               hard    nofile            65535
        # 实际发现修改无效
            # 实际原因是:
                # ulimit 是 bash 内置命令,所以修改配置,不能立即生效,需要注销重新登录
                # 执行 ulimit 命令设置可以立即生效,但是仅仅针对当前会话生效
                # 本地登录和 ssh 远程登录不是一个会话,本地的修改不能体现在ssh 远程登录上(并不是本地配置没有生效)
                # /etc/ssh/sshd_config中UseLogin 默认是no,更改为 yes 后,SSH验证结束后会使用本地的/bin/login程序,会读取/etc/security/limits.conf,所以 ulimit -a 的结果和本地登录看到的一致
                    /etc/ssh/sshd_config 中 UsePAM 设置为yes
                    # sed -i '/#UseLogin no/a\UseLogin yes' /etc/ssh/sshd_config
                    # grep -E "UseLogin yes|UsePAM yes" /etc/ssh/sshd_config
                    echo "session    required     pam_limits.so" >> /etc/pam.d/login
                    echo "session    required     pam_limits.so" >> /etc/pam.d/system-auth
                    echo "session    required     pam_limits.so" >> /etc/pam.d/sshd
                    grep pam_limits.so /etc/pam.d/login /etc/pam.d/system-auth /etc/pam.d/sshd
                    systemctl restart sshd
            # 其他相关参考内容:
                # CENTOS7 /etc/security/limits.conf 文件的配置作用域缩小了一些。limits.conf这里的配置,只适用于通过PAM认证登录用户的资源限制,它对systemd的service的资源限制不生效。
                # 登录用户的限制,与上面讲的一样,通过 /etc/security/limits.conf 和 limits.d 来配置即可。
                # systemctl启动的进程资源限制,可以通过修改 /etc/systemd/system.conf 后重启系统设置非用户的进程资源限制。

# 修改打开的最大进程数
    # ulimit -u可以查看linux下打开的最大进程数。
    # 临时修改
        ulimit -u 65535 可即时修改,但重启后就无效了
    # 永久生效(设置所有用户的最大进程数为4096,root用户不做限制)
        cat /etc/security/limits.d/20-nproc.conf 

            *          soft    nproc     4096
            root       soft    nproc     unlimited