Introduction
Before we discuss Linux ulimit
command, let's first understand-What is ulimit
Command ?
The "ulimit" command line utility stands for "User Limit." It is used to manage or track specific user resources. Limits and default values are present in the resources. To establish the maximum file size, the number of user processes, the amount of virtual memory, and many other parameters, these values can be changed based on the user's needs.
In this tutorial, you will understand ulimit
command in Linux. We will also address a few FAQs on Linux ulimit
command.
How Does ulimit Command Work on Linux?
The general syntax of the ulimit
command is as follows:
Syntax
ulimit [flags][limit]
The following components are a part of the syntax:
- ulimit: Refers to the "ulimit" command.
- flags: Indicates the "ulimit" command's supported flags and options.
- limit: Displays the "Hard" or "Soft" limit value for a specific resource.
Limitation Types
The "ulimit" essentially consists of two different types of resource limitations: the "Hard" limit and the "Soft" limit. Below is a description of both:
Soft Limit
The soft limits are the effective values supplied to the user for the actual processing. Although it can be raised, the user cannot raise the soft limit above the hard limit.
To obtain the detailed report of the soft limit, use the command below:
ulimit -Sa
Hard Limit
The maximum value that may only be altered by the root user is known as the "hard" limit. It also serves as the soft limit's upper limit or ceiling.
To view the detailed report on the hard limit, enter the command below:
ulimit -Ha
To learn about the built-in flags that the "ulimit" command supports, type the "-help" command in the terminal:
ulimit --help
To view the entire list of available options/flags, scroll down the page.
Examples of Linux ulimit Command
Now, examine the "ulimit" command's practical usage to comprehend how it functions on Linux.
Example 1: Get Brief Limit Report
The "ulimit" command typically returns the entire amount of resources that the current user has without any input.
As seen in the screenshot, use the "-a" flag to acquire a detailed or concise report on that resource.
ulimit -a
The output displays every resource along with any associated limitations.
Example 2: Display Maximum Users Process
The entire value of the maximum process that the user may occupy is represented by the "-u" flag. The following is how this flag is actually used:
ulimit -u
The maximum processes for the logged-in user are displayed as "7490" in the report.
Modify the Users Process Limit
Execute the "ulimit" command as follows to limit the processes of the currently logged-in "vegastack" or the specific user processes:
ulimit -u 20
It has been modified to "20" as the maximum process limit. This practice is not recommended because it limits each user's maximum process. It stops users from using all the system processes.
Example 3: Show Maximum File Size
The maximum size the user can create while using Linux is shown by the "-f" flag. Both "unlimited" and a defined size in "Kilobytes" are possible:
ulimit -f
The maximum file size is indicated as "unlimited" in the output.
Adjust the File size Limit
Setting or modifying the file size limit is another advantage of the "-f" flag. The "File1.txt" size restriction in this example is increased to "40" from the current "39k" limit:
ulimit -f 40 File1.txt
The error shown in the screenshot will appear if the user adds the content after changing the limit to "40k":
Example 4: Get Maximum Virtual Memory Size
The "-m" flag displays the maximum amount of virtual memory that the process can hold in "kilobytes". It might be limitless:
ulimit -v
The "unlimited" virtual memory can be seen in the output.
The "unlimited" output indicates that the current process uses all the RAM that is available. The disadvantage of a process having "unlimited" memory is that it slows down other processes with lower memory requirements.
Set the Specific Memory Size Limit
When using the "-v" flag with the allocated memory, a process's "unlimited" memory capacity can be restricted to a specific limit:
ulimit -v 1000
As a result, the virtual memory has been changed to "1000kB."
Example 5: Check Maximum Scheduling Priorities
The maximum scheduling priority that a logged-in user may provide to a process is indicated by the "-e" flag. Use the "ulimit" command with the "-e" flag in the following manner to do this:
ulimit -e
The user "itslinuxfoss" is not given any scheduling priority in this instance.
Example 6: Number of Opened Files
The maximum number of files that can be opened at once is indicated by the "-n" flag. It also goes by the name "file descriptor." The term "file descriptor" often refers to a numerical identifier that uniquely identifies an open file in the active operating system.
The "-n" command in this example shows that the process has "13" file descriptors:
ulimit -n
How to Modify the Hard and Soft Limits Manually?
The resource limits are specified in the "limit.conf" configuration file. The directory "/etc/security" contains this file. Changes to this file can only be made by the root user. To modify this file, you must be logged in as root or use the "sudo" command.
Run the following command using "sudo" to launch the "nano" text editor and open the "limit.conf" file. This file defines all the suitable limits:
nano /etc/security/limits.conf
The aforementioned command displays information about the "limit.conf" file in the "nano" editor "window":
To view all the information, scroll down the window.
Each entry in the file follows a structure, as seen in the display. Follow the entry's fundamental structure, which consists of the four parts "domain," "type," "item," and "value," to modify the default limit values and add new limit values. The screenshot above shows a description of these options.
That's all about the Linux "ulimit" command.
FAQs of Linux ulimit
Command
What is the syntax of the ulimit
command?
The basic syntax of the ulimit
command is ulimit [option] [value]
. The option can be any of the supported resource limit names, such as -c
for core file size or -n
for the maximum number of open file descriptors.
How can I use the ulimit
command to display current resource limits?
To display the current resource limits, simply run ulimit -a
or ulimit -Ha
. It will show the current limits and their corresponding soft and hard values.
Can I use ulimit
to set resource limits on a per-user basis?
Yes, you can use ulimit
to set resource limits on a per-user basis by modifying the user's shell configuration file (e.g., .bashrc
, .bash_profile
). Changes made to these files will affect the user's session when they log in.
How can I set a new resource limit using the ulimit
command?
To set a new resource limit, use the ulimit -S
or ulimit -H
command followed by the desired resource name and the limit value. For example, ulimit -S -n 1024
sets the maximum number of open files to 1024 for the current session
What is the difference between soft and hard resource limits?
Soft limits are the current limits enforced for a specific resource. Users can set and modify soft limits within the existing hard limits. Hard limits, on the other hand, are the maximum values that can only be changed by the superuser.
How can I modify the hard resource limit using ulimit
?
By default, regular users cannot modify the hard limits using ulimit
. Only the superuser (root) can change the hard limits by modifying system-wide configuration files or using the sudo
command.
How can I check the resource limits of another user using ulimit
?
By default, regular users cannot check the resource limits of other users. However, the superuser can run the su - username -c 'ulimit -a'
command to check the resource limits of a specific user.
Conclusion
The "ulimit" command on Linux is commonly used to list the resource limit utilized by a certain user. There are two types of limits: "Hard" and "Soft." The user can increase the resource limit in the default range. This tutorial illustrated a brief description of the "ulimit" command using practical examples.
If you have any queries, please leave a comment below, and we’ll be happy to respond to them.