Understand the ulimit Command and Soft Limits in Linux for Better Performance
When people think of Linux, they think about fine control over resource allocation and consumption.
One of the best things about using Linux on a server is its ability to manage system resources, like memory and processing capacity. You will find several great system tools that monitor resource utilization and allow you to set up consumption limits for these resources.
In this tutorial, we will discuss ulimit, a feature-packed utility that you can use to set resource consumption patterns and control the resources applications and processes can use.
However, we will start with a brief introduction to the ulimit utility and understand the idea of setting limits. Once you have this basic understanding, we will move on to the practical usage of the command.
Table Of Contents
- What is ulimit?
- Use ulimit to Set and Change Resource Consumption Limits
- Changing Soft or Hard Limits in the limits.conf File
What is ulimit?
The ulimit command in Linux is a shell built-in that provides control over the resources available to the shell and the processes it spawned.
The utility allows you to set limits on how much of a resource (like CPU time or file size) a process can consume. This is crucial in multi-user or multi-server environments where resource allocation needs to be managed carefully to prevent individual processes from over-consuming resources and affecting the processing of others.
You can use ulimit to manage all types of server resources, such as:
- CPU time limits
- File size creation limits
- Data segment size
- Virtual memory size
- Core file size
You can set individual limits for each of these resources to ensure that the system runs smoothly and efficiently.
It’s important to note that ulimit settings can be set for individual user sessions or as a system-wide experience. This flexibility allows for tailored resource management based on specific needs and scenarios.
What is limits.conf?
The limits.conf file is a critical file in the process of setting limits with the ulimit utility. You can edit this file to set up control limits that the ulimit refers to when setting up resource limits. In most cases, you can find the file at /etc/security/limits.conf.
When you change values in this file, the changes are retained even after you restart the computer. Note that you need to have superuser (administrator) rights to make any changes to the limits.conf file.
You can use your favorite text editor to view and edit the file. Here’s how the initial parts of the limits.conf file usually appears:
The Standard ulimit Syntax
You can typically access the ulimit utility via the shell (Bash, Zsh, etc.). The easiest way is to launch the terminal and start using the basic command syntax to interact with the utility.
The basic syntax of the ulimit command is quite straightforward:
# ulimit [options] [limit]
Here, options determine which resource limit is being set or displayed, and the limit is the value you wish to set for that resource.
For instance, here is the command syntax to set the maximum file size:
# ulimit -f 1024
The ulimit Command Flags
We recommend using one or more ulimit flags to either view or set limits for different system resources. When you include a [limit] with these flags, it sets a new value for the specified resource.
The [Limit] Parameter
When you add a [limit] value, it sets a new value for the chosen resource. If you don’t add a [limit], the command will show you the current soft limit for that resource. This happens unless you use the -H flag.
If you don’t use either the -H or -S flags, the [limit] you set will apply to both the soft and hard limits.
Exit Values for the ulimit Command
The ulimit command usually exits with the following two status codes:
0: This means the command worked without any problems.
Greater than 0: This indicates that either there was an issue in command execution or you tried to set a limit higher than allowed.
The Idea of Limits
When working with the ulimit utility, you should be aware of the idea of limits. In this context, the idea of limits can be further divided into the following two ideas:
Soft limits are the values set by the kernel for various resources. While users can change these limits, they can’t set values higher than the hard limits. Think of the hard limit as the maximum allowed value for the soft limit.
To see all the soft limits that are currently set for your user account, you can run the following command in the terminal:
# ulimit -Sa
Hard limits set the upper boundary for resource usage for a user. These limits also act as the maximum possible values for the soft limits. Only users with root (administrator) privileges can modify these hard limits.
To check all the hard limits currently in place for your user account, run the following command:
# Ulimit -Ha
Use ulimit to Set and Change Resource Consumption Limits
Let’s see how you can use the ulimit command to change the system’s resource consumption.
Before trying out the commands and ideas we will discuss later on, we recommend you have the following:
- A system running a mainstream Linux distribution.
- A user account with sudo privileges.
Get an Overview of System Limits
Let’s start with an overview of the system resource consumption limits by running the ulimit command without any flags or options.
The general output of the command is unlimited.
This output shows the maximum size of files that can be created by the user. Here, unlimited means there is no restriction on the file size that can be created by the root user.
View All Limits
You can view all the limits for your user account by simply adding the -a flag to the ulimit command:
Limit the CPU Time
Run the following command in the terminal:
# ulimit -t 60
This command sets a limit on how long (in seconds) each process can use the CPU. The above command limits the CPU time for each process to 60 seconds. If a process tries to use more than this amount of CPU time, it will be stopped.
Restrict the Size of Core Files
Run the following command in the terminal to prevent the system from creating core system files.
# ulimit -c 0
The core files are usually generated when a program crashes, and they can take up space. By setting the limit to 0, you’re telling the system not to create these files at all. Depending upon your system setup, this command can help save disk space.
Set the Maximum Number of Open File Descriptors
File descriptors are used by processes to handle files and connections. The default limit of these file descriptors is 2000. However, you can run the following command to increase the limit to 2048, allowing each process to handle more files or connections at the same time.
#ulimit -n 2048
It’s especially useful for servers or programs that need to manage many connections simultaneously.
Changing Soft or Hard Limits in the limits.conf File
Editing the limits.conf file to change the soft or hard limits for system resources is an important task for system administrators.
Here’s an example to illustrate the process of editing the file and setting the resource consumption levels:
Open the limits.conf File
First, you need to open the /etc/security/limits.conf file in your preferred editor.
This file requires superuser permissions to edit, so you should prefix the edit command with sudo to open it without any issues. In this demonstration, we will open the file in Vi:
# vi /etc/security/limits.conf
Important: Changes in limits.conf are sensitive and should be made carefully to avoid system issues.
Adjust the Limit Values
To change how many resources a user or group can use, edit, or add entries in the limits.conf file.
Each entry is made up of four parts:
- <domain>: This tells who the limit is for. It can be a user’s name, a group (with @ in front), or a wildcard (* for everyone).
- <type>: This is either soft or hard.
Soft is a limit that users can increase themselves, but only up to the hard limit.
Hard is the maximum limit that can’t be exceeded in any case.
- <item>: This is the resource you’re setting a limit on, like the number of open files.
- <value>: This is the number you set as the limit.
For instance, if you want to set a limit on the number of open files for a group named developers, you could add or edit the entry like this:
@developers soft nofile 800
@developers hard nofile 1000
When applied, any user in the developers group can open up to 800 files (the soft limit) but can increase this limit up to 1000 files (the hard limit) if needed..
In this tutorial, we showed you how to use ulimit and limits.conf to control how your Linux system uses its resources. These tools help keep your system running well, even when it’s very busy or many people are using it. With ulimit, you can set limits that last for a short time. For long-term changes, we recommend making changes within the limits.conf file.
RedSwitches offers the best dedicated server pricing and delivers instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.
Q. What is the maximum stack size limit that can be set using ulimit?
The maximum stack size limit, which controls the memory allocated for function calls and local variables in a process, can be set using the ulimit command. For example, ulimit -s 10240 sets the maximum stack size to 10 MB.
Q. How does ulimit control the size of files written by processes?
The ulimit command can set limits on the size of files written by processes. For instance, ulimit -f 500 would limit the size of files created by the user’s processes to 500 blocks (with block size typically being 512 bytes).
Q. What does the -a flag do in the ulimit command?
The -a flag in the ulimit command is used to display all the current user limits in a readable format, including file size, CPU time, memory usage, and other types of limits.
Q. How do I modify the file limit using ulimit?
You can modify the file limit, which is the number of open files allowed for a process, using ulimit -n. For example, ulimit -n 2048 sets the file limit to 2048 open files.
Q. Can ulimit be used without root access?
Yes, users can use ulimit without root access to modify their own process limits within the constraints set by the system’s hard limits. However, only users with root access can change hard limits.
Q. What is the role of a security configuration file in setting limits?
The limits.conf file, a key security configuration file in Linux, is used to set permanent system-wide resource limits. This file defines both soft and hard limits for various system resources.
Q. How does ulimit manage max memory size?
The ulimit command can set a limit on the maximum memory size that processes can use. For example, ulimit -m can be used to set the maximum resident set size, although its effectiveness varies across different Linux distributions.
Q. Is ulimit a built-in Linux shell command?
Yes, ulimit is a built-in Linux shell command used for controlling the resources available to the shell and processes started by it.
Q. Can you give examples of ulimit options for setting correct ulimit values?
Sure! For setting correct ulimit values, you can use options like -t for CPU time, -u for the number of processes, and -v for virtual memory size. For instance, ulimit -t 60 sets the CPU time limit to 60 seconds.