12

I'm running tmux and creating many sessions to various servers. After opening 128, I can no longer open new ones, or open new tabs in Terminal. The error messages is:

forkpty: Device not configured
Could not create a new process and open a pseudo-tty.

Is there any way I can increase the number of ptys?

2
  • It'd be helpful to know what version of OS X you're running, and what kind of Mac, and particularly, how much RAM you have installed.
    – gosmond
    Commented Feb 20, 2013 at 6:13
  • I'm using 10.8.2 and have 8gb of RAM, which should be more than enough to open thousands of ssh sessions. Commented Mar 2, 2013 at 22:45

2 Answers 2

14

(Tested on OS X 10.8.2; other releases may be slightly different.)

You can change the system’s limit on ptys with the sysctl command.

sudo sysctl -w kern.tty.ptmx_max=255

This should immediately let you open more ptys.

To set this automatically after each reboot, put the following line in the /etc/sysctl.conf file:

kern.tty.ptmx_max=255

(Note: I did not try this, but, from a look at the source code, it looks like launchd does process this file.)

This file may not exist; if you have to create it, you should probably be sure it has restricted ownership and permissions:

sudo chown root:wheel /etc/sysctl.conf
sudo chmod 644 /etc/sysctl.conf

Eventually, you may run into some other per-process or per-user resource limits (e.g. total number of processes per user, and total number of open file descriptors per process).

4
  • Eventually got to test this, works great, thanks. For the record, I couldn't set this above 999, but that will do for now. Commented Mar 2, 2013 at 22:44
  • 1
    This allows me to open up to 169 ptys, but not more. sudo sysctl kern.tty.ptmx_max returns 999 which is correct. Anything I'm missing on this one? Commented Apr 12, 2013 at 9:16
  • @ubuntudroid: You might be running into a limit on the number of open files (ulimit in bash). These kinds of limits are inherited from parent processes, so to change it for tmux you will need to change it in (e.g.) a shell and then restart your tmux server. To make the change “persistent”, try a launchctl.conf that uses a limit command. Commented Apr 12, 2013 at 18:44
  • Whenever inside tmux on my Mac (10.9.4) i noticed ulimit -a reported a 256 file descriptor limit. Calling ulimit -n 2000 totally worked though.
    – Steven Lu
    Commented Sep 2, 2014 at 17:33
0

I had new terminal windows failing with

[forkpty: Too many open files]
[Could not create a new process and open a pseudo-tty.]

even though I had kern.tty.ptmx_max: 511 (the default setting for my computer running Catalina with 64 GiB memory). I also had kern.maxfilesperproc: 98304 so I knew it was not that limit, either.

I thought it might be ulimit since ulimit -n was 256, but discounted that because lsof -c Terminal | wc was 315, well over 256. Turns out it was ulimit but lsof lists more files than ulimit limits. I will skip the explanation, but what you want to look at is

lsof -X -c Terminal | wc

The -X limits lsof to showing just file descriptors, which what ulimit limits. That was 257 (the 256 limit, plus a header line in the lsof output).

So, in addition to the 2 sysctl settings, I needed to adjust the default ulimit with launchctl. That turns out to be harder than you would expect.

The command to use to view the ulimit soft and hard limits on the number of open files is launchctl limit maxfiles which on my computer output

maxfiles    256           unlimited

The command to change the limits is launchctl limit maxfiles <softlimit> <hardlimit> but you cannot set the hard limit to "unlimited" because the kernel converts "unlimited" to 10240. If you leave out the hard limit, it is set to the same as the soft limit, which is even worse. So what I did was use the value from sysctl kern.maxfilesperproc as the hard limit. I ran

sudo launchctl limit maxfiles 1001 98304

Well, that took care of ulimit, but unfortunately, it also reset the kernel parameters:

kern.maxfiles: 98304
kern.maxfilesperproc: 1001

which is definitely not what I wanted. I wanted the soft limit to change the default ulimit and the hard limit to change kern.maxfilesperproc but instead the soft limit changed kern.maxfilesperproc and the hard limit changed kern.maxfiles. So I ran it again, restoring the previous values from the kernel

sudo launchctl limit maxfiles 98304 196608

This sets the ulimit way too high, but I think that is better than setting the kernel limit too low.

These changes also do not persist after reboot. To get them to persist, now that /etc/launchd.conf no longer works, consider these solutions for persisting a single setting or restoring the launchd.conf behavior.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .