How to Disable Swap on Ubuntu Linux 20.04
(Updated )
Sometimes you need to disable Swap on a Linux server. For example, Kubernetes worker nodes need to have Swap turned off in order to increase the performance and stability. This tutorial will show you how to disable Swap on a Ubuntu Linux server.
Check if Swap is enabled
First check if swap is enabled:
sudo swapon --show
If swap is enabled you should see the path to the swap file and its size.
ubuntu@kube03:~$ sudo swapon --show
NAME TYPE SIZE USED PRIO
/swap.img file 1.9G 4.5M -2
You can also check by running the free
command:
free -h
The Swap line should show the total size and how much is used.
ubuntu@kube03:~$ free -h
total used free shared buff/cache available
Mem: 952Mi 211Mi 124Mi 1.0Mi 617Mi 582Mi
Swap: 1.9Gi 4.0Mi 1.9Gi
ubuntu@kube03:~$
Disable Swap
Run the following command to disable Swap:
sudo swapoff -a
Now remove the Swap file:
sudo rm /swap.img
The next thing we need to do is modify the fstab
file so that the Swap file is not re-created after a system reboot.
Remove following line from /etc/fstab
/swap.img none swap sw 0 0
Check Swap is disabled
Run the following command to check Swap is disabled.
sudo swapon --show
There should be no output if it’s disabled.
Conclusion
This tutorial showed you how to disable Swap, remove the old Swap file and make sure Swap is disabled after your system restarts.