Chat Zalo Chat Messenger Phone Number Đăng nhập
How to Use the ulimit Linux Command {With Examples} - phoenixNAP

How to Use the ulimit Linux Command {With Examples} – phoenixNAP

Getting Started

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.

How to use the ulimit command on Linux.

Prerequisites

A system

  • 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.

The

first section of the limits.conf file looks like this

: ulimit,

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:

ulimit

[flags][limit]

For example, to set the size of physical memory in kilobytes, type:

ulimit -m 15000

Flags

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:

H hard limit-S soft limit -a-b-c-d-e-f[limit][limit]-i-k-l-m-n-p-P-q -r-R-s-t-T-u-v-x

[limit] Parameter

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.

The limit

applies to both the soft limit and the hard limit if the –H and -S indicators are not specified.

Output values

The ulimit command has two

output

values: 0.

  • It marks a successful completion.
  • >0. An upper limit request was rejected or an error occurred.

Soft

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:

ulimit -Sa

Hard

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

-u

10

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.

For example:

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.

Limit

file size

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

to

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.

vim limits.conf

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>.

For example:

Be sure to uncomment the line when editing the configuration file

.

Conclusion

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.

Contact US