Adding a swap file to Ubuntu 16.x
Recently, I needed to add a swap to a Linux host that didn't have a spare partition, therefore, I was able to use this method to create a swap file, activate, and test it.
1. Create the temporary directory we will use for swap.
# mkdir -v /var/cache/swap
2. Create the swap file in the temporary directory (In the example below, we are creating a 500MB swap file, change to suit your needs)
# dd if=/dev/zero of=/var/cache/swap/swapfile bs=1M count=500
3. Change the mode of the file so only root can read/write to it
# chmod 600 /var/cache/swap/swapfile
4. Activate the swap file
# mkswap /var/cache/swap/swapfile
5. Verify the swapfile using 'free -m' or 'swapon -s'
# swapon -s
6. Add the swap partition to fstab to make the swap file presistant across reboots
# echo "/var/cache/swap/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab
7. To ensure you can reboot correctly, test the swap file by disabling it, then enabling it
# swapoff /var/cache/swap/swapfile
# swapon -va
8. Reboot
# reboot
1. Create the temporary directory we will use for swap.
# mkdir -v /var/cache/swap
2. Create the swap file in the temporary directory (In the example below, we are creating a 500MB swap file, change to suit your needs)
# dd if=/dev/zero of=/var/cache/swap/swapfile bs=1M count=500
3. Change the mode of the file so only root can read/write to it
# chmod 600 /var/cache/swap/swapfile
4. Activate the swap file
# mkswap /var/cache/swap/swapfile
5. Verify the swapfile using 'free -m' or 'swapon -s'
# swapon -s
6. Add the swap partition to fstab to make the swap file presistant across reboots
# echo "/var/cache/swap/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab
7. To ensure you can reboot correctly, test the swap file by disabling it, then enabling it
# swapoff /var/cache/swap/swapfile
# swapon -va
8. Reboot
# reboot
Comments