Skip to content
TrySitePrice

Ubuntu Linux Create and Add Swap File Tutorial

Best/Tutorial

Here's how you can create and add a swap file on Ubuntu Linux:

  1. Determine the size of the swap file: The recommended size for a swap file is equal to twice the size of your RAM. You can use the following command to determine the size of your RAM:

free -m

  1. Create a swap file: Use the dd command to create a new file with the desired size. Replace with the size of the swap file you want to create, in MB.

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

  1. Set the correct permissions for the swap file:

sudo chmod 600 /swapfile

  1. Mark the file as a swap file:

sudo mkswap /swapfile

  1. Enable the swap file:

sudo swapon /swapfile

  1. Make the swap file permanent: Add the following line to the /etc/fstab file to make the swap file permanent across reboots:

/swapfile none swap defaults 0 0

After completing these steps, your swap file should be up and running, and you should be able to see the new swap space when you run the free command.