ulimit is a built-in Linux shell command that allows you to view or limit the amounts of system resources consumed by individual users. Limiting resource usage is valuable in environments with multiple users and system performance issues.
In this tutorial, you will learn how to use the ulimit command on Linux with examples.
- running Linux
- Accessing a terminal
- Sudo privileges
What is limits.conf? The
limits.conf file is
a configuration file that defines the resource allocation settings of the ulimit uses system. The full path to the configuration file is /etc/security/limits.conf.
The file values persist after the system restarts. Superuser permissions are required to edit the limits.conf file.
first section of the limits.conf file looks like this
Soft Limit, and Hard Limit on Linux Explained
The following sections explain the ulimit syntax and the
difference between two types of resource throttling: a soft limit and a hard limit. ulimit syntax The ulimit command takes the following general syntax:
For example, to set the size of physical memory in kilobytes, type:
ulimit -m 15000
Use the ulimit flags to view or limit individual values for multiple system resources. When a [limit] is given, it is the new value of the specified resource.
The available indicators are:
Adding a [limit] passes the new value for the specified resources. When omitted, the command prints the current soft limit value for the specified resource, unless you specify the -H flag.
applies to both the soft limit and the hard limit if the –H and -S indicators are not specified.
The ulimit command has two
- It marks a successful completion.
- >0. An upper limit request was rejected or an error occurred.
limit Flexible resource limits are kernel-applied values for the corresponding resource. The soft limit is manageable by any user, and its maximum value cannot exceed the hard limit. The hard limit acts as a ceiling for the soft limit.
To view detailed simplified limits for the current user, run:
limit The physical resource limit
defines the physical resource limit for a user. At the same time, the hard limit is the maximum value for the soft limit. Only root users can change the maximum limit.
To view the detailed hard limits for
the current user, run: ulimit -Ha
How to use
ulimit Run the ulimit command
by entering the command name in the terminal: ulimit
The output shows the amount of resources that the current user has access to. In this example, the user has unlimited system resources. To view or set individual resource limits, use the available ulimit flags.
The following sections list the most common uses of the ulimit command.
Detailed Throttling Report
Get a detailed report with all resource limits for the current user by specifying the -a: ulimit -a
flag The output contains a detailed report on resource limits for
the current user. Process number limit Limit the maximum number of processes
for a user by specifying the -u flag
and the number of processes.
For example, we will limit
the process number to 10: ulimit
Restricting the maximum number of processes per user prevents them from consuming all system resources. Limiting the process number also avoids the adverse effects of possible attacks such as the fork pump.
In the example above, we first limit the process number to 10 and then run a fork pump. Otherwise, the fork pump would consume all resources and make the system unresponsive.
The -f flag sets the maximum file size that a user can create. For example, the following command limits the file size to 50 KB:
ulimit -f 50
Test whether the limit works by creating a larger file. For example, we use the cat command to redirect the /dev/zero output to a file, which would be much larger than 50KB:
The output indicates that the file size limit has been exceeded. Check
the file size by running: ls -lh file
The output of the ls command shows that the file size is exactly 50KB, which is the limit we have previously set
. Limit maximum virtual memory Use the -v flag
set the maximum amount of virtual memory available to a process. Limiting the virtual memory of a process prevents it from using all memory and prevents thrashing.
For example, the following command limits the virtual memory available to a process to 1000 KB: ulimit -v 1000
Limit the number of open files The -n flag limits
the number of simultaneously open files
(file descriptors). The following example sets the number of
open files to five: ulimit -n 5 To
test this, we will try to open several text files, resulting in an error
: edit the limits.conf file to change the soft or hard limit
To change the soft or physical limit, edit the values in the limits.conf file.
Follow the steps below:
1. Open a terminal window and change the directory to /etc/security: cd /etc/security
2. Open the limits.conf file with a text editor, such as the vim editor.
3. Change limit values by editing existing entries or adding a new one. Each throttling entry has four parts:
- <domain>. Defines a user, a group, or contains a wildcard (* or %).
- <type>. It accepts two values: soft or hard.
- <article>. Accepts any of the values listed in the configuration file.
- <value>. An integer value expressed in a unit associated with <item>.
Be sure to uncomment the line when editing the configuration file
Now you know how to use the ulimit command to prevent users, failed commands, or programs from using an excessive amount of system resources. Unlimited resource usage in a shared environment affects the experience of other users, so the best way to avoid it is to set appropriate limits.