By using AWS re:Post, you agree to the AWS re:Post Terms of Use

How do I set ulimit values in an Amazon Linux operating system?

4 minute read
0

I want to set ulimit values in Amazon Linux to increase the quotas for system resources.

Resolution

Note: It's a best practice to maintain backups of your instances and data. Before you troubleshoot, create an Amazon Elastic Block Store (EBS) backed Amazon Machine Image (AMI), or create snapshots of your Amazon EBS volumes.

To set ulimit values, run the ulimit command, or modify your system configuration files. To manage system resource quotas for individual users, run the ulimit command.

The ulimit command controls the following system quotas:

  • nofile: Maximum number of open file descriptors
  • nproc: Maximum number of processes
  • stack: Stack size
  • core: Core file size
  • memlock: Maximum locked-in-memory address space

There are two types of system quotas, hard and soft. The soft quota is the current quota that the systems enforces. Use soft quotas to increase or decrease quotas until your reach the hard quota. The hard quota is the maximum value that you can increase the soft quota to. Only the root user can increase hard quotas.

Before you set new ulimit values, run the following command to check the resource quotas for the session:

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

Note: To query a hard quota, replace -a in the ulimit command with -Ha.

Set temporary ulimit values

To set temporary ulimit values for the current shell session, run the ulimit command. Use the -S option to set the soft quota and the -H option to set the hard quota. For example, to change the maximum number of open files to 10000, run the following commands:

ulimit -Sn 10000
ulimit -Hn 10000

Note: Replace 10000 with the number that you want to change the maximum number of open files to.

If you're a root user, then you can run the following command to temporarily set the ulimit nproc value to unlimited:

ulimit -u unlimited

Set persistent ulimit values

To set persistent ulimit values across reboots, modify the /etc/security/limits.conf file.

It's a best practice to create a backup file of limits.conf with the filename limits.conf.bk in the /etc/security/ folder. To create a backup, run the following command:

cp /etc/security/limits.conf /etc/security/limits.conf.bk

Open the file with a text editor, and add the following lines to the :/etc/security/limits.conf file:

username soft nofile 65536
username hard nofile 65536
username soft nproc 4096
username hard nproc 8192

Note: Replace username with your username, 65536 with your maximum number of open files, and 4096 and 8192 with the maximum number of user processes. 

To apply the number for all users, use *:

* soft nofile 65536
* hard nofile 65536
* soft nproc 4096
* hard nproc 8192

To apply the changes, log out and then log back in.

To check the nproc open file quotas for a user, run the following commands:

Unlimited:

ulimit -u

Soft quota:

ulimit -Sn

Hard quota:

ulimit -Hn

To set the nproc quota to unlimited as a permanent update /etc/security/limits.conf, use the following configuration:

username soft nproc unlimited
username hard nproc unlimited

Note: Replace username with your username.

Best practices for ulimit values

When you set ulimit values, use the following best practices:

  • Set ulimit values for the specific requirements of your applications and processes.
  • Monitor your system's resource usage, and adjust ulimit values as needed.
  • Test your applications and processes with the new ulimit values to make sure that they function correctly.
  • Check whether to set systemwide or specific user quotas, and then adjust your configurations.

Important: If you set extremely high values or set the unlimited value, then you might experience security and stability issues.

Related information

How to set ulimit values on the Red Hat Enterprise Linux (RHEL) website

AWS OFFICIAL
AWS OFFICIALUpdated 2 months ago