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.

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

Option Description
-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

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.

  • 0 Usuários acharam útil
Esta resposta lhe foi útil?

Artigos Relacionados

SSH login without password

You want to use Linux and OpenSSH to automate your tasks. Therefore you need an automatic login...

How to remove a Linux user

How do I drop or remove old users account from my Linux server? I can login using the user’s SSH...

How to restrict and except SSH access to specific IPs

Once your IP is public it gets attention from so many bots in the internet that do brute force...

How to lock and unlock user account in linux

With the help of two commands you can lock and unlock the user account in Linux. To Lock the...

Adding a user to a group

Synopsis This article describes how to add a Linux user (account) to a group. Environment...

Powered by WHMCompleteSolution