How to Create a Swap File on Linux Using the dd Command
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/zeroinitializes the swap file with zeros.of=/swapfilesets the path for the swap file.bs=1024andcount=1048576create a 1GB file.mkswapformats the file for swap usage, andchmod 0600restricts access to root only.swaponenables 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.