The ulimit command in Linux allows you to manage system resources on a per-user basis by limiting what a user or process can use. It is especially useful in a VPS (Virtual Private Server) system, where it helps optimize productivity, manage workflow, and prevent resource consumption. This guide covers everything you need to know about ulimit, from basic setups to advanced configurations. By the end, you’ll be equipped to maximize your VPS’s potential through effective resource management.

The ulimit command allows Linux users to control system resources on a per-user basis, restricting resources like file creation, memory usage, and a number of processes. It’s very useful for VPS hosting, where you want to prevent individual processes from overwhelming the server.
It’s an ulimit in a VPS protecting you from unnecessary resources using applications or users. The approach above balances the resource allocation and remains stable and efficient.
In VPS hosting, multiple applications or users may share resources, which can easily lead to overuse if left unchecked. Here’s how ulimit is particularly valuable in a VPS setup:
Knowing the two types of ulimit limits is crucial:
For example, if the soft limit for file descriptors is set to 1024 and the hard limit to 2048, a user can raise their limit to 2048 but not beyond. This setup keeps resource use under control without rigidly restricting users.
You can check current ulimit settings by running:
ulimit -a

To adjust limits, use the following syntax:
ulimit -[option] [value]
For example, setting the maximum number of open files to 1024:
ulimit -n 1024

Below are some frequently used ulimit options for VPS environments:
With each option, you can manage certain resource types, giving added flexibility to your VPS management.
Here’s a quick look at common ulimit options and their relevance to VPS:
| Option | Description |
|---|---|
| -n | It sets the maximum number of open files. Important for work done on many files or many connections. |
| -u | It limits the number of processes that a user can create; this is helpful for applications that use multiple methods. |
| -f | Restricts the size of files a user can create. Useful for disk space management. |
| -v | Controls the maximum amount of virtual memory available to a process, managing memory-hungry applications. |
| -s | It defines stack size for a process but is required for applications with particular stack size needs. |
Using these options allows you to precisely tailor resource use according to the needs of your VPS setup.
Let’s go through setting up specific limits for common scenarios on a VPS.
File descriptors represent open files, network connections, and sockets. Managing file descriptors is crucial in setups that handle numerous simultaneous connections.
To set a soft limit of 1024 and a hard limit of 2048 for open files:
ulimit -Sn 1024

ulimit -Hn 2048

For applications that create multiple processes, controlling the number of processes can prevent server strain.
Set a soft limit of 512 and a hard limit of 1024 processes:
ulimit -Su 512

ulimit -Hu 1024

Restricting the file size can help manage disk space if users are generating large files.
To limit the file size to 100 MB:
ulimit -f 102400

(Note that ulimit specifies file size in 1 KB blocks, so 100 MB = 102400 KB.)
Memory management is crucial for VPS performance. Setting a virtual memory limit can help keep applications in check.
To set a 1 GB limit:
ulimit -v 1048576

(Memory is measured in KB; 1 GB equals 1048576 KB.)
In a VPS with multiple users, you should set unique ulimit values to keep resources balanced. Here’s how to configure limits per user:
Open /etc/security/limits.conf in a Text editor:
sudo nano /etc/security/limits.conf

[username] [limit_type] [limit_item] [value]
Example for setting open file limits for the user “john”:
john hard nofile 8192
john soft nofile 8192

This method allows you to customize resources based on each user’s needs, improving overall VPS efficiency.
You can set the global configuration of specific system-wide resource limits in a Linux server for all the users through some specific global configuration files. This method guarantees that limits are applied to every user and session on the server side so they have a safe, controlled environment to use resources.
The /etc/security/limits.conf file is commonly used to define system-wide ulimit settings. Here’s how to apply limits globally:
Open the limits.conf File:
Use a Text editor with root privileges to open /etc/security/limits.conf:
sudo nano /etc/security/limits.conf

To apply limits for all users, use the wildcard * in place of a specific username. This action applies the limits uniformly across all accounts.
* hard nofile 8192
* soft nofile 8192

*: Applies the settings to all users on the server.
hard: Sets the hard limit, the absolute maximum that users cannot exceed.
soft: Sets the default limit that users start with but can increase up to the hard limit if necessary.
nofile: Represents the maximum number of open files.
4096 and 2048: Maximum allowable values for hard and soft limits, respectively.
Example Configurations:
Here are examples of commonly applied global settings:
* soft nofile 2048 # Default open file limit
* hard nofile 4096 # Max open file limit
* soft nproc 1024 # Default max number of processes per user
* hard nproc 2048 # Max number of processes per user
* soft stack 10240 # Default stack size
* hard stack 32768 # Max stack size
* soft fsize 102400 # Max file size in KB

In addition to limits.conf, you can configure ulimit settings in the /etc/profile to enforce limits every time a shell session is started. This approach applies the settings globally whenever a user starts a new shell.
Open /etc/profile:
sudo nano /etc/profile

Add your desired ulimit commands at the end of the file. Here are some examples:
ulimit -n 4096 # Set max open files to 4096
ulimit -u 2048 # Set max user processes to 2048
ulimit -s 10240 # Set stack size to 10240 KB

Apply Changes:
To apply these changes, you can either reboot the system or use the following command to reload the profile:
source /etc/profile
If you’re managing a Linux server with services started through systemd, you may need to set ulimit limits within individual service files. Such a configuration enables each service to apply the defined limits during startup.
Locate the Service File:
Service files are typically located in /etc/systemd/system/ or /usr/lib/systemd/system/. For example, if you’re configuring limits for the Apache :
sudo nano /etc/systemd/system/httpd.service
Under the [Service] section, add the desired limit directives. Here are some examples:
[Service]
LimitNOFILE=4096 # Max number of open files
LimitNPROC=2048 # Max number of processes
LimitSTACK=10240 # Stack size limit in KB

After editing the service file, reload systemd and restart the service to apply changes:
sudo systemctl daemon-reload
sudo systemctl restart httpd.service

After configuring global ulimit settings, it’s good practice to verify them. You can use the following commands to check the active limits:
Check Open Files Limit:
ulimit -n

Check Process Limit:
ulimit -u

View All Limits:
ulimit -a

It will display the current values for each resource limit, confirming that your configurations are active across all user sessions.
Settings adjusted through ulimit from the command line are temporary, lasting only for the current session. Here are ways to make ulimit settings persistent:
Service-Specific Limits: If configuring limits for a specific service (e.g., Apache or Nginx), set ulimit values in the service’s systemd configuration file, such as:
[Service]
LimitNOFILE=4096
Using these methods, you can keep ulimit configurations active across reboots and logins.
Here are some frequent issues when working with ulimit and ways to fix them:
Following these tips should help address most issues you’ll face with ulimit on a VPS.
For more control over your VPS, consider these advanced tips:
The ulimit command is a powerful solution for setting limits on resources in your Linux VPS, including file access, memory, and process numbers. Proper use of ulimit ensures that your server isn’t pounded up to crash or bottleneck. Whether you’re new to Linux or an experienced user, ulimit is a valuable tool for optimizing your VPS environment.

Vinayak Baranwal wrote this article. Use the provided link to connect with Vinayak on LinkedIn for more insightful content or collaboration opportunities.