How to Disable Swap in Linux
In scenarios like changing the swap size, you should disable the swap first. This quick tutorial shows the steps for disabling swap in Linux.
β Abhishek Prakash
You may encounter a scenario where you need to disable the swap in Linux. Suppose you want to change the swap size. You should disable it before making any changes.
If for some reasons, you decided to disable swap in Linux, this tutorial shows you the steps to do it.
There are two kinds of swap space in Linux:
- swap partition (a dedicated partition)
- swap file (a file under root acting as swap)
A system can have either of the swap partition or swap file or both. You can check active swap using command:
swapon --show
However, more reliable information can be found in /etc/fstab file that shows all the swap on your system, active or inactive.
Now that you know how to locate your swap file or partition, letβs see how to disable it.
Disable swap in Linux
If you want to disable a specific swap file or partition, provide its path like this:
sudo swapoff /dev/nvme0n1p2
An easier way is to disable all the active swap on your system like this:
sudo swapoff -a
If you check for active swap again using swapon βshow command, you wonβt see any swap anymore.
Re-enable swap in Linux
If you want to reactivate a disabled swap space, you can use the swapon command like this:
sudo swapon /dev/nvme0n1p2
Or you can enable all the available swap space using this command:
sudo swapon -a
Deleting swap space
If you want to delete the swap partition or file, youβll have to first disable it. You just saw the steps for disabling it.
Now, if you have a swap file, deleting it is the same as removing any file in Linux.
sudo rm path_to_swap_file
If you have a swap partition, you can delete the partition using fdisk command.
The important part is that you should also remove the entry for the deleted swap file or partition from the /etc/fstab file.
I hope you found this quick little tutorial helpful in handling swap space to your Linux system. On a related topic, you may read about clearing the swap on your Linux system.
Creator of Linux Handbook and It's FOSS. An ardent Linux user & open source promoter. Huge fan of classic detective mysteries from Agatha Christie and Sherlock Holmes to Columbo & Ellery Queen.