Skip to content

How to create a swap file in linux using dd

  • by

Swap file creation
As root use dd to create a swap file the size of your choosing (M = Megabytes, G = Gigabytes). For example, creating a 512 MB swap file:

# dd if=/dev/zero of=/swapfile bs=1M count=512

Set the right permissions

# chmod 600 /swapfile

After creating the correctly sized file, format it to swap:

# mkswap /swapfile

Activate the swap file:

# swapon /swapfile

Edit fstab to add an entry for the swap file:

# vim /etc/fstab

/swapfile swap swap defaults 0 0

Remove swap file

To remove a swap file, the current swap file must be turned off.

As root:

# swapoff -a

Remove swap file:

# rm -f /swapfile

Finally remove the relevant entry from /etc/fstab.