119

What is the difference between hard and soft limits in ulimit?

For number of open files, I have a soft limit of 1024 and a hard limit of 10240. It is possible to run programs opening more than 1024 files. What is the soft limit for?

1
  • 7
    Are you talking about ulimit -n? Try running a shell with a very low value (bash -c 'ulimit -n 4; exec 3>a; exec 4>b; exec 5>c'). What's the output? Jan 20, 2012 at 17:23

2 Answers 2

107

A hard limit can only be raised by root (any process can lower it). So it is useful for security: a non-root process cannot overstep a hard limit. But it's inconvenient in that a non-root process can't have a lower limit than its children.

A soft limit can be changed by the process at any time. So it's convenient as long as processes cooperate, but no good for security.

A typical use case for soft limits is to disable core dumps (ulimit -Sc 0) while keeping the option of enabling them for a specific process you're debugging ((ulimit -Sc unlimited; myprocess)).

The ulimit shell command is a wrapper around the setrlimit system call, so that's where you'll find the definitive documentation.

Note that some systems may not implement all limits. Specifically, some systems don't support per-process limits on file descriptors (Linux does); if yours doesn't, the shell command may be a no-op.

4
  • 7
    +1 for the soft limits use case. Resident set size limit (ulimit -m, RLIMIT_RSS) is an example of a limit that isn't effective on Linux anymore. Virtual memory limit (ulimit -v, RLIMIT_AS) works, though. Jan 20, 2012 at 17:53
  • 1
    @Gilles, do you mean without restarting the process, when we change the soft limit, it can be effect immediately?
    – Ryan
    Jun 29, 2013 at 2:52
  • 1
    @Ryan Yes, a program can change its own soft limit at any time by calling setrlimit (to the extent permitted by the hard limit unless running as root of course). Most programs don't have a command that lets the user do that, but you can try attaching to the program with a debugger and making it issue a setrlimit call, or under Linux you can call prlimit (for which I don't know of any shell utility). Jun 29, 2013 at 8:27
  • 2
    There is now a prlimit shell utility too.
    – telcoM
    Mar 26, 2019 at 9:16
3

The hard limit is for the security purpose. For a non-root user, he can only decrease the hard limit from the currently set hard limit; he cannot increase it. Increasing the hard limit can be done only by root user (or maybe with sudo privilege, not sure about that). What a non-root user can do is choose a limit (called soft limit) which can be in the range [0, hard limit] for its processes. Its the soft limit which is seen and taken in consideration by the processes.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .