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.

Leave a comment

0 Comments.

Leave a Reply


[ Ctrl + Enter ]