In this guide, we’ll go over the steps to create a swap file on Linux using the dd command. Once the swap file is set up, we’ll configure it in /etc/fstab so it activates automatically on boot.

Creating the Swap File

To begin, we’ll use the dd command to allocate 1GB for the swap file. Then, we’ll format the file, secure its permissions, and activate the swap space.

sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
sudo mkswap /swapfile && sudo chmod 0600 /swapfile
sudo swapon /swapfile

Explanation:

  • if=/dev/zero initializes the swap file with zeros.
  • of=/swapfile sets the path for the swap file.
  • bs=1024 and count=1048576 create a 1GB file.
  • mkswap formats the file for swap usage, and chmod 0600 restricts access to root only.
  • swapon enables the swap file immediately.

Adding the Swap File to fstab

To make sure the swap file is used after rebooting, add it to the /etc/fstab configuration file:

echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab

This will ensure that the system mounts the swap file on boot.

Wrapping Up

Forgetting how to do basic tasks like this can happen when you’re used to using automation tools like Ansible. However, it’s good to know how to set up a swap file manually in case automation isn’t available.