ulimit
command is used by the administrator to limit hardware resources in a pool share and is mainly used by shared hosting providers to curb unwanted hardware resource usage by other tenants.
There are two types of limits that you can set on your Linux machine.
Both limits have different usage. One can be overridden by root or sudo privilege, and the second one can be overridden by normal users within the threshold that the root account has set.
Ulimit is a built-in command located under /bin or /usr/bin. It can be found easily on all Linux distributions, whether it is Debian, Ubuntu, Arch Linux, Fedora, or any other RHEL-based distros.
So, let’s see how you can enforce limitations on physical hardware for other users and what types of limitations you can implement on them.
Table of Contents
Type of Limit
Although we have mentioned that ulimit has two types of limits hard limit and soft limit, we will now discuss them separately in the following sections.
What is the hard limit?
A hard limit is set by a privileged user to limit hardware resources for other users, and it can be set through the configuration “/etc/security/limits.conf”, which can be modified by the root user.
Once you have specified the limits in “limits.conf”, you need to restart your system for the changes to be applied.
What is the soft limit?
A privileged user also sets the “soft limit” to limit hardware resources for other users, but there are slight changes compared to the hard limit.
Standard users can also override soft limits within capped resources by root users.
You can manipulate soft limit resources through your terminal, but for persistent changes, you need to contact your administrator to remove the cap limit.
To set a soft limit on a current shell, use the -S option along with the ulimit command.
$ ulimit -S
Ulimit Command Syntax
The basic command syntax of ulimit is shown below.
$ ulimit [-OPTIONS] -[TYPE-OF-LIMIT]
System resources on which you can set limit
If you open the /etc/security/limit.conf configuration file, you will find the following list of items on which you can apply limitations:
These are listed below, and use the specific resource keywords when you want to apply changes throughout the system using limit.conf.
core limits the core file size (KB)
data max data size (KB)
fsize maximum filesize (KB)
memlock max locked-in-memory address space (KB)
nofile max number of open file descriptors
rss max resident set size (KB)
stack max stack size (KB)
cpu max CPU time (MIN)
nproc max number of processes
as address space limit (KB)
maxlogins max number of logins for this user
maxsyslogins max number of logins on the system
priority the priority to run user process with
locks max number of file locks the user can hold
sigpending max number of pending signals
msgqueue max memory used by POSIX message queues (bytes)
nice max nice priority allowed to raise to values: [-20, 19]
rtprio max realtime priorit
chroot change root to directory (Debian-specific)
How to find a detailed report about the current user
To check for any limitations on your account, you can invoke ulimit with the -a option, which will generate a report of the current logged in user.
$ ulimit -a
For better understanding, we have changed the default output a bit.
You can also generate a completely separate report for hard and soft limits assigned to your account or system by specifying -H (Hard limit) and -S (Soft limit).
$ ulimit -Ha
$ ulimit -Sa
How to check and change the max locked memory limit in Linux
To check the max locked memory limit, you can use the -l option along with the ulimit command.
$ ulimit -l
The output of the above command
From the above output, I have discovered I do have some limitations on Memory locked on my system. To change a limit, you can use -l along with the limitation value.
Let me change the memory-locked value of my current shell.
ulimit -l 15000
ulimit -l
The output of the above command
How to check and change the virtual memory limit in Linux
To check the virtual memory limit, you can use the -v option along with the ulimit command.
$ ulimit -v
From the above output, I have discovered I don’t have any limitations on virtual Memory. To set a limit, you can use -v along with the limitation value.
Let me set virtual memory limitations on my current shell.
$ ulimit -v 20000
$ ulimit -v
The output of the above command:
How to check and change open file limit in Linux
To check the open file limit, you can use -n option along with the ulimit command.
$ ulimit -n
The output of the above command
From the above output, I have discovered that I do have some limitations on the number of files that can be open on my system. To change a limit, you can use -n along with the limitation value.
Let me increase the limit on the number of open files in my current shell.
$ ulimit -n 2000
$ ulimit -n
The output of the above command:
How to check and change the file size limit in Linux
To check the file size limit, you can use -f option along with the ulimit command.
$ ulimit -f
The output of the above command
From the above output, I have discovered I don’t have any limitations on individual file size . To set a limit, you can use -f along with the limitation value.
Let me set a file size limitation on my current shell.
$ ulimit -f 5000
$ ulimit -f
The output of the above command:
How to check and change the max user process limit in Linux
To check the max user process limit, you can use -u option along with the ulimit command.
$ ulimit -u
The output of the above command
From the above output, I have discovered I do have some limitations on the number of processes a particular user can create. To change a limit, you can use -u along with the limitation value.
$ ulimit -u 500
$ ulimit -u
The output of the above command:
How to check and change the CPU time limit in Linux
To check the CPU time limit, you can use the -t option along with the ulimit command.
$ ulimit -t
The output of the above command
From the above output, I have discovered I don’t have any limitations on CPU time. To set a limit, you can use -t along with the limitation value.
$ ulimit -t 4
$ ulimit -t
The output of the above command:
How to check and change max memory size limit in Linux
To check the physical memory size limit, you can use the -m option along with the ulimit command.
$ ulimit -m
The output of the above command
From the above output, I have discovered I don’t have any limitations on physical Memory. To set a limit, you can use -m along with the limitation value.
Let me set max memory limitation on my current shell.
$ ulimit -m 4096
$ ulimit -m
The output of the above command:
Save changes ulimit changes permanent in limit.conf
The above change that you have made will be applicable till the shell is running, and once it is closed, every changes will be elapsed. To persist changes throughout the system, you must modify the /etc/security/limit.conf file.
To edit /etc/security/limit.conf, you need to have sudo privileges. One important point is that whatever changes you make to limit.conf can affect your users, and it can also crash your system when you are not sure about the value.
Therefore, I recommend that you modify the file only if you are sure about what you are doing.
To modify the limit.conf file, open your terminal using any command-line editor with sudo.
$ sudo nano /etc/security/limit.conf
While scrolling, you will find a suggested format to use to save changes.
<domain> <type> <item> <value>
- <domain>: specify user, group, and you can also use (* or %) wildcard
- <type>: Declare which type of limit you are assigning: “hard” or “soft”
- <item>: Specify the resource on which you want to set a limit.
- <value>: Define limit value
For example, if I want to set a soft file (fsize) limit for trendoceans, then I need to append this line to limit.conf file
trendoceans soft fsize 500
Suggestion for you: How to fix file size limit exceeded
Once you have modified “limit.conf”, you will need to reboot your system for the new changes to apply.
Wrap up
That’s all for ulimit command usage in Linux
A man with a tech effusive who has explored some of the amazing technology stuff and is exploring more. While moving towards, I had a chance to work on Android development, Linux, AWS, and DevOps with several open-source tools.