Create a Swap File on Linux with dd Command

We are going to use a dd command to create a swap file on a Linux system and then add it to fstab.

I’ve been using Ansible automation for so long that I forgot how to do trivial things by hand.

Create a Swap File with dd

Create a 1GB swap file, make the swap space, harden swap file permissions, and enable it.

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

Add a Swap File to Fstab

This ensures that the swap file is used upon system boot.

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

Leave a Reply

Your email address will not be published. Required fields are marked *