limit cpu usage in linux

How to Limit CPU Usage Per User in Linux

Linux is a powerful multi user operating system used by organizations all over the world. It allows multiple users to easily work on a single system with shared resources. But it is always good to limit CPU usage per user in Linux to avoid resource bottlenecks and performance problems in your system. Without CPU Usage limits, it may happen that a single application by a user takes up most of CPU processing, causing other applications and users to face issues. In this article, we will learn how to limit CPU usage per user in Linux.


How to Limit CPU Usage Per User in Linux

In this article, we will learn how to limit the number of processes allowed by each user and how to check & modify current system limits. For this purpose, you need root access to your system.

To configure user limits you need to open the following file in a text editor.

$ vi /etc/security/limits.conf

It consists of configuration used by ulimit, created by pam_module. Each record in this file has the following format.

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

In the above record, the different parameters mean the following:

  • Domain – includes usernames, groups, guid ranges etc
  • Type – soft and hard limits
  • Item – system resource to be limited – core size, file size,  nproc etc
  • Value – value for limit

Here is a sample record base don above syntax.

@test_user          hard           nproc                20

The above line sets a hard limit of 20 processes on user group test_user.

You can add/edit/remove records to set limits for users, processes, groups, etc. But please take a backup of this file before you make any changes to it, so that you can easily revert back to old values, if things don’t work out as expected.

If you want to view the limit for a given command, you can do so with the cat command. Here you need to replace PID with the PID of process whose limit you want to find out.

# cat /proc/PID/limits

You can find out the PID of process using ps command. Here is an example to find limits for process with PID 2487.

# cat /proc/2487/limits

Here is a sample output of above command.

Limit                     Soft Limit           Hard Limit           Units     
Max cpu time              unlimited            unlimited            seconds   
Max file size             unlimited            unlimited            bytes     
Max data size             unlimited            unlimited            bytes     
Max stack size            8388608              unlimited            bytes     
Max core file size        0                    unlimited            bytes     
Max resident set          unlimited            unlimited            bytes     
Max processes             32042                32042                processes 
Max open files            1024                 4096                 files     
Max locked memory         65536                65536                bytes     
Max address space         unlimited            unlimited            bytes     
Max file locks            unlimited            unlimited            locks     
Max pending signals       32042                32042                signals   
Max msgqueue size         819200               819200               bytes     
Max nice priority         0                    0                    
Max realtime priority     0                    0                    
Max realtime timeout      unlimited            unlimited            us   

As you can see above, it shows all the specific system resources such as stack size, core file size, open files, etc.

In the above output, you will see separate columns for hard and soft limits. The main difference between soft and hard limit is that hard limit, once set by the user, cannot be changed programmatically or otherwise. Soft limit can be changed by another user or process. Hard limit is the ceiling value for soft limits. Many times, users & processes self limit their system usage by reducing their soft limits.

In this article, we have learnt how to limit CPU usage per user in Linux. You can use these steps in almost every Linux distribution.

It is always a good practice for system administrators to regularly monitor the system limits of users and modify them as per their requirement. In most cases, the default soft and hard limits are enough but if you want to get the most performance out of your system, you may need to customize them as per your requirement.

Also read:

How to Automate mysql_secure_installation
How to List SFTP Users with System Access
How to Reset Jenkins Admin User Password
How to Check CP Progress in Linux
How to Run Fsck to Fix File System Error

Leave a Reply

Your email address will not be published. Required fields are marked *