Get skill in using Ulimit Command in Linux to Limit System Resources

Limit System Resources with ulimit Command in Linux

In Linux, you can control everything! The resources of the system must be managed to not be deleted on intention or accidentally which leads to some troubles for users on the system. To do this, admins need to create restrictions for the user and allocate resources in the most effective manner possible based on requirements. The ulimit command in Linux helps system administrators achieve this purpose and limit the available resources of the system such as RAM or disk space for other users.

Understanding the ulimit command in Linux helps you know what is ulimit and how to use it, especially in environments with multiple users. Using the ulimit command in Linux, you will be able to limit the amount of CPU, the memory, the number of open files, and the number of processes. This article presents useful examples to explain the ways you can limit resource usage. Ultimately, you will get skilled in limiting the amount of system resources for different users consume to ensure they only receive what they require.

What is the ulimit Command in Linux?

ulimit is a built-in Linux shell command which is used to view, adjust, or restrict the current user’s resource usage. Using the ulimit command in Linux, admins can control system resource allocations which is essential for any system. To stop programs from using too many system resources, administrators can set limits on how much memory and CPU time a program can use.

There is a security configuration file connected to ulimit. Although the precise location may differ, it usually looks like /etc/security/limits.conf. You can rapidly edit that setting with ulimit. After a system reboot, changes made to the file remain in effect. To change the limits.conf file, you must have superuser permissions.

Hard limit and Soft limit

There are two types of limits in Linux: soft limits and hard limits. Since users can also adjust ulimit settings, soft and hard limits would be important. In this way, as an administrator, you might want your user to stay around a specific value. Then, you might set a strict restriction that the user could not go beyond.

The associated resource’s kernel-enforced values are the soft resource limits. Any user can manage the soft limit, and its maximum value is limited to what can be achieved within the hard limit. The soft limit is capped by the hard limit.

Run the following command to see the current user’s detailed soft limits:

ulimit -Sa

The hard resource limit specifies a user’s physical resource limit. The maximum value for the soft limit is also known as the hard limit. It is only possible for root users to modify the hard limit.

Run the following command to see the current user’s detailed hard limits:

ulimit -Ha

If there are no -H or -S flags set, the restriction applies to both the soft and hard limits.

Note: Certain limitations could be hard limitations, which means that once they are established, they can’t be increased.

ulimit Syntax

The basic syntax for using this command is as follows:

ulimit [options][limit]

To view the resource amount that the current user has access to, run the command below:

ulimit
Output

:~$ ulimit
Unlimited
:~$

In this way, you find the limit available for the resources of the user. As you see in the above output, the user has unlimited system resources. There are some flags to help you view or set individual resource limits. Stay with us on this guide to review the most common uses of the ulimit command in Linux.

ulimit options in Linux

To limit individual values for system resources, ulimit options can be used. Take a look at the below table to see what a flag does when comes with the limit command in Linux.

OptionDescription
-aLists all current resource limits.
-bThe maximum socket buffer size.
-cThe core dump size, expressed in the number of 512-byte blocks.
-dThe data area size, in kilobytes.
-eThe highest process scheduling priority (nice).
-fThe file size limit in blocks when using the [limit] parameter. Not specifying a [limit] instructs the command to report the file size limit.
-HThe hard limit for the given resource
-iThe maximum size allowed for locking in memory.
-kThe queue allocation number limit.
-lThe pending signal number limit.
-mThe physical memory size, in kilobytes.
-nThe maximum number of file descriptors that a process can have.
-PThe pipe buffer size.
-pThe maximum number of pseudoterminals.
-qThe maximum number of bytes in POSIX message queues.
-rThe maximum number of threads a process can have.
-RThe maximum process running time, expressed in microseconds.
-sThe stack size, in kilobytes.
-SThe soft limit for the given resource. Any process can change the soft limit.
-tSpecifies a process' maximum running time, in seconds.
-TThe thread number limit.
-uSpecifies how many processes a user can create.
-vThe maximum virtual memory available for processes.
-xThe maximum number of file locks.

Prerequisites to Use ulimit Command in Linux

To let this tutorial work correctly, provide the options below and move on.

  • A Server running Linux VPS.
  • A non-root user with sudo privileges.
  • Access to a Terminal.

Using ulimit Linux Command to Limit System Resources [7 Examples]

Linux/Unix system administrators must keep monitoring the system performance to be able to control system resource allocations or limit resource usage if it is required. So, before checking out the system resource amounts, it is a good idea to use a Linux Network Monitoring tool. Let’s learn How to Use the limit Command in Linux through the below practical examples.

Example 1. Check Resources Details

When you need to see a list of all resource limits for the current user, use the -a flag. This will give you the details of all resources available.

ulimit -a

Following the execution of the command, a list of resources and the currently logged-in Linux user’s limit will be displayed.

Note: You must include the username after the -a flag to find the details of the resources that are accessible for a user other than the one who is presently signed in.

Example 2. Limit the Number of Processes with Linux ulimit Command

In this example of the limit command in Linux, you will learn how to limit the number of processes that a user can create. Using the flag -u with the ulimit command enables you to set a maximum process number limit for the user:

For example, to limit the process number to 15, run:

ulimit -u 15

Limiting the maximum number of processes that a user can have stops them from consuming all the system’s resources. Restricting the process number also mitigates the negative consequences of possible attacks, like fork bombs.

Note: Getting familiar with Dangerous Linux Commands helps you know why fork bombs and some other Linux commands are destructive.

Example 3. File Size Limit

If you consider limiting the size of a file that the user can make, you can use the -f flag which sets the max size for a user to make.

For example, to limit the file size to 100 KB or less, run:

ulimit -f 100

Example 4. Limit Max Amount of Virtual Memory using ulimit in Linux

In this example, you will learn how to prevent thrashing with the use of ulimit in Linux. Limiting the virtual memory available for a process leads you to achieve this purpose. To do this, you just need to use the -f flag and set the maximum virtual memory amount available to a process.

Run the command below to stop the virtual memory from using up all the memory:

ulimit -v 500

In this way, you can limit the virtual memory available for a process to 500 KB.

Example 5. Set ulimit Command in Linux to Change Soft or Hard Limit

To use linux ulimit command to change the soft or hard limit, you need to edit limits.conf file.

Open up a terminal and follow the below steps to edit the values in the limits.conf file:

  • Change the directory to /etc/security:
cd /etc/security
  • Using your favorite text editor, open the limits.conf file:
nano limits.conf

Finally, edit the existing entries (Domain, type. Item, value) to change the limit values. You can also add a new one. The general syntax of ulimit value in Linux goes like this:

[domain] [type] [item] [value]

Note: You will need to make modifications to the security file as root if you wish to permanently alter any user’s access limits.

Example 6. Limit Open Files Numbers

The ulimit command in Linux is also used for limiting the number of files a user is allowed to open simultaneously. To do this, you need to use -n flag with the ulimit command:

For example, to set the number of open files to 10, type:

ulimit -n 10

Example 7. Limit the Core file Size

To check the available limit of the core file size for the current user, ulimit command in Linux can be used with the -c flag.

ulimit -c

In this way, you will see the available limit of the core file size for the current Linux user. Using the -c flag, you can limit the file size if you give a limit value to it.

For example, to set the maximum core file size to 15, run:

ulimit –c 15

That’s it! Now, you know how to check ulimit for a user in Linux and How to set ulimit in Linux.

The ulimit command is essential for systems that have multiple users. Linux users track and monitor system resources to control them. Load Average in Linux allows Linux users to monitor system resources.

Conclusion

In this article, you learned about the ulimit command and the different uses of this command. With the help of examples, you know how to use the ulimit command in Linux. This allows you to prevent users or programs from using system resources amount. Linux system administrators may find this tutorial helpful and essential since setting reasonable restrictions is the best strategy to stop unrestricted resource use in shared environments, as it negatively affects the experience of other users.

To fix issues, use the command ulimit -H -S to reset the limits to their default values. Let us know if you use any ulimit command in Linux which was not explained in this guide.

Leave a Reply

Your email address will not be published. Required fields are marked.