Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So I have 4 GB RAM + 4GB swap. I want to create a user with limited ram and swap: 3 GB RAM and 1 GB swap. Is such thing possible? Is it possible to start applications with limited RAM and swap avaliable to them without creating a separate user (and not installing any special apps - having just a default Debian/CentOS server configuration, and not using sudo)?


So I opened terminall and typed into it ulimit command: ulimit -v 1000000 which shall be like 976,6Mb limitation. Next I called ulimit -a and saw that limitation is "on". Then I started some bash script that compiles and starts my app in nohup, a long one nohup ./cloud-updater-linux.sh >& /dev/null &... but after some time I saw:

enter image description here

(which would be ok if no limitations were applied - it downloaded some large lib, and started to compile it.)

But I thought I applied limitations to the shell and all processes launched with/from it with ulimit -v 1000000? What did I get wrong? How to make a terminal and all sub processes it launches be limited on ram usage?

share|improve this question
You can't put memory restrictions on a user as a whole, only on each process. And you can't distinguish between RAM and swap usage. If you want finer control, run the user's processes in a virtual machine. – Gilles Mar 16 '12 at 23:30
@Gilles pretty sure that virtual machines just use cgroups and namespaces, or derivatives of – RapidWebs Aug 15 '14 at 0:38
up vote 36 down vote accepted

ulimit is made for this. You can setup defaults for ulimit on a per user or a per group basis in


ulimit -v KBYTES sets max virtual memory size. I don't think you can give a max amount of swap. It's just a limit on the amount of virtual memory the user can use.

So you limits.conf would have the line (to a maximum of 4G of memory)

luser  hard  as   4000000

UPDATE - CGroups

The limits imposed by ulimit and limits.conf is per process. I definitely wasn't clear on that point.

If you want to limit the total amount of memory a users uses (which is what you asked). You want to use cgroups.

In /etc/cgconfig.conf:

group memlimit {
    memory {
        memory.limit_in_bytes = 4294967296;

This creates a cgroup that has a max memory limit of 4GiB.

In /etc/cgrules.conf:

luser   memory   memlimit/

This will cause all processes run by luser to be run inside the memlimit cgroups created in cgconfig.conf.

share|improve this answer
is such thing settable on useradd? – myWallJSON Mar 16 '12 at 13:41
@myWallJSON Not directly, but you can immediately add it to limits.conf, or you can setup a group with certain limits in limits.conf and add user to that group. – utopiabound Mar 16 '12 at 14:56
That's awesome! I didn't know you could do this! Great answer +1 – Yanick Girouard Mar 16 '12 at 16:50
@utopiabound: Updated my Q with some data I got trying to use ulimit. – myWallJSON Mar 16 '12 at 22:24
@f.ardelian Upgrade the kernel. Here's an article about how to do just that! – Daniel C. Sobral Feb 11 '13 at 1:05

You cannot cap memory usage at the user level, ulimit can do that but for a single process.

Even with using per user limits in /etc/security/limits.conf, a user can use all memory by running multiple processes.

Should you really want to cap resources, you need to use a resource management tool, like rcapd used by projects and zones under Solaris.

There is something that seems to provide similar features on Linux that you might investigate: cgroups.

share|improve this answer
Well, I suppose setting a cap on users login shell or something like that could be interpreted as "setting a limit for the user", since all processes would inherit from that shell? – amn Apr 18 at 14:31
@amn It won't. A user might simply open a new login shell to workaround such a limit. – jlliagre Apr 18 at 14:46
Right, that invalidates my assumption alright. – amn Apr 18 at 16:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.