首先介绍下Linux系统中"一切都是文件"。
1. Linux系统文件句柄数概念
文件句柄和文件描述符
2. 查询Linux系统文件句柄数
# ulimit -a core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 7179 max locked memory (kbytes, -l) 64 max memory size (kbytes, -m) unlimited open files (-n) 65535 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) 7179 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited
3. 配置文件
/etc/security/limits.conf * soft nofile 2048 * hard nofile 32768 * soft nofile 2048 * hard nofile 32768 第一列表示域(domain),可以使用用户名(root等),组名(以@开头),通配置*和%,%可以用于%group参数。 第二列表示类型(type),值可以是soft或者hard 第三列表示项目(item),值可以是core, data, fsize, memlock, nofile, rss, stack, cpu, nproc, as, maxlogins, maxsyslogins, priority, locks, msgqueue, nie, rtprio. 第四列表示值.
4. 如何修改最大句柄数
临时生效: ulimit -n 2048 或者 echo ulimit -n 2048>>/etc/profile
永久生效:修改/etc/security/limits.conf配置文件
5. 如何查询当前系统中文件句柄数使用情况
1)统计各进程打开句柄数:lsof -n|awk '{print $2}'|sort|uniq -c|sort -nr
2)统计各用户打开句柄数:lsof -n|awk '{print $3}'|sort|uniq -c|sort -nr
3)统计各命令打开句柄数:lsof -n|awk '{print $1}'|sort|uniq -c|sort -nr