Linux Swap File

Linux Swap File

For some unexplainable reason it annoyed me that most Linux distros use a swap partition instead of a swap file. Depending on your setup a swap file will be just as fast and will even allow you to suspend to disk. Here is how you do it.

Enter the following lines at the Terminal:

sudo dd if=/dev/zero of=/swap bs=1G count=X
sudo chown root:root /swap
sudo chmod 600 /swap
sudo mkswap /swap
sudo swapon /swap

Replace “X” in the first line with how many gigabytes you want your swap file to be (~1.5 times your amount of RAM is a good rough guess). Now add the line “/swap none swap sw 0 0” to your /etc/fstab file.

If you already have a swap partition you can safely remove the corresponding line from /etc/fstab, sudo swapoff partition, and then remove the swap partition completely with a tool like GParted.

If you get a error from dd indicating you do not have enough RAM you can use sudo dd if=/dev/zero of=/swap bs=1M count=X instead.

Leave a Reply

Your email address will not be published. Required fields are marked *