Prerequisites
Before limiting user resources, make sure you have:
- SSH access to your VPS
- Root or sudo privileges
Method 1: Using /etc/security/limits.conf
Connect to your VPS:
ssh hxroot@YOUR_SERVER_IP -p 22
sudo nano /etc/security/limits.conf
Add limits for a user (e.g., "alice"):
alice soft nproc 10
alice hard nproc 20
alice soft nofile 100
alice hard nofile 200
alice soft core 0
alice hard core 0
alice soft cpu 60
alice hard cpu 120
Limit types explained:
- nproc – Maximum number of processes
- nofile – Maximum open file handles
- core – Core dump file size (0 disables)
- cpu – CPU time in minutes
- as – Address space limit (memory)
- rss – Resident set size (physical memory)
- data – Data segment size
- fsize – Maximum file size
Apply Changes (Reboot or re-login)
User must log out and log back in for limits to take effect.
Method 2: Using systemd cgroups (Per Service)
Edit service override:
sudo systemctl edit myapp.service
Add:
[Service]
CPUQuota=50%
MemoryMax=512M
TasksMax=20
Reload and restart:
sudo systemctl daemon-reload
sudo systemctl restart myapp
Method 3: Using cgroups directly (Advanced)
Create a control group:
sudo cgcreate -g cpu,memory:/limitedgroup
sudo cgset -r cpu.cfs_quota_us=50000 limitedgroup
sudo cgset -r memory.limit_in_bytes=536870912 limitedgroup
Run a process in the group:
sudo cgexec -g cpu,memory:/limitedgroup mycommand
Check Current User Limits
ulimit -a
For a specific user (switch user first):
su - alice
ulimit -a
Set Limits for All Users (Global Defaults)
In /etc/security/limits.conf:
* soft nproc 50
* hard nproc 100
* soft nofile 256
* hard nofile 512
Limits for root User
Add a separate section for root (usually not limited).
✅ User resource limits have been configured. This prevents any single user from overusing server resources.