Ubuntu Linux Create and Add Swap File 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 [swap_file_size] with the size of the swap file you want to create, in MB.
sudo dd if=/dev/zero of=/swapfile bs=1024 count=[swap_file_size]
  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.

Leave a Comment