Ubuntu Linux: Create swap file

Written by
Date: 2013-01-22 19:40:13 00:00


Memory in Linux is can be increased by create swap space, it can be a special partition, or a file which will work as a swap file. We will cover now, how to create a swap file.

Create a swap file

sudo dd if=/dev/zero of=/swapfile bs=1024 count=512k

Where:

  • bs=1024: Says that each block is made of 1024 bytes
  • count=512K: Says that there will be 512K blocks, so, 512 Megabytes of swapfile.

You can change count to 1024K if you want 1 Gigabyte of swap, more than that is not really recommended.

Format the file as swap

sudo mkswap /swapfile

Enable the swap

sudo swapon /swapfile

Make it permanent

To make the swap space available after every boot, edit the /etc/fstab file and add this line.

/swapfile       none    swap    sw      0       0 

Set the permissions to the swap file

sudo chown root:root /swapfile 
sudo chmod 0600 /swapfile