mac os OS X 優山美地(Yosemite)如何提高最大連線數限制
超過系統最大檔案開啟數之後,系統報 too many open files
mac os yosemite 之後,網上原來提高最大開啟埠號限制的方法都不可行了。最新的辦法而下:
1, 檢視限制:
ulimit -a
2, 建立新的配置檔案,配置系統開啟最多檔案限制(如果沒有的話)
sudo vi /Library/LaunchDaemons/limit.maxfiles.plist
檔案內容
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>limit.maxfiles</string>
<key>ProgramArguments</key>
<array>
<string>launchctl</string>
<string>limit</string>
<string>maxfiles</string>
<string>655360</string>
<string>655360</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>ServiceIPC</key>
<false/>
</dict>
</plist>
3,建立每個最大程序數限制的配置檔案:sudo vi /Library/LaunchDaemons/limit.maxproc.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple/DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>limit.maxproc</string>
<key>ProgramArguments</key>
<array>
<string>launchctl</string>
<string>limit</string>
<string>maxproc</string>
<string>2048</string>
<string>2048</string>
</array>
<key>RunAtLoad</key>
<true />
<key>ServiceIPC</key>
<false />
</dict>
</plist>
4,以上兩個檔案 需要owned byroot:wheel
讀寫許可權:-rw-r--r--
5,執行launchctl limit是配置生效。也許需要重啟電腦(本人系統需要, OS X 10.10.3)
最好檢視配置是否生效:
$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) unlimited
max memory size (kbytes, -m) unlimited
open files (-n) 655360
pipe size (512 bytes, -p) 1
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 2048
virtual memory (kbytes, -v) unlimited
6,開啟埠後限制。
檢視socket埠範圍限制:
$ sysctl -a | grep port
kern.ds_supgroups_supported: 1
kern.ipc_portbt: 0
kern.hv_support: 1
vfs.generic.nfs.client.callback_port: 0
vfs.generic.nfs.server.require_resv_port: 0
vfs.generic.nfs.server.export_hash_size: 64
net.inet.ip.portrange.lowfirst: 1023
net.inet.ip.portrange.lowlast: 600
net.inet.ip.portrange.first: 1024
net.inet.ip.portrange.last: 65535
net.inet.ip.portrange.hifirst: 49152
net.inet.ip.portrange.hilast: 65535
net.inet.tcp.randomize_ports: 0
net.inet.udp.randomize_ports: 1
net.inet.ipsec.esp_port: 4500
machdep.misc.fast_uexc_support: 1
系統預設埠開啟範圍大小隻有大概1500個,
修改辦法:
開啟(沒有就建立)檔案 /etc/sysctl.conf
sudo vi /etc/sysctl.conf
新增以下兩行:
net.inet.ip.portrange.first=1024
net.inet.ip.portrange.last=65535
第一行指定最小埠,低於1024的埠,系統預留,部分已經預設分配給常見應用了。一般需要root才能分配這低於1024的埠範圍,不建議使用,除非你知道你在做什麼。第二行最大隻能到達65535=2^16 -1。因為ipv4埠只佔16bits,因此最大隻能到達65535。
因此一個客戶端,配置一個ipv4地址,能夠建立的最大連線數為65535-1024=64511。
如何使用mac os 建立連線測試伺服器,考慮帶其他應用程式佔用的埠,一般配置最大建立64000個連線,基本到達系統最大限制了。