How to find ulimit for user on Linux

How can I find the correct ulimit values for a user account or process on Linux systems?

For proper operation, we must ensure that the correct ulimit values set after installing various software. The Linux system provides means of restricting the number of resources that can be used. Limits set for each Linux user account. However, system limits are applied separately to each process that is running for that user too. For example, if certain thresholds are too low, the system might not be able to server web pages using Nginx/Apache or PHP/Python app. System resource limits viewed or set with the NA command. Let us see how to use the ulimit that provides control over the resources available to the shell and processes.

ADVERTISEMENTS

How to find ulimit for user on Linux

The ulimit Linux command sets or displays user process resource limits. Typically, limits are defined in /etc/security/limits.conf file or systemd units.
How to find ulimit for user on Linux

Two types of limits

All Linux limits are categorized as either soft or hard:

  1. Soft limit – All users can change soft limits, up to max set by the hard limits. Pass the -S option to the ulimit.
  2. Hard limit – Only root users allowed to change esource hard limits. Pass the -H option to the ulimit.

Viewing ulimit for Linux user account

The syntax is as follows to view all soft and hard limits for the current user:
ulimit -Sa ## Show soft limit ##
ulimit -Ha ## Show hard limit ##

core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 126787
max locked memory       (kbytes, -l) 65536
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1048576
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) unlimited
cpu time               (seconds, -t) unlimited
max user processes              (-u) 126787
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

List all hard ulimit for user named ‘tom’

You must run the following command as root user or at least have access to that account via sudo/su:
su - tom -c "ulimit -Ha"
su - tom --shell /bin/bash -c "ulimit -Ha"

## You can use the sudo command ##
sudo -u tom bash -c "ulimit -Ha"
sudo -u tom sh -c "ulimit -Ha"

How to find ulimit -a for other users and process on Linux

Find all soft ulimit for user named ‘jerry’

Again, run it as root either using the su command or sudo command:
su - jerry -c "ulimit -Sa"
su - jerry --shell /bin/sh -c "ulimit -Sa"

## You can use the sudo command ##
sudo -u jerry bash -c "ulimit -Sa"
sudo -u jerry sh -c "ulimit -Sa"

Related tip: How to run multiple commands in sudo under Linux or Unix

Finding ‘ulimit -a’ for process user

A Linux process is nothing but a running instance of a program. For example, when you start the Firefox app, you created a process. However, some process runs in the background for a longer time. Typically server process works in the background, and you can not use the sudo command or su command to find their limits. In this example, nginx is running as www-data user on Debian Linux, but shell access to www-data user account blocked by default for security reasons. In other words, the following su or sudo command would fail 100%:
$ su - www-data -c "ulimit -Sa"
This account is currently not available.

How to find ulimit for a process

The syntax is:
cat /proc/PID/limits
First find PID (process ID) for nginx, run ps command along with the grep command:
ps aux | grep nginx
Sample outputs:

root        8868  0.0  0.0 127044 24048 ?        Ss   May23   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data   18074  0.0  0.0 127276 24716 ?        S    May23   0:09 nginx: worker process
www-data   18075  0.0  0.0 127276 22284 ?        S    May23   0:00 nginx: worker process

Now run the following cat command:
cat /proc/8868/limits
Sample outputs:

Limit                    Soft Limit          Hard Limit          Units     
Max cpu time              unlimited            unlimited            seconds   
Max file size             unlimited            unlimited            bytes     
Max data size             unlimited            unlimited            bytes     
Max stack size            8388608              unlimited            bytes     
Max core file size        0                    unlimited            bytes     
Max resident set          unlimited            unlimited            bytes     
Max processes             unlimited            unlimited            processes 
Max open files            1024                 1048576              files     
Max locked memory         65536                65536                bytes     
Max address space         unlimited            unlimited            bytes     
Max file locks            unlimited            unlimited            locks     
Max pending signals       385944               385944               signals   
Max msgqueue size         819200               819200               bytes     
Max nice priority         0                    0                    
Max realtime priority     0                    0                    
Max realtime timeout      unlimited            unlimited            us    

Most developers and sysadmin need to know the max number of processes and open files per nginx process. In a nutshell, we find the PID using the “ps aux | grep appname“, then look at that PID’s “limits” file in /proc/ directory. Then you will know for sure what values apply to that process to tweak for performance.

Understanding ulimit flags

OptionDescription
-S use the `soft’ resource limit
-H use the `hard’ resource limit
-a all current limits are reported
-b the socket buffer size
-c the maximum size of core files created
-d the maximum size of a process’s data segment
-e the maximum scheduling priority (`nice’)
-f the maximum size of files written by the shell and its children
-i the maximum number of pending signals
-k the maximum number of kqueues allocated for this process
-l the maximum size a process may lock into memory
-m the maximum resident set size
-n the maximum number of open file descriptors
-p the pipe buffer size
-q the maximum number of bytes in POSIX message queues
-r the maximum real-time scheduling priority
-s the maximum stack size
-t the maximum amount of cpu time in seconds
-u the maximum number of user processes
-v the size of virtual memory
-x the maximum number of file locks
-P the maximum number of pseudoterminals
-T the maximum number of threads

See bash man page here or run the following commands:
help ulimit
man bash

Conclusion

In this tutorial, You learned to find ulimit values of currently running process and user account on Linux using the ‘ulimit -a’ builtin command and /proc/${PID}/limits file.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.