Add swap file to a linux server (Debian or Ubuntu)

This article describes how you can add a swap file on Debian or Ubuntu. You can use this to add additional swap space or to add swap to a server without any swap space. (You can use this as temporary swap space, too)

1. Create the swap file:

Use „dd“ to create the swap file. The following command creates a 1024MB swap file (1024 * 1024MB = 1048576).

dd if=/dev/zero of=/swapfile bs=1024 count=1048576
root@server:~# dd if=/dev/zero of=/swapfile bs=1024 count=1048576
1048576+0 records in
1048576+0 records out
1073741824 bytes (1.1 GB) copied, 16.8866 s, 63.6 MB/s

2. Set up a Linux swap area:

mkswap /swapfile
root@server:~# mkswap /swapfile
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=9748279e-3efa-4e33-a305-3c14527cfb30

3. Activate swap space:

swapon /swapfile

4. Add swap file to fstab (optional)

To activate the swap file on system start, add entry the following line to „/etc/fstab„.

/swapfile swap swap defaults 0 0

5. Check swap

Execute „free -m“ or use „top“ to check the swap space of your system:

root@server:~# free -m
             total       used       free     shared    buffers     cached
Mem:          3529       3427        102          0         21       3198
-/+ buffers/cache:        208       3321
Swap:         1535          0       1535

Here you see the server with the 1024MB Swap file and a prior existing 512MB swap partition.

This Post Has 2 Comments

  1. Sehr schön gemacht 🙂 hilft mir immer wieder. Vielen dank! 😛

  2. Hi,

    statt „Swapdatei (1024 * 1024MB = 1048576).“

    muss es

    Swapdatei (1024 * 1024kb = 1048576 kb).

    heißen was dann 1024 MB oder 1 GB sind.

    Gruß

Schreibe einen Kommentar

eMail-Benachrichtigung bei weiteren Kommentaren.
Auch möglich: Abo ohne Kommentar.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.