修改Linux最大文件描述符及ulimit相关使用

系统环境:Ubunut 16.04 LTS

1, 查看当前限制参数:ulimit -a

guli@guli-Ubuntu1:~$ ulimit -a
core file size          (blocks, -c) 0 (core dump 文件大小)
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 7685
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024 (当前用户所能打开的最大文件描述符)
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 7685
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

2,临时、立即生效的修改:ulimit -n 65535

3,永久生效的修改:修改 /etc/security/limits.conf 文件

guli@guli-Ubuntu1:~$ cat /etc/security/limits.conf
# /etc/security/limits.conf
#
#Each line describes a limit for a user in the form:
#
#            
#
#Where:
# can be:
#        - a user name
#        - a group name, with @group syntax
#        - the wildcard *, for default entry
#        - the wildcard %, can be also used with %group syntax,
#                 for maxlogin limit
#        - NOTE: group and wildcard limits are not applied to root.
#          To apply a limit to the root user,  must be
#          the literal username root.
#
# can have the two values:
#        - "soft" for enforcing the soft limits
#        - "hard" for enforcing hard limits
#
# can be one of the following:
#        - core - limits the core file size (KB)
#        - data - max data size (KB)
#        - fsize - maximum filesize (KB)
#        - memlock - max locked-in-memory address space (KB)
#        - nofile - max number of open files
#        - rss - max resident set size (KB)
#        - stack - max stack size (KB)
#        - cpu - max CPU time (MIN)
#        - nproc - max number of processes
#        - as - address space limit (KB)
#        - maxlogins - max number of logins for this user
#        - maxsyslogins - max number of logins on the system
#        - 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
#        - chroot - change root to directory (Debian-specific)
#
#                 
#

#*               soft    core            0
#root            hard    core            100000
#*               hard    rss             10000
#@student        hard    nproc           20
#@faculty        soft    nproc           20
#@faculty        hard    nproc           50
#ftp             hard    nproc           0
#ftp             -       chroot          /ftp
#@student        -       maxlogins       4

# End of file

配置示例:

* soft nofile 65535
* hard nofile 65535

注:
soft: 软限制
hard: 硬限制

注,* 不对 root 用户生效,如果要对 root 用户加限制,使用:

root soft nofile 65535
root hard nofile 65535

修改后保存,退出登录,重新登录,执行 ulimit -a 查看修改。

查看 ulimit 命令的使用帮助:man bash,然后搜索 ulimit:

ulimit [-HSTabcdefilmnpqrstuvx [limit]]

ulimit 用于资源限制,对于 shell 或 进程所有使用的资源进行限制。

限制方法:针对用户,或用户组进行设置。

-H: 硬限制
-S: 软限制

不加 -H,-S,默认都进行设置。

LIMIT 的值,除了可以是数字,也可以是 
    hard, soft, unlimited

    hard: 当前 hard 限制值
    soft: 当前 soft 限制值
    unlimited: 无限制

不加 LIMIT,表示打印对应选项的资源(有多个选项时,会显示资源名和单位):

guli@guli-Ubuntu1:~$ ulimit -f
unlimited

guli@guli-Ubuntu1:~$ ulimit -c
0

guli@guli-Ubuntu1:~$ ulimit -c -f
core file size          (blocks, -c) 0
file size               (blocks, -f) unlimited

             
    -a     All current limits are reported
    -b     The maximum socket buffer size
    -c     The maximum size of core files created
    -d     The maximum size of a process's data segment
    -e     The maximum scheduling priority ("nice")
    -f     The  maximum  size  of files written by the shell and its
          children
    -i     The maximum number of pending signals
    -l     The maximum size that may be locked into memory
    -m     The maximum resident set size (many systems do not  honor
          this limit)
    -n     The maximum number of open file descriptors (most systems
          do not allow this value to be set)
    -p     The pipe size in 512-byte blocks (this may not be set)
    -q     The maximum number of bytes in POSIX message queues
    -r     The maximum real-time scheduling priority
    -s     The maximum stack size
    -t     The maximum amount of cpu time in seconds
    -u     The maximum number of processes  available  to  a  single user
    -v     The  maximum  amount  of  virtual memory available to the
          shell and, on some systems, to its children
    -x     The maximum number of file locks
    -T     The maximum number of threads

    If limit is given, and the -a option is not used, limit  is  the
    new  value  of  the  specified resource.  If no option is given,
    then -f is assumed.  Values are in 1024-byte increments,  except
    for  -t,  which is in seconds; -p, which is in units of 512-byte
    blocks; and -T, -b, -n, and -u, which are unscaled values.   The
    return  status is 0 unless an invalid option or argument is sup‐
    plied, or an error occurs while setting a new limit.

使用方法:

ulimit -a 显示当前所有参数
ulimit -n LIMIT 设置最大文件描述符个数
ulimit LIMIT = ulimit -f LIMIT 默认使用 -f 选项,设置可创建文件的最大值

注,
一般选项单位: 1024-byte 为一个单位
-s 秒
-p 512-byte 为一个单位
-T -b -n -u 无单位

参考:http://www.cnblogs.com/likehua/p/3831331.html

你可能感兴趣的:(修改Linux最大文件描述符及ulimit相关使用)