Linux – Limit user to N CPU cores

central-processing-unitlinuxuser-permissions

I want to limit a user on my system to only be able to use one core of my system.

After searching, I found this, but I can't figure out how to use it.

I would also like to limit the use of that core to a specific percentage to prevent abuse.

Best Answer

The approach of limiting cores/cpu usage is most likely not what you want. Is it that you have a user who likes to run multicore/intensive tasks, and it ends up slowing down the whole server? The solution is to "nice" that user automatically.

In your /etc/security/limits.conf, you would want to add an entry for your abusive user (-19 to 19, with 19 being the lowest priority):

username    -       priority        19

Basically this means setting that user to run at a lower priority. If nobody else is running processes, then this user can consume the extra available CPU (idle CPU is wasted CPU). But if anything else is running, this user's processes will yield CPU. I've heard that servers are designed to run optimally at 100% cpu, and that you can't bank unused CPU cycles to use later.

Background: I have a Linux server that runs at 100% CPU 24x7 on all cores. It simultaneously runs computation intensive financial analysis software, a MySQL DB, a Tomcat instance, and Apache/PHP. The financial software will eat as much CPU and cores they can get, so I have them at priority 19. I want my Tomcat and Apache/PHP servers to be responsive, so I set them at -2 or -3. The web pages come up just as fast with or without the financial software running, thanks to properly managing priorities.