> ## Content Index
> Fetch the complete content index at: https://huizhou92.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Mac: Set Maximum Connection and Port Parameters
- URL: https://huizhou92.com/mac-set-maximum-connection-and-port-parameters/
- Published: 2024-09-04T08:38:23.000Z
- Updated: 2026-09-08T02:30:27.000Z
- Description: Mac: Set Maximum Connection and Port Parameters. Maximum Connections Limit The maximum number of connections limit refers to the limit of the maximum numbe。
- Author: huizhou92
- Tags: #Migrated-1788833207488, #Import 2026-09-08 02:07

### 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

```shell
## 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=1048600
```

Per-Process

```ruby
# 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=1048576
```

To make permanent changes, add the following content to `/etc/sysctl.conf`

```bash
kern.maxfiles=20480  
kern.maxfilesperproc=18000
```

This file may need to be created manually.

#### Ulimit Command

```shell
$ 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

```yaml
➜  ~ 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: 0
```

This indicates changing the starting address of the dynamic port to 32768; others are similar.

```bash
$ sysctl -w net.inet.ip.portrange.first=32768
```

#### launchd's Process Limitations

Get the current limits:

```bash
➜  ~ launchctl limit maxfiles 
 maxfiles    2048           unlimited
```

The output will be similar to this:  
The former is the soft limit, and the latter is the hard limit.  
Temporary modification it :

```shell
$ sudo launchctl limit maxfiles 65536 200000
```

To 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
<?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>
```

```shell
$ 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.plist
```

#### Persisting Mac Configuration (Not Recommended)

```bash
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, restart
```

The value of *ulimit -n* can be written in the .bashrc file.