Mac OSX下修改 ulimit 參數


OSX下Leopard, Yosemite版本的操作系統下, 修改 ulimit 命令默認的程序是 launchd.
默認的ulimit命令繼承自launchd, 默認的limits參數如下:

daviddeMacBook-Pro:etc david$ sudo launchctl limit
Password:
  cpu         unlimited      unlimited      
  filesize    unlimited      unlimited      
  data        unlimited      unlimited      
  stack       8388608        67104768       
  core        0              unlimited      
  rss         unlimited      unlimited      
  memlock     unlimited      unlimited      
  maxproc     709            1064           
  maxfiles    256            unlimited



如果因為如socket連接數過小等原因, 需要修改ulimit相關參數, 可以按照如下方式操作:
1.在 /etc/launchd.conf(如無, 需用擁有root權限的賬戶創建) 文件中, 增加這一行:

echo "limit maxfiles 65535 unlimited" | sudo tee -a /etc/launchd.conf
tee命令是等待用戶輸入, 並將輸入寫入到指定文件中, -a參數表示添加方式為"追加".


2.在本次會話中, lanuchd 命令已經啟動了; 如果想讓配置在任何用戶任何會話中都生效, 最簡單的方式就只需要重啟一下server.
如果還需要修改其他參數, 只需要使用 >> 命令追加到 /etc/launchd.conf文件中即可.
注:

1.上面

65535 unlimited

這2個參數即是設置soft和hard值.

2.Linux發行版redhat/centos配置與osx不同,如下:

vi /etc/security/limits.conf
# noproc進程數,nofile文件打開數
# soft軟限制, 在程序的進程中可自行改變.
# hard硬限制, 程序不可隨意改變,除非有root權限.
* soft noproc 11000
* hard noproc 11000
* soft nofile 4100
* hard nofile 4100

超過系統最大文件打開數之后,系統報 too many open files

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 by root:wheel

chown root:wheel /Library/LaunchDaemons/limit.maxproc.plist
chown root:wheel /Library/LaunchDaemons/limit.maxproc.plist
讀寫權限:-rw-r--r--

執行 ulimit -a
下面的參數應該是MacOS的默認參數

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) 256
pipe size               (512 bytes, -p) 1
stack size              (kbytes, -s) 8192
cpu time                (seconds, -t) unlimited
max user processes              (-u) 709
virtual memory          (kbytes, -v) unlimited


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個連接,基本到達系統最大限制了。
————————————————
版權聲明:本文為CSDN博主「明明很忙」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/mingtingjian/article/details/77675761


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM