Guides

Fork bomb :(){ :|:& };: explained, and how to protect a Linux server

What the bash fork bomb :(){ :|:& };: does, why not to run it, and how to stop one on Linux with ulimit, limits.conf nproc, systemd TasksMax and pids.max.

Published ~7 min read
On this page

Short answer: :(){ :|:& };: is the classic bash fork bomb. It defines a function named : that starts two copies of itself in the background, and every copy does the same, so the number of processes doubles until the server cannot start anything else. Do not run it; give every user and every service a process limit instead, so a runaway script hits that limit and not the whole machine.

bash · do not run this
:(){ :|:& };:        # the fork bomb: do NOT paste this into a shell

# the same fork bomb with a readable name: do NOT run this either
bomb() {
  bomb | bomb &
}
bomb

Nothing on this page was run as a fork bomb. The syntax is explained from the bash manual, the limits from the systemd 252, pam_limits and kernel documentation, and the numbers further down were read (read only) on one of our Debian 12 servers.

How :(){ :|:& };: works, piece by piece

PartWhat it does
:()Defines a function named :. In its default mode bash accepts any unquoted word without a $ as a function name, and functions are looked up before builtins, so this hides the builtin :, which normally does nothing.
{ ... }The function body. { and } are reserved words, which is why the space after the opening brace is required.
:|:Calls the function twice, joined by a pipe. Each command of a multi-command pipeline runs in its own subshell, a separate process.
&Runs that pipeline in the background, so the call returns at once and nothing waits for the children.
;Ends the definition.
:The last word calls the function once and starts the chain.

Every call creates two processes and returns, and each of those creates two more. After 20 generations that is more than a million processes, far above what most servers allow. Since each parent returns at once, there is no single process to kill that stops the rest. Once a limit is reached, fork() fails with EAGAIN, and bash prints fork: retry: Resource temporarily unavailable while it waits and retries. The name is irrelevant: bomb(){ bomb|bomb& };bomb is the same program.

Why you should not run it, not even once

  • On a server without per-user limits it uses up the process table and memory. sshd cannot fork for new logins, open shells cannot start commands, and recovery usually means a console or a hard reset, which interrupts databases and anything else in the middle of a write.
  • On a shared machine, or on a server that is not yours, it is a denial of service attack on everyone else using it.
  • It is short and looks harmless to anyone who cannot read shell syntax, which is the whole trick. Never paste a command you cannot read.
  • On a KVM VPS it runs into the limits of the guest's own kernel, not the host's, but your server stops responding just the same.

Check the limits your server has now

any Linux with systemd · read only
ulimit -u                               # max processes for your user (soft limit)
ulimit -Hu                              # hard limit
cat /proc/sys/kernel/pid_max            # highest PID number
cat /proc/sys/kernel/threads-max        # system-wide task ceiling, sized from RAM
systemctl show -p DefaultTasksMax       # default cap per service
systemctl show user-$(id -u).slice -p TasksMax   # cap for all your login sessions
example output · one Debian 12 server, systemd 252, run as root
160288
160288
4194304
320576
DefaultTasksMax=48086
TasksMax=105790

Your numbers will differ, because they scale with RAM. At boot the kernel sets the default process limit (ulimit -u) to half of threads-max: 160,288 here. systemd raises pid_max to 4,194,304 on 64-bit systems (since systemd 243), which leaves threads-max as the real ceiling. DefaultTasksMax is 15% of the smallest of pid_max, threads-max and the root cgroup's pids.max, so 15% of 320,576 = 48,086. All sessions of one user share one slice with TasksMax=33% from systemd's own drop-in, here 105,790.

These are ceilings for the whole machine, not protection: a user who may start 100,000 processes can still freeze a small server. To pick a real limit, count what a user runs today, threads included, and set the cap well above that: ps -L -U deploy --no-headers | wc -l.

Per-user limit: ulimit -u and nproc in limits.conf

bash · this shell and everything it starts
ulimit -Su 2000
/etc/security/limits.d/90-nproc.conf
# <domain>  <type>  <item>  <value>
*           soft    nproc   2048
*           hard    nproc   4096

pam_limits applies the file at login. On our Debian 12 server the PAM files for sshd, login, su, sudo and runuser load it, so the new limit counts from the next login. Three details decide whether it helps:

  • RLIMIT_NPROC counts processes, more precisely threads, of the user's real UID across the whole system, not per shell. A program with many threads uses up the same budget.
  • It is not enforced for root or for processes with CAP_SYS_ADMIN or CAP_SYS_RESOURCE, and a * line in limits.conf never applies to root. A fork bomb run as root ignores it.
  • systemd services are not PAM sessions and never read limits.conf; the same dead end is described for open files in the Too many open files guide. For services, use TasksMax=.

systemd: TasksMax for services and login sessions

TasksMax= drives the cgroup pids controller. It takes a number, a percentage of the system maximum, or infinity, and it counts every task in the unit whatever the user, root included. That makes it the better fence for services:

systemd · one service
sudo systemctl edit myapp.service
#   [Service]
#   TasksMax=512
systemctl status myapp.service | grep Tasks    # Tasks: 7 (limit: 512)
systemd · all login sessions, every user
sudo mkdir -p /etc/systemd/system/user-.slice.d
sudo tee /etc/systemd/system/user-.slice.d/50-tasksmax.conf > /dev/null <<'EOF'
[Slice]
TasksMax=4096
EOF
sudo systemctl daemon-reload
systemctl show user-1000.slice -p TasksMax

A drop-in in user-.slice.d applies to every user-UID.slice, because systemd also reads drop-ins for the name cut after each dash. If a user who is logged in still shows the old value, set it on the running slice: sudo systemctl set-property user-1000.slice TasksMax=4096. Memory and CPU caps for a unit work the same way, see limits in a systemd service.

Under the hood: cgroup v2 pids.max

cgroup v2 · read only
cat /sys/fs/cgroup/user.slice/user-1000.slice/pids.max      # the limit, or max
cat /sys/fs/cgroup/user.slice/user-1000.slice/pids.current  # tasks right now
cat /sys/fs/cgroup/user.slice/user-1000.slice/pids.events   # max N: times the limit was hit

TasksMax= is written to pids.max. The kernel documentation gives the fork bomb as its example of why this controller exists: it runs out of tasks before memory limits notice anything. Once a cgroup is full, fork() and clone() return EAGAIN, and pids.events counts how often the limit was hit, which tells you afterwards that it actually caught something. Containers use the same mechanism: docker run --pids-limit 256 IMAGE sets it for one container.

ProtectionCountsRoot limited?Set in
ulimit -u / nprocProcesses and threads of one real UID, system-wideNoShell, limits.conf via pam_limits, LimitNPROC= in a unit
TasksMax= (service)Every task in one serviceYessystemctl edit, unit file
TasksMax= (user-.slice)Every task in one user's sessionsYes, root has user-0.sliceDrop-in or set-property
pids.maxEvery task in a cgroupYesWritten by systemd, Docker, or by hand

If a fork bomb is already running

  1. 1A limit held: kill the user's processes all at once

    Log in as root or another admin; that session has its own cap. Then remove the whole set in one go rather than PID by PID:

    as root
    loginctl terminate-user USERNAME     # every process of that user's sessions
    id -u USERNAME                       # the UID, 1000 in the next line
    echo 1 > /sys/fs/cgroup/user.slice/user-1000.slice/cgroup.kill   # whole cgroup, SIGKILL
    pkill -STOP -u USERNAME; pkill -KILL -u USERNAME   # anything outside sessions
    pgrep -c -u USERNAME                                # 0 when it is over

    The kernel documents cgroup.kill as safe against concurrent forks; the file exists on Debian 12's 6.1 kernel. Stopping before killing works because a stopped process cannot fork; repeat the pair if pgrep still counts any. The options of pkill are covered in the kill command guide.

  2. 2Only a shell of the same user is left

    External commands need a new process, so only builtins work. kill -9 -1 is a builtin and sends SIGKILL to every process you are allowed to signal, except init and the shell itself. As a normal user that is exactly your own processes; never run it as root, where it takes sshd and everything else down with it.

  3. 3Nothing responds

    Use an out of band console if your provider offers one (VNC or a serial console on a VPS, the server's management console on a dedicated server). With the magic SysRq keys, Alt+SysRq+s, u, b syncs the disks, remounts them read-only and reboots, which is gentler than a reset. On our Debian 12 server kernel.sysrq is 438, which allows those three keys but not the kill keys e and i; root can always write to /proc/sysrq-trigger. Afterwards, check the journal and the OOM killer messages, then set the limits above so it cannot happen again.

Frequently asked

What does :(){ :|:& };: do?

It defines a bash function named : whose body calls itself twice through a pipe and puts that pipeline in the background, then calls it once. Every call starts two new processes and returns at once, so the number of processes doubles until a limit or the memory runs out and the system can no longer start programs.

Does ulimit -u protect against a fork bomb?

Partly. ulimit -u and the nproc item in limits.conf cap the processes and threads of one user, but they are not enforced for root and systemd services never read limits.conf. A TasksMax= limit on services and on user-.slice uses the cgroup pids controller and also covers root and every service.

Can a fork bomb damage my server?

It does not damage hardware or delete files by itself. It uses up process slots and memory until the processes are killed or the machine is rebooted. The real risk is a hard reset while a database or another program is writing, so set limits before it happens instead of relying on recovery.

What replaced UserTasksMax in logind.conf?

systemd 239 removed UserTasksMax= from logind.conf, and logind now ignores it with a notice. Put TasksMax= under [Slice] in a drop-in such as /etc/systemd/system/user-.slice.d/50-tasksmax.conf, run systemctl daemon-reload, and check the result with systemctl show user-1000.slice -p TasksMax.

Related articles

All guides