Programming Languages
Posted By Luca

Linux fork bomb and how to prevent it


A fork bomb is a piece of code that defines a function and runs it repetitively, until the system has no more computing resources. The system will also become unresponsive and the only solution to get it working again is reboot the system.

The code

The fork bomb code is (for example) like this one:

:(){:|:&};:

Explanation

First of all you define a function called “:”. The Syntax of the function is “:(){};”. Now in the function you will call the function itself recursively and send the output again to itself “:|:”. The “&” runs the function in the background. Now the fork bomb is done and you only need to call it with “:” at the end.

Live demonstration

For a live demonstration I have found a YouTube video:

How to prevent it

Preventing it is quite simple. You need to modify the system resources limits. Login to the server via SSH or open a new terminal window and edit the limits.conf file as root:

sudo vi /etc/security/limits.conf

Set a limit for the processes number for a specific user or a group:

username hard nproc 100
@groupname hard nproc 100

My name is Luca Costa, an enthusiastic system engineer. In this blog you will find articles about technolgy and more. Feel free to contact me!

View Comments
There are currently no comments.