在Linux系统上,无论编写应用程序还是测试,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)。
在一些开发或测试过程中经常需要涉及到socket最大连接数(也即文件最大打开数)的设置修改,网上能搜索到一些资料,不过很多都不是很管用,本文总结了一下自己修改过程中实测有效的方法,希望能帮到有需要的朋友。
使用命令ulimit -a查看,其中open files就是最大连接数,一般情况下web服务器最大连接数的设置不能超过它,linux一般默认是1024
- root@ubuntu:~# 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) 31498
- 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) 31498
- virtual memory (kbytes, -v) unlimited
- file locks (-x) unlimited
open files 部分就是打开文件数1024个,一般这个太小了。也可以用ulimit -n查看
查看Linux系统级的最大打开文件数限制,使用如下命令:
root@ubuntu:~#cat /proc/sys/fs/file-max
810905
第一步:配置/etc/security/limits.conf
sudo vim /etc/security/limits.conf
文件尾追加下面两行,添加完后保存
- * hard nofile 40960
- * soft nofile 40960
40960可以根据自己业务需要自行设置,四列参数的设置见英文,简单讲一下:
第一列,可以是用户,也可以是组,要用@group这样的语法,也可以是通配符如*%
第二列,两个值:hard,硬限制,soft,软件限制,一般来说soft要比hard小,hard是底线,决对不能超过,超过soft报警,直到hard数
第三列,见列表,打开文件数是nofile
第四列,数量,这个也不能设置太大
- #
- #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
- # - NOTE: group and wildcard limits are not applied to root.
- # To apply a limit to the root user, <domain> must be
- # the literal username root.
- #
- #<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)
- # - 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)
- #
- #<domain> <type> <item> <value>
- #
-
-
- #* soft core 0
- #root hard core 100000
第二步:/etc/pam.d/su(官方)或/etc/pam.d/common-session(网络)
sudo vim /etc/pam.d/su
将 pam_limits.so 这一行注释去掉 ,保存修改
重起系统
sudo vim /etc/pam.d/common-session
加上以下一行并保存
session required pam_limits.so
打开/etc/pam.d/su,发现是包含/etc/pam.d/common-session这个文件的,所以修改哪个文件都应该是可以的
这个觉得修改su这个文件比较好,取消注释就OK了,不容易出错,vim打开,定位,x一下即可
官方只介绍到第二步,就重启系统了,没有第三步,我这里实测好象并没有改变,感觉是不是全是第三步的作用?!
第三步:配置/etc/profile
最后一行加上
ulimit -SHn 40960
重启,ulimit -n 验证,期待显示结果40960就没问题了
然而,显示总是那么残酷,重启之后发现,设置仍然没生效。谷歌半天,特别提醒ubuntu的pam_limits.so有个bug,就是不支持通配符,没办法,把前面的通配符*换成具体的用户名吧,root,test,nginx,web等等
- root soft nofile 40960
- root hard nofile 40960
- root soft nproc 40960
- root hard nproc 40960
-
这里特别提醒一下吧,这个坑真的很坑,花了近半天时间解决这个问题,希望这个经验可以给其他人带来帮助。
查看系统所有ipv4的tcp连接
root@ubuntu:~# ss -at4
a:显示所有
-t:tcp
-4:ipv4
显示socket摘要
root@ubuntu:~# ss -s
查看当前有多少个TCP连接到当前服务器命令:
netstat -antp |grep -i est |wc -l
查看 TCP 8080 端口连接数
netstat -nat|grep -i "8080"|wc -l
关于netstat请参考文章
https://www.cdsy.xyz/computer/system/linux/230402/cd42335.html
这个方法确实可行,相互学习,共同进步。