How to Configure ulimit values permanently on Linux

In a Unix-based system, every process is subject to various resource limitations set by the operating system. One of these limitations is the maximum number of open files that a process can have, which is controlled by the ulimit command. The ulimit command is used to set or display these limits, and can be used to control the maximum number of open files, the maximum amount of memory, the maximum CPU time, and other resources. In this blog, we will discuss how to configure ulimit values permanently on Linux.

Configure ulimit values permanently on Linux

Understanding Ulimit 

Before we dive into how to change ulimit values, it’s important to understand what ulimit is and how it works. Ulimit is a command-line utility that is used to control resource limits for processes running on a Unix-based system. These resource limits can include the maximum number of open files, the maximum amount of memory, and the maximum CPU time, among other things.

The ulimit command has two types of limits:

  • Hard limit
  • Soft limit

The hard limit is the absolute limit that a process can never exceed, while the soft limit is the current limit that can be changed by the process itself. When a process tries to exceed the soft limit, it receives a warning, and if it tries to exceed the hard limit, it is terminated.

By default, the ulimit values are set to a relatively low value, which can cause problems for some applications. For example, if a web server is running out of file descriptors, it may not be able to accept new connections, causing the server to become unresponsive. To prevent this, we can increase the ulimit values permanently for a user or all users.

Changing Ulimit values for a single user

To change the ulimit values permanently for a single user, we need to modify the shell configuration file for that user. The shell configuration file is usually located in the user’s home directory and is named .bashrc or .bash_profile, depending on the shell used by the user. To change the ulimit values for a user, follow these steps:

1. Log in as the user whose ulimit value you want to change, or switch to that user account using the su command.

2. Open the user’s shell configuration file using a text editor such as vi or nano.

3. Add the following line at the end of the file:

ulimit -n <new_limit>

Replace “<new_limit>” with the desired value for the maximum number of open files for the user.

4. Save the changes and exit the editor.

5. To apply the changes, either logout and log back in or run the following command to reload the shell configuration file:

source ~/.bashrc

Or

source ~/.bash_profile

6. To verify that the new limit is in effect, run the following command:

ulimit -n

This should display the new limit that you have set.

Changing Ulimit values for all users

To change the ulimit values permanently for all users, we need to modify the system-wide shell configuration file. The system-wide shell configuration file is usually located in the /etc/security/ directory and is named limits.conf. To change the ulimit values for all users, follow these steps:

1. Log in as the root user or switch to the root account using the su command.

2. Open the limits.conf file using a text editor such as vi or nano.

3. Add the following lines at the end of the file to set the soft and hard limits for all users:

* soft nofile <new_soft_limit>

* hard nofile <new_hard_limit>

Replace “<new_soft_limit>” and “<new_hard_limit>” with the desired values for the maximum number of open files for all users.

4. Save the changes and exit the editor.

5. To apply the changes, either log out and log back in or run the following command to reload the shell configuration file:

sysctl -p

6. To verify that the new limits are in effect, run the following command:

ulimit -n

This should display the new limits that you have set.

Understanding the limits.conf file

The limits.conf file is used to set resource limits for all users on a Unix-based system. The file consists of a series of entries, each of which sets a resource limit for a group of users or for all users. Each entry has the following format:

<domain> <type> <item> <value>

Where:

  • domain: Specifies the domain to which the limit applies. The domain can be a user, a group, or an wildcard (*).
  • type: Specifies the type of limit. The type can be soft or hard.
  • item: Specifies the resource for which the limit is being set. The item can be nofile (the maximum number of open files), nproc (the maximum number of processes), or others.
  • value: Specifies the value of the limit.

For example, the following entry sets the soft and hard limits for the maximum number of open files for all users to 65535:

* soft nofile 65535

* hard nofile 65535

Tips for Setting Ulimit Values

When setting ulimit values, there are some things to keep in mind:

  1. Be careful not to set the limits too high, as this can cause instability and performance problems.
  2. Always test the changes before deploying them in a production environment.
  3. Check the system documentation to ensure that you are using the correct configuration file and syntax for your specific operating system.
  4. Remember that setting ulimit values permanently can have system-wide implications, so make sure you know what you are doing and test your changes carefully.

Certainly! Here are a few examples of how you might use ulimit to set resource limits for different use cases:

1. Web server with many concurrent connections

Suppose you are running a web server and need to support a large number of concurrent connections. You might want to increase the maximum number of open files for all users to accommodate the high volume of connections. To do this, you could add the following lines to the /etc/security/limits.conf file:

* soft nofile 65535

* hard nofile 65535

This would set the soft and hard limits for the maximum number of open files to 65535 for all users, ensuring that your web server can handle a large number of concurrent connections.

2. Development environment with many processes

In a development environment, you might be running many processes simultaneously, such as multiple instances of an IDE, a build tool, and a testing framework. To avoid hitting the maximum number of processes limit, you could set a higher value for the maximum number of processes for your user account. To do this, you could add the following line to the ~/.bashrc file:

ulimit -u 4096

This would set the maximum number of processes for your user account to 4096, allowing you to run multiple processes simultaneously without hitting the limit.

3. System with high I/O load

If you are running a system with a high I/O load, such as a database server, you might want to increase the maximum number of open files for the user account that runs the database software. To do this, you could add the following lines to the /etc/security/limits.conf file:

dbuser soft nofile 8192

dbuser hard nofile 8192

This would set the soft and hard limits for the maximum number of open files to 8192 for the user account named “dbuser”. This would ensure that the database software can open a large number of files simultaneously, improving its performance.

4. User account with specific needs

Sometimes you might have a user account with specific needs that require higher or lower resource limits. For example, you might have a user who needs to run a memory-intensive application, in which case you might want to increase the maximum amount of memory the user can use. To do this, you could add the following line to the /etc/security/limits.conf file:

johndoe soft rss 4096

This would set the soft limit for the maximum amount of resident set size (i.e., memory) that the user “johndoe” can use to 4 GB.

5. User account with limited resources

In some cases, you might want to limit the number of resources that a user can consume in order to prevent them from overloading the system. For example, you might have a user account that is used for running automated scripts, and you want to limit the amount of CPU time that the scripts can use. To do this, you could add the following line to the /etc/security/limits.conf file:

scriptuser soft cpu 1800

This would set the soft limit for the maximum amount of CPU time that the user “scriptuser” can use to 30 minutes (1800 seconds).

6. User account with specific file size limits

You might have a user who needs to work with large files, but you don’t want them to be able to create files that are too large and take up too much disk space. To set a limit on the maximum file size for a user, you could add the following line to the /etc/security/limits.conf file:

largefileuser hard fsize 10000000

This would set the hard limit for the maximum file size for the user “largefileuser” to 10 MB.

7. System with limited memory

If you are running a system with limited memory, you might want to set a lower maximum heap size for Java applications in order to prevent them from consuming too much memory and causing the system to crash. To do this, you could add the following line to the /etc/security/limits.conf file:

* hard as 512000

This would set the hard limit for the maximum address space (i.e., virtual memory) to 512 MB for all users, ensuring that Java applications do not consume too much memory.

8. User account with high priority

In some cases, you might have a user account that needs to have higher priority than other users in order to ensure that their processes are given more CPU time. To set a higher priority for a user, you could add the following line to the /etc/security/limits.conf file:

highpriorityuser hard priority 10

This would set the hard limit for the priority level for the user “highpriorityuser” to 10, ensuring that their processes are given higher priority than other users.

These are just a few examples of how you might use ulimit to set resource limits for different use cases. By understanding how to configure ulimit values, you can help ensure that your Unix-based system performs optimally and remains stable, regardless of the workload or the number of users.

Conclusion

In this blog, we discussed how to change ulimit values permanently for a user or all users in a Unix-based system. We also discussed the limits.conf file and the syntax used to set resource limits for all users. Remember that setting ulimit values permanently can have system-wide implications, so make sure you know what you are doing and test your changes carefully. By properly configuring ulimit values, you can help ensure the stability and performance of your Unix-based system.

See also:

List of monitoring tools 

Linux Blogs

AWS Cloud Blogs

Database Blogs

DevOps Blogs

Interview Questions & Answers

Docker Blogs

Google Cloud Blogs