Mac: Set Maximum Connection and Port Parameters
Maximum Connections Limit
Maximum Connections Limit
The maximum number of connections limit refers to the limit of the maximum number of files (file descriptors) that the system can open, which is divided into global and per-process types:
Global
## The system's default maximum number of connections limit is 2147483647
$ sysctl kern.maxfiles
kern.maxfiles: 2147483647
## Set the system's maximum number of connections from 49152 to 1048600
$ sudo sysctl -w kern.maxfiles=1048600Per-Process
# The default maximum number of connections per process is 61440
$ sysctl kern.maxfilesperproc
kern.maxfilesperproc: 61440
# Set the per-process connection limit from 24576 to 1048576, the maximum number of connections per process must be less than or equal to the global connection limit
$ sudo sysctl -w kern.maxfilesperproc=1048576To make permanent changes, add the following content to /etc/sysctl.conf
kern.maxfiles=20480
kern.maxfilesperproc=18000This file may need to be created manually.
Ulimit Command
$ ulimit -n 4864 #### Display the maximum number of files that the current shell can open, the default value is: 4864, this value is always less than the value of kern.maxfilesperproc because a shell is a process. $ ulimit -n 1048576 ### Set the maximum number of files that the current shell can open to 1048576, this value cannot be greater than kern.maxfilesperproc, otherwise, it will prompt that the setting has failed.Dynamic Port Range
➜ ~ sysctl net.inet.ip.portrange
net.inet.ip.portrange.lowfirst: 1023
net.inet.ip.portrange.lowlast: 600
net.inet.ip.portrange.first: 49152
net.inet.ip.portrange.last: 65535
net.inet.ip.portrange.hifirst: 49152
net.inet.ip.portrange.hilast: 65535
net.inet.ip.portrange.ipport_allow_udp_port_exhaustion: 0This indicates changing the starting address of the dynamic port to 32768; others are similar.
$ sysctl -w net.inet.ip.portrange.first=32768launchd's Process Limitations
Get the current limits:
➜ ~ launchctl limit maxfiles
maxfiles 2048 unlimitedThe output will be similar to this:
The former is the soft limit, and the latter is the hard limit.
Temporary modification it :
$ sudo launchctl limit maxfiles 65536 200000To modify system-wide, create a plist file limit.maxfiles.plist in the folder /Library/LaunchDaemons and chmod the file permissions, and then load the new settings
<?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>200000</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>ServiceIPC</key>
<false/>
</dict>
</plist>$ sudo chown root:hxzhouh /Library/LaunchDaemons/limit.maxfiles.plist
$ sudo chmod 644 /Library/LaunchDaemons/limit.maxfiles.plist
$ sudo launchctl load -w /Library/LaunchDaemons/limit.maxfiles.plistPersisting Mac Configuration (Not Recommended)
sudo touch /etc/sysctl.conf;
## Add the following content to the /etc/sysctl.conf file
kern.maxfiles=1048600
kern.maxfilesperproc=1048576
net.inet.ip.portrange.first=49152
net.inet.ip.portrange.last=65535
## After adding, restartThe value of ulimit -n can be written in the .bashrc file.