CentOS 5中ulimit值调整并永久生效的方法

*    soft    nofile    65536
*    hard    nofile    65536
*    soft    nproc    10240
*    hard    nproc    10240

Each line describes a limit for a user in the form:
<domain> <type> <item> <value>
Where:

  • <domain> can be:
    • an 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
  • <type> can have the two values:
    • "soft" for enforcing the soft limits
    • "hard" for enforcing hard limits
  • <item> can be one of the following:
    • core - limits the core file size (KB)
  • <value> 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
    • 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
    • rtprio - max realtime priority
    • chroot - change root to directory (Debian-specific)

*    soft    nofile    65536
*    hard    nofile    65536
*    soft    noproc    10240
*    hard    noproc    10240

ulimit -n unlimited

ulimit -n 9000

centos:

core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 515248
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) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 1024
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

其中 "open files (-n) 1024 "是Linux操作系统对一个进程打开的文件句柄数量的限制(也包含打开的SOCKET数量,可影响MySQL的并发连接数目)。这个值可用ulimit 命令来修改,但ulimit命令修改的数值只对当前登录用户的目前使用环境有效,系统重启或者用户退出后就会失效。

系统总限制是在这里,/proc/sys/fs/file-max,可以通过cat查看目前的值,修改/etc/sysctl.conf 中也可以控制。

另外还有一个,/proc/sys/fs/file-nr,可以看到整个系统目前使用的文件句柄数量。

查找文件句柄问题的时候,还有一个很实用的程序lsof.可以很方便看到某个进程开了那些句柄.也可以看到某个文件/目录被什么进程占用了。

修改方法

若要令修改ulimits的数值永久生效,则必须修改配置文档,可以给ulimit修改命令放入/etc/profile里面,这个方法实在是不方 便;还有一个方法是修改/etc/sysctl.conf 。我修改了,测试过,但对用户的ulimits -a 是不会改变的。只是/proc/sys/fs/file-max的值变了。(我给命令放到rc.local中失败)

我认为正确的做法,应该是修改/etc/security/limits.conf(里面有很详细的注释),追加下述内容:

 
 
view plain     copy
  1. * soft nofile 32768  
  2. * hard nofile 65536 

也可以直接运行下面这个shell。直接copy到你的终端中运行就好了:

 
 
view plain     copy
  1. echo -ne " 
  2. * soft nofile 65536 
  3. * hard nofile 65536 
  4. > > /etc/security/limits.conf 

就可以将文件句柄限制统一改成软32768、硬65536。配置文件最前面的是指domain,设置为星号代表全局,另外你也可以针对不同的用户做出不同的限制。

注意:这个当中的硬限制是实际的限制,而软限制,是warnning限制,只会做出warning.其实ulimit命令本身就有分软硬设置,加-H就是硬,加-S就是软。默认显示的是软限制,如果运行ulimit命令修改的时候没有加上的话,就是两个参数一起改变。

生效

修改完重新登录就可以见到。(我的系统是Centos5.6,修改了,重新登录后就立刻生效,可以用ulimit -a 查看确认。)

如果你使用squid的话,你要在/etc/init.d/squid的文件加入ulimit -HSn 65535.另外,在squid.conf中也要加入max_filedesc 16384。

ulimit -a 用来显示当前的各种用户进程限制。
Linux对于每个用户,系统限制其最大进程数。为提高性能,可以根据设备资源情况,设置各linux 用户的最大进程数,下面我把某linux用户的最大进程数设为10000个:
ulimit -u 10000
对于需要做许多 socket 连接并使它们处于打开状态的 Java 应用程序而言,最好通过使用 ulimit -n xx 修改每个进程可打开的文件数,缺省值是 1024。
ulimit -n 4096 将每个进程可以打开的文件数目加大到4096,缺省为1024
其他建议设置成无限制(unlimited)的一些重要设置是:
数据段长度:ulimit -d unlimited
最大内存大小:ulimit -m unlimited
堆栈大小:ulimit -s unlimited
CPU 时间:ulimit -t unlimited
虚拟内存:ulimit -v unlimited
  
暂时地,适用于通过 ulimit 命令登录 shell 会话期间。
永久地,通过将一个相应的 ulimit 语句添加到由登录 shell 读取的文件中, 即特定于 shell 的用户资源文件,如:

1)、解除 Linux 系统的最大进程数和最大文件打开数限制:
    vi /etc/security/limits.conf
    # 添加如下的行
    * soft noproc 11000
    * hard noproc 11000
    * soft nofile 4100
    * hard nofile 4100

    说明:* 代表针对所有用户
    noproc 是代表最大进程数
    nofile 是代表最大文件打开数
2)、让 SSH 接受 Login 程式的登入,方便在 ssh 客户端查看 ulimit -a 资源限制:
    a、vi /etc/ssh/sshd_config
       把 UserLogin 的值改为 yes,并把 # 注释去掉
    b、重启 sshd 服务:
       /etc/init.d/sshd restart
3)、修改所有 linux 用户的环境变量文件:
vi /etc/profile
ulimit -u 10000
ulimit -n 4096
ulimit -d unlimited
ulimit -m unlimited
ulimit -s unlimited
ulimit -t unlimited
ulimit -v unlimited

/**************************************

有时候在程序里面需要打开多个文件,进行分析,系统一般默认数量是1024,(用ulimit -a可以看到)对于正常使用是够了,但是对于程序来讲,就太少了。

修改2个文件。
1./etc/security/limits.conf
    vi /etc/security/limits.conf
    加上:
    * soft nofile 8192
    * hard nofile 20480
2./etc/pam.d/login
    session required /lib/security/pam_limits.so
**********
    另外确保/etc/pam.d/system-auth文件有下面内容
    session required /lib/security/$ISA/pam_limits.so
    这一行确保系统会执行这个限制。
***********
3.一般用户的.bash_profile
#ulimit -n 1024
重新登陆ok
-------------
对于solaris

其实在系统里面有这样一个命令ulimit,以下是ulimit -a执行的结果:

time(seconds) unlimited
file(blocks) unlimited
data(kbytes) unlimited
stack(kbytes) 8192
coredump(blocks) unlimited
nofiles(descriptors) 1024
memory(kbytes) unlimited
其中nofiles就是文件描述符的变量值,该值受rlim_fd_cur这个参数的影响,可以用ulimit -n number命令来修改。但不管怎么改,程序仍然不能突破fd=256的限制。在Solaris Tunable Parameters Reference Manua这本书里面能查到以下的资料:

A 32-bit program using standard I/O is limited to 256 file descriptors。
A 64-bit program using standard I/O can use up to 2 billion descriptors。
这也就是说32位的程序是没有办法突破这个限制的,只有64位的程序才能使用高达2亿个文件描述符,SUN的软硬件在很早以前就实现了64位的架构,现在唯一要解决的就是将程序编译成64位程序,为了生成64位程序,就必须要有64位的编译器(其实不是这样的),如果你去www.sunfreeware.com 下载64位编译器gcc,网站上没有特别注明是64位的gcc,但是会有个意外的收获,就是该软件的说明里面注明了只要在用gcc编译的时候加上-m64的option就能生成64位程序了。

于是用gcc -m64去编译生成一个64位程序后,用ulimit -n 102400将number of fd设成很大的情况下,所有问题迎刃而解,再也不存在文件描述符不够用的情况。

在/etc/system文件设置rlimi_fc_max和rlim_fd_cur格式如下:

* set hard limit on file descriptors
set rlim_fd_max = 4096
* set soft limit on file descriptors
set rlim_fd_cur = 1024
命令ulimit使用格式如下:

usage: ulimit [ -HSacdfnstv ] [ limit ]
ulimit -a是显示各参数的设置值,ulimit -n是用来设置fd的最大值的。
*************************************************

修改文件描述符限制

Solaris有两个参数控制进程可打开的文件描述符:rlim_fd_max,rlim_fd_cur。前者修改是个硬设置,修改需要权限,后者是个软设置,用户可以limit或者setrlimit() 修改,该值最大不能超过前者。一般我们在/etc/system里修改这两个参数

set rlim_fd_max = 65535

set rlim_fd_cur = 65535

==========================

ulimit 用于shell启动进程所占用的资源。

可以使用该命令查看进程占用资源的情况。

使用方法:ulimit [-acdfHlmnpsStvw] [size]

-H 设置硬件资源限制.
-S 设置软件资源限制.
-a 显示当前所有的资源限制.
-c size:设置core文件的最大值.单位:blocks
-d size:设置数据段的最大值.单位:kbytes
-f size:设置创建文件的最大值.单位:blocks
-l size:设置在内存中锁定进程的最大值.单位:kbytes
-m size:设置可以使用的常驻内存的最大值.单位:kbytes
-n size:设置内核可以同时打开的文件描述符的最大值.单位:n
-p size:设置管道缓冲区的最大值.单位:kbytes
-s size:设置堆栈的最大值.单位:kbytes
-t size:设置CPU使用时间的最大上限.单位:seconds
-v size:设置虚拟内存的最大值.单位:kbytes 5
1]在RH8的环境文件/etc/profile中,我们可以看到系统是如何配置ulimit的:

#grep ulimit /etc/profile
ulimit -S -c 0 > /dev/null 2>&1    (输出重定向,正常输出和异常输出都忽略)

这条语句设置了对软件资源和对core文件大小的设置
2]如果我们想要对由shell创建的文件大小作些限制,如:

#ll h
-rw-r--r-- 1 lee lee 150062 7月 22 02:39 h
#ulimit -f 100 #设置创建文件的最大块(一块=512字节)
#cat h>newh
File size limit exceeded
#ll newh
-rw-r--r-- 1 lee lee 51200 11月 8 11:47 newh
文件h的大小是150062字节,而我们设定的创建文件的大小是512字节x100块=51200字节
当然系统就会根据你的设置生成了51200字节的newh文件.
3]可以像实例1]一样,把你要设置的ulimit放在/etc/profile这个环境文件中.
如果针对所有用户设置,可在/etc/security/limits.conf 设置.

猜你喜欢

转载自wanguan2000.iteye.com/blog/1255347