Sunday, February 15, 2009

/etc/security/limits.conf file

How to: Prevent a fork bomb by limiting user process

Posted By Vivek Gite On November 27, 2007 @ 5:28 pm In CentOS, Debian Linux, Howto, Linux, RedHat/Fedora Linux, Security | 13 Comments
[1]

Earlier, I wrote about fork bomb [2], few readers like to know about getting protection against such attacks:

How do I protect my system from a fork bomb under Linux?

Limiting user processes is important for running a stable system. To limit user process just add user name or group or all users to /etc/security/limits.conf file and impose process limitations.
Understanding /etc/security/limits.conf file

Each line describes a limit for a user in the form:

Where:

* can be:
o an user name
o a group name, with @group syntax
o the wildcard *, for default entry
o the wildcard %, can be also used with %group syntax, for maxlogin limit
* can have the two values:
o "soft" for enforcing the soft limits
o "hard" for enforcing hard limits
* can be one of the following:
o core - limits the core file size (KB)
* can be one of the following:
o core - limits the core file size (KB)
o data - max data size (KB)
o fsize - maximum filesize (KB)
o memlock - max locked-in-memory address space (KB)
o nofile - max number of open files
o rss - max resident set size (KB)
o stack - max stack size (KB)
o cpu - max CPU time (MIN)
o nproc - max number of processes
o as - address space limit
o maxlogins - max number of logins for this user
o maxsyslogins - max number of logins on the system
o priority - the priority to run user process with
o locks - max number of file locks the user can hold
o sigpending - max number of pending signals
o msgqueue - max memory used by POSIX message queues (bytes)
o nice - max nice priority allowed to raise to
o rtprio - max realtime priority
o chroot - change root to directory (Debian-specific)

Login as the root and open configuration file:
# vi /etc/security/limits.conf
Following will prevent a "fork bomb":
vivek hard nproc 300
@student hard nproc 50
@faculty soft nproc 100
@pusers hard nproc 200

Above will prevent anyone in the student group from having more than 50 processes, faculty and pusers group limit is set to 100 and 200. Vivek can create only 300 process. Please note that KDE and Gnome desktop system can launch many process.

Save and close the file. Test your new system by dropping a form bomb:
$ :(){ :|:& };:

Article printed from nixCraft: http://www.cyberciti.biz/tips

URL to article: http://www.cyberciti.biz/tips/linux-limiting-user-process.html


Rubick

Could you tell me what it’s the difference between hard and soft limits?
People told me soft is like warning and hard is real max limit, but I’m not sure


vivek

Yup, you are correct about soft and hard limit. For example, following will prevent anyone in the student group from having more than 50 processes, and a warning will be given at 30 processes.

@student soft nproc 30
@student hard nproc 50


Sergei Vasilyev

I wonder how to limit number of used cpu cores per user or per user process in case when process is multithreaded and server has multiply number of CPU.


Joshi

i think this can be done via: apt-get install cpulimit



Robert Delahunt

I don’t see any info for doing it without PAM, so here’s some info (for us Slackware people, etc, and others not using PAM):

Put this in /etc/profile.conf:

ulimit -u 100

where this is the limit of processes anyone can run. Be warned that it could cause problems if you don’t know how many typical processes you run, so play with ps aux | wc -l and other stuff to check how many you would need. Cheers

No comments:

Post a Comment