在 macOS 上修改 最大文件描述符限制(Too many open files) 和 网络端口相关参数 需要调整系统级配置的详细步骤

在这里插入图片描述


在 macOS 上修改 最大文件描述符限制(Too many open files) 和 网络端口相关参数 需要调整系统级配置。以下是详细步骤:



一、临时修改(仅当前终端会话有效)

1. 修改文件描述符限制

# 查看当前限制
ulimit -n      # 软限制
ulimit -Hn     # 硬限制

# 临时提高限制(例如设置为 65536)
ulimit -n 65536

2. 修改网络端口相关参数

# 查看当前内核参数
sysctl kern.maxfiles
sysctl kern.maxfilesperproc

# 临时修改内核参数(例如设置为 65536)
sudo sysctl -w kern.maxfiles=65536
sudo sysctl -w kern.maxfilesperproc=65536

二、永久修改(系统重启后仍有效)

1. 修改文件描述符限制

方法一:通过 launchd 配置(推荐)

  1. 创建或编辑全局配置文件:

    sudo nano /Library/LaunchDaemons/limit.maxfiles.plist
    
  2. 添加以下内容(设置软硬限制为 65536):

    <?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>65536</string>
            <string>65536</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>ServiceIPC</key>
        <false/>
    </dict>
    </plist>
    
  3. 加载并生效:

    sudo launchctl load -w /Library/LaunchDaemons/limit.maxfiles.plist
    

方法二:修改 Shell 配置文件(如 .zshrc.bash_profile

echo "ulimit -n 65536" >> ~/.zshrc  # 替换为你的 shell 文件
source ~/.zshrc

2. 修改内核参数(网络端口相关)

  1. 编辑内核配置文件:

    sudo nano /etc/sysctl.conf
    
  2. 添加以下内容(根据需求调整数值):

    kern.maxfiles=65536
    kern.maxfilesperproc=65536
    kern.ipc.somaxconn=65535  # 控制监听队列长度
    net.inet.tcp.msl=1000     # 调整 TCP 最大段生命周期(单位:毫秒)
    
  3. 应用配置:

    sudo sysctl -p
    

三、验证修改结果

  1. 检查文件描述符限制:

    ulimit -n
    sysctl kern.maxfiles
    sysctl kern.maxfilesperproc
    
  2. 检查网络参数:

    sysctl net.inet.tcp.msl
    sysctl kern.ipc.somaxconn
    

四、注意事项

  1. 权限问题:修改系统级配置需要 sudo 权限。
  2. 数值选择:根据实际需求调整,避免设置过高导致系统资源耗尽。
  3. 应用适配:某些应用(如 Nginx、Node.js)可能需要单独配置其内部文件描述符限制。

通过以上步骤,可以解决 macOS 上因文件描述符或端口限制导致的 Too many open files 错误。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

独隅

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值