在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://blog.csdn.net/wade3015/article/details/90779669
這個方法確實可行,相互學習,共同進步。