The ulimit command in Linux is an extremely useful command for system admins who manage multi-user systems.
They need to have control over the resources being used by the users. This task can be made easy by setting limits for each user.
The ulimit command in Linux is an essential tool for this purpose. This shell command enables us to check and set resource limits for all users on a Linux system. Setting the right limits goes a long way in ensuring that your system works optimally.
Table of Contents
Before we start
You need to have root user access or sudo non-root user access to be able to allocate resources for all the users.
Basic Usage and Options for the ulimit Command
The ulimit command is a tool which is embedded in the shell. To launch ulimit, we simply type ‘ulimit‘ in the command line.
As you can see in the screenshot, we added the -a tag to display the limits for all the users.
The ulimit command in Linux allows various options to check and modify the limits for different system resources. These tags are shown in the table below.
|-c||Specifies the size of core dumps, in number of 512-byte blocks|
|-d||Specifies the size of the data area, in number of K bytes.|
|-e||the maximum scheduling priority (`nice’)|
|-s||the maximum stack size|
|-u||the maximum number of user processes.|
|-T||the maximum number of threads|
|-b||the maximum number of threads|
These are some of the most used options for the ulimit command in Linux. However, it can be difficult to remember all these tags on the top of your head.
So, if you wish to use an option but you can’t remember the functionality, use the –help tag to get a list of all the options available for the command.
Getting familiar with limits.conf
All the resource limits for a user is saved in a file named limits.conf, holding the configuration for all user limits. This file is saved in the directory at /etc/security/limits.d by default, which can hold resource limit configurations for multiple users.
Let’s take a look at the limits.conf file using the cat command
Here, the configuration file deals with four parameters:
Let’s look at each of these parameters individually.
1. The domain Parameter
We start with the ‘domain’. This defines the domain to which a limit belongs. It can be a specific user, a group of users or even a wildcard. The two wildcards available here are as follows.
- * – This refers to the default entry for the domain
- % – This is used used to specify maxlogin limit
These help the root user to define the user or the group of users to set a limit for.
2. The type Parameter
Next comes ‘type’. This refers to the type of limit which we wish to set for our user. There are two types of limits for us to choose from.
- soft- These are limits which can be exceeded by a user if required using the ulimit function
- hard- These limits are fixed by the root user and cannot be changed by anyone without root access to the system
Note that any kind of limits can only be set by a user with root access. Any other user can only edit the soft limits set for them if needed.
3. The item Parameter
The third parameter refers to the ‘item’ or the resource which we wish to limit. The resource can be selected by writing their reference name. Here is a list of resources and their reference names.
4. The value Parameter
The last parameter refers to the ‘value’ or the actual limit for our item. The unit for each value is associated with its corresponding item.
Let’s move on to learning how to set user limits.
Setting the Values in the ulimit File
Now that you understand what the parameters are, let’s set some limits.
Suppose you want to set a max data size limit to the “journaldev” as 4096kb, you can add a line at the end of the file in the following format:
journaldev hard data 4096
Setting/Changing Limits on the Fly
You can also set limits on the fly using the ulimit command. This will temporarily
NOTE: Resource limits on a system are set to make sure your system doesn’t run out of memory. These settings are essential to maintain the balance of resources on your system. Editing the ulimit values without proper knowledge can cause your system to misbehave or even stop working.
When you wish to change a limit, you simply need to call the ulimit command in Linux, followed by the limit option and the limit which you wish to set. An example of this is shown below.
ulimit -c unlimited
This command will set the limit for your core file size (denoted by the -c tag) to ‘unlimited’. Using this command, you can easily set limits for any system resource as per your requirement.
You can verify the change using the ulimit command with the appropriate tag as shown earlier in this tutorial.
Remember, changing the limits can only be done if the limits are set as soft limits in the limits.conf file. For hard limits, only the root user has access to modification of the limits.
This tutorial aimed to introduce you to the ulimit command and its functionalities. There are many more things one can do using the ulimit command in Linux.
So make sure you explore by typing man ulimit and info ulimit in your terminal to get in-depth information about the ulimit command.