4

I have heard

sudo launchctl limit maxfiles 64000 unlimited

And

conf=/etc/sysctl.conf
if sudo cat $conf | command rg kern.maxfiles ; then
  ecerr "kern.maxfiles is already set in $conf"
else
  sudo echo 'kern.maxfiles=40480
kern.maxfilesperproc=28000' >> "$conf"
fi

For previous versions of macOS, but none of these work in Big Sur.

4 Answers 4

5

/etc/sysctl.conf is no longer supported with Big Sur. To make persistent changes systctl can be issued on boot time using a launch daemon in /Library/LaunchDaemons/com.startup.sysctl.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>com.startup.sysctl</string>
    <key>LaunchOnlyOnce</key>
    <true/>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/sbin/sysctl</string>
        <string>kern.maxfiles=40480</string>
        <string>kern.maxfilesperproc=28000</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

and install it with

chown root:wheel /Library/LaunchDaemons/com.startup.sysctl.plist
launchctl load /Library/LaunchDaemons/com.startup.sysctl.plist
2
  • What security settings need to be flipped to allow writing into ` /Library/LaunchDaemons/` directory? It seems to be protected by Big Sur.
    – Gunnar
    Jun 23, 2021 at 15:10
  • I was using a terminal session as root and a curses-based text editor Jun 24, 2021 at 12:15
2

I have used the following technique with success on Big Sur v11.6

So it seems that there are multiple settings at play here.

The numbers returned by ulimit -Sn and ulimit -Hn for soft and hard per process limits.

The the numbers returned by launchctl limit maxfiles the first of which is a soft per process limit and the second a hard per process limit.

Then the results of sysctl -a | grep maxfiles which shows kern.maxfiles and kern.maxfilesperproc where kern.maxfiles should be the hard limit for all processes and kern.maxfilesperproc should be the hard limit for a single process.

It seems I can permanently set all of these by adding a file at /Library/LaunchDaemons/limit.maxfiles.plist

with the following content, where 524288 corresponds to the soft limit and 16777216 to the hard limit.

<?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>524288</string>
      <string>16777216</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>ServiceIPC</key>
    <false/>
  </dict>
</plist>

In order for the file to take effect you need to ensure that it's owned by root has group wheel.

sudo chown root:wheel /Library/LaunchDaemons/limit.maxfiles.plist

You also need to load the daemon.

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

And finally to have this affect the output of ulimit -Sn and ulimit -Hn i needed to restart my machine.

Note: It seems like a mistake that setting a per process soft and hard limit via launchctl affects the kernel max per proc and max file limits, but that is what happens, so I've just chosen values large enough that I think its unlikely I will run into any trouble with them.

1

Try:

sudo sysctl kern.maxfiles=64000 kern.maxfilesperproc=28000

Unfortunately it will not persist over reboots. Adding these to /etc/sysctl.conf does not appear to work in Big Sur.

To see your current settings use:

sysctl -a | grep maxfiles
0

I found this step works for me on Big Sur:

Enable server performance mode as describe below (don't forget to reboot) https://support.apple.com/en-us/HT202528

Then this:

sudo launchctl limit maxfiles 9000000 9999999    

And finally this (pay attention NOT to use sudo here):

ulimit -Sn 9000000

PS: Unfortunately the last two commands are not persist reboot, so they need to be done every time needed, or to include it on the script. Alternatively to add it at ~/.zshrc file on your Big Sur home directory.

2
  • (ulimit is a shell bultin, so using sudo with it is meaningless.)
    – HappyFace
    Jun 23, 2021 at 12:19
  • You're right @HappyFace :) Thank you for the correction... I've remove the sudo and in-fact it was not necessary and wrong in the first place.
    – Alex Yang
    Jun 23, 2021 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.