Atlantic.Net Blog

How to Control System Resource Usage with ulimit in Linux

Ulimit is a command-line utility in a Linux-based operating system used to allocate and limit resources. You can use it to control resources at the global, group, and user levels. Setting the right limits will make your system works optimally. You can use ulimit to set restrictions on the resources used by a process so that only important processes on your servers can keep running.

In this post, we will show you how to control system resource usage with ulimit in Linux.

Basic Syntax

The basic syntax of the ulimit command is shown below:

ulimit [option]

A brief explanation of each option is shown below:

  • -a: Display the limits for all the users.
  • -d: Define the size of the data area.
  • -c: Define the size of the core dump.
  • -e: Used to define the priority.
  • -s: Define the maximum stack size.
  • -u: Define the maximum number of user processes.

By default, all resource limit is saved in the file /etc/security/limits.conf.

How to Use ulimit

To display the resource limit of all user, run the following command:

ulimit -a

Output:

core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 15237
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 15237
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

To display the resource limit for a specific user, run the following command:

ulimit -a root

Output:

core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 15237
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 15237
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

To display the soft limit of a current user, run the following command:

ulimit -S

To display the hard limit of a current user, run the following command:

ulimit -H

To check the hard limit allocate on the maximum number of user processes, run the following command:

ulimit -Hu

Output:

15237

To change the default hard limit temporarily, run the following command:

ulimit -u 20000

To change the hard limit permanently, edit the /etc/security/limits.conf file:

nano /etc/security/limits.conf

Add the following line:

root    hard    nproc   20000

Save and close the file when you are finished.

To check the open files limit, run the following command:

ulimit -n

To change the open files limit, run the following command:

ulimit -n 1000

To check the max memory size limit, run the following command:

ulimit -m

To change the max memory size limit, run the following command:

ulimit -m 2000

Conclusion

In the above guide, you learned how to limit system resources with the ulimit command. ulimit will help you to make your system run with optimal performance. Try it today on your Atlantic.Net VPS hosting account!

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year