Master Linux Resource Limits: /etc/security/limits.conf, ulimit, and systemd Explained
This article provides a comprehensive guide to Linux resource limits, covering the role of /etc/security/limits.conf and its overrides, proper ulimit configuration, temporary versus permanent settings, common commands, and how systemd services manage their own limits.
1. Detailed Explanation of /etc/security/limits.conf
The file is actually the PAM module
pam_limits.soconfiguration and applies only to individual sessions; it does not affect system service limits. Also note the directory
/etc/security/limits.d/which can override settings.
Configuration format
<code># /etc/security/limits.conf
# This file sets the resource limits for the users logged in via PAM.
# It does not affect resource limits of the system services.
# Also note that configuration files in /etc/security/limits.d directory,
# which are read in alphabetical order, override the settings in this file
# when the domain is the same or more specific.
# <domain> <type> <item> <value>
# <domain> can be a user name, a group name prefixed with @, * for all users,
# or % for maxlogin limits.
# <type> is "soft" (enforced soft limit) or "hard" (enforced hard limit).
# <item> can be core, data, fsize, memlock, nofile, rss, stack, cpu,
# nproc, as, maxlogins, maxsyslogins, priority, locks, sigpending,
# msgqueue, nice, rtprio, etc.
# Example entries:
#* soft core 0
#* hard rss 10000
#@student hard nproc 20
#@faculty soft nproc 20
#@faculty hard nproc 50
#ftp hard nproc 0
</code>/etc/security/limits.d/ directory
The directory contains default files such as
*-nproc.confthat limit user processes. You can create additional
.conffiles here.
CentOS 7 uses
/etc/security/limits.d/20-nproc.confwith default
* soft nproc 4096and
root soft nproc unlimited.
CentOS 6 uses
/etc/security/limits.d/90-nproc.conf.
2. Configuring ulimit
Important notes
Do not set
nofile unlimited. The maximum allowed value for
nofileis 1 048 576 (2²⁰); exceeding it prevents SSH login and produces an error like “pam_limits(sshd:session): Could not set limit for ‘nofile’: Operation not permitted”.
Basic configuration
Place custom limits in
/etc/security/limits.d/instead of directly editing
/etc/security/limits.conf. For example, create
/etc/security/limits.d/20-nofile.confwith:
<code>root soft nofile 65535
root hard nofile 65535
* soft nofile 65535
* hard nofile 65535
</code>And
/etc/security/limits.d/20-nproc.confwith:
<code>* - nproc 65535
root soft nproc unlimited
root hard nproc unlimited
</code>Override behavior
Specific entries override more generic ones. For example, a
root soft nofile 65538entry takes precedence over a wildcard
* soft nofile 65539, but the soft value cannot exceed the hard value.
3. ulimit Effectiveness
Temporary configuration
Set a temporary limit with
ulimit -Sn 65536; it is lost after a reboot.
Permanent configuration
Add the same lines to
/etc/security/limits.confor a file under
/etc/security/limits.d/, then log out and back in for the changes to take effect.
4. Common ulimit Commands
<code>-S set soft limit
-H set hard limit
-a display all current limits
-n maximum number of open file descriptors
...</code>Use
ulimit -ato view all limits,
ulimit -nfor the open‑file limit, and
ulimit -Sn 65536to change it.
5. systemd‑related Limits
Systemd services have their own limits, which can differ from the PAM configuration. The effective limits are taken from three places:
Global system configuration
/etc/systemd/system.confUser‑service configuration
/etc/systemd/user.confPer‑service unit files
/usr/lib/systemd/system/*.serviceTo view a service’s limits, run
systemctl show sshd | grep '^Limit'or inspect
/proc/<pid>/limitsfor a running process.
To modify a service’s limit, either edit the global config and reload the daemon (
systemctl daemon-reexec) or add
LimitNOFILE=32768to the service’s unit file and run
systemctl daemon-reload && systemctl restart <service>.
6. Additional Tips
Check a process’s limits with
cat /proc/<pid>/limits.
Adjust a running process’s limits with
prlimit --pid <pid> --nofile=1024:4096.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.