Swapfile instead of a swap partition

First create the file to use for swap. I use /swapfile.

sudo dd if=/dev/zero of=/swapfile bs=1M count=2000

The above command would create a 2 GB file, feel free to adjust the size to fit your need.

Now set the correct permissions on the swapfile.

sudo chown root:root /swapfile
sudo chmod 600 /swapfile

Now setup that file to be used for swap.

sudo mkswap /swapfile

Enable the swapfile.

sudo swapon /swapfile

To have the swapfile automatically loaded on boot, add this to /etc/fstab.

/swapfile none swap sw 0 0

Now if you run free -m you can check that it lists the correct amount of swap.

Comments

Post comment