Skip to content

Linux

ulimit and nfile

Learn how shell ulimit differs from systemd LimitNOFILE so daemons stop hitting open-file errors.

Updated Aug 29, 20263 min read9 reads
ulimit and nfile
Check shell ulimit versus the service LimitNOFILE

The ulimit command controls how many file descriptors a process may open, and nofile is the name of that limit. Each socket, log file, and PHP worker uses a descriptor, so nginx, php-fpm, and MySQL need a sensible value. A change in your shell only affects that session; daemons started by systemd read LimitNOFILE from their unit instead.

The kernel sets a system ceiling with fs.file-max. Soft and hard per-user limits live in limits.conf or a drop-in and apply when PAM opens a login session. systemd units carry their own limits for services. PHP has no separate nofile setting in php.ini; it inherits whatever limit the process already has. On a LiteSpeed account, lsphp follows the web server unit, not the limit from your SSH session.

bash
ulimit -n; systemctl show nginx | grep LimitNOFILE

The first command shows the limit for your current shell. The second shows the soft and hard values the nginx unit actually uses. If you raised ulimit in .bashrc and php-fpm still reports 1024, this comparison explains why. The daemon never sourced your bashrc. Use systemctl edit on the nginx or php-fpm unit, set LimitNOFILE=65535, then restart that unit so the new limit applies. A reload alone often leaves the old limit in place.

Shell, PAM, and systemd units

/etc/security/limits.conf and files under limits.d/ apply when PAM creates a session. They help interactive users and some older SysV-style daemons. Services started by systemd ignore those files unless the unit is written to honor them. Cron jobs often run with a default of 1024, which is why a scheduled task can hit “too many open files” while your SSH session looks fine. Match the place you edit to the process that fails.

You can use ss -s for a rough sense of connection volume. That is not the same as the file limit, but the two meet in practice: each keep-alive connection holds a descriptor on the proxy. A busy LiteSpeed vhost with a low nofile can surface as intermittent 502 errors rather than a clear message about open files.

What you can set on a VPS

On a KVM VPS you have root, so you can edit unit drop-ins and restart services yourself. Prefer a moderate raise such as 65535 for a busy proxy rather than an extreme value. After you change LimitNOFILE, confirm with systemctl show on that unit and with a fresh check of the error that prompted the change.

Shared hosting and CloudLinux

On shared hosting you do not set system ulimits. The node is already configured, and you have no root. If you hit a CloudLinux LVE file limit, that is an account cap. Raising ulimit inside cron or a user script will not bypass it. Contact support if you see repeated open-file errors on a shared account so we can review the LVE usage with you.

Share

Send this article

Need someone else to do this? Send them the link — the commands are in the article.

Tagged

Was this article helpful?

Be the first to rate this article.