A swap partition is a useful tool for optimizing your Linux system. If you don't have one yet, this guide will take you through the steps to create and enable a swap partition with ease. Let's get started!
To begin, you'll need to create a partition for the swap if you don’t already have one. This can be done using the GParted live version on a USB drive or a live USB drive such as the Debian live USB. For this guide, we'll use Debian live, which you can download from the Debian website.
Steps to Create and Enable a Swap Partition
Boot with the live USB Once you have the live USB ready, boot from it. After booting, run the following commands:
sudo apt install gparted
This command installs GParted, a graphical partition editor.
Launch GParted
sudo gparted
This opens the GParted interface, where you can create and manage partitions.
Create the Swap Partition From GParted, create a new swap partition or use an available free partition.
List Your Block Devices
lsblk
This command lists all the block devices on your system, helping you identify the correct partition for swap.
Set Up the Swap Area
sudo mkswap /dev/sda3
Replace /dev/sda3 with the appropriate partition name. This initializes the specified partition as swap.
Enable Swap To activate the swap, run one of the following commands:
sudo swapon /dev/sda3
This enables swap for the specified partition. Alternatively:
sudo swapon -a
This enables all swap spaces, including both swap partitions and swap files.
Edit the fstab File
sudo nano /etc/fstab
Open the fstab file in the nano text editor. Add one of the following lines to ensure the swap partition is activated automatically at boot:
Using the partition name:
/dev/sda3 none swap sw 0 0
Using the UUID:
UUID=XXX-XXX-XXX none swap sw 0 0
Replace XXX-XXX-XXX with the actual UUID of your swap partition.
Save and Exit If you are using nano, press Ctrl + X, then Y, and then Enter to save and close the file.
And that’s it! You've successfully created and enabled a swap partition on your Linux system. This ensures improved system performance and a more seamless Linux experience.