Skip to content

How to Disable Swap on Linux

DodaTech 2 min read

In this tutorial, you'll learn about How to Disable Swap on Linux. We cover key concepts, practical examples, and best practices.

The Problem

Your Linux system is using swap when you need it disabled for performance reasons (e.g., Kubernetes nodes, latency-sensitive applications) or because you want to rely entirely on physical RAM.

Quick Fix

Turn Off Swap Immediately

sudo swapoff -a
# (no output)
swapon --show
# (no output — no swap devices)

swapoff -a deactivates all swap devices and files immediately. Use swapon --show to verify no swap is active.

Verify Current Swap Usage

free -h
#               total        used        free      shared  buff/cache   available
# Mem:           15Gi        2.1Gi        8.9Gi        ...         ...       12Gi
# Swap:            0B          0B          0B
free | grep Swap | awk '{print $2}'
# 0

Check that the Swap row shows 0 in both total and used columns. If total is non-zero, swap is still active.

Remove Swap Entry from /etc/fstab (Permanent)

sudo sed -i '/swap/d' /etc/fstab
# (no output)
grep swap /etc/fstab
# (no output — swap entry removed)

Remove the line containing swap from /etc/fstab so swap does not re-enable on reboot. On some distributions, also check /etc/default/grub for resume= parameters.

Disable Swap on a Raspberry Pi or Swap-File System

sudo dphys-swapfile swapoff && sudo dphys-swapfile uninstall
# (no output)
sudo systemctl disable dphys-swapfile
# Removed /etc/systemd/system/multi-user.target.wants/dphys-swapfile

Systems like Raspberry Pi OS use dphys-swapfile to manage a swap file. Disable both the service and the swap file.

Monitor Memory After Disabling Swap

free -h
#               total        used        free      shared  buff/cache   available
# Mem:           15Gi        12Gi        1.5Gi        ...         ...        2.5Gi
vmstat 1 5
# procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
#  r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st

After disabling swap, monitor memory pressure with vmstat and free. If available memory drops consistently below 10%, consider adding more RAM or re-enabling swap with a lower vm.swappiness value.

Use Debug Mode for Risky Operations

# See every command before it executes
bash -x risky_script.sh
# + sudo sed -i 's/old/new/' /etc/fstab

Always use debug mode when running scripts that modify system configuration like fstab, SELinux settings, or firewall rules. This shows every command before it executes, catching typos early.

Additional Troubleshooting

# Check the error message and stack trace for more context
echo "Review the full error output to identify the root cause"

If the above steps do not resolve the issue, examine the complete error message and stack trace. Often the key detail is in the middle of the traceback rather than the final line. Search for the error message in the project documentation or issue tracker for additional solutions.

Prevention

  • Run free -h before and after disabling swap to confirm the change
  • For Kubernetes nodes, set failSwapOn: true in kubelet config to reject nodes with swap
  • Monitor memory pressure after disabling swap — applications that relied on swap may run out of memory
  • Consider increasing RAM or tuning vm.swappiness instead of disabling swap entirely

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro