Sharing is caring

Turn off swap

How to turn off swap in Linux. Not every Linux server I administer needs to have a swap partition and to start swapping. For instance, the MySQL servers I maintain all have more than enough RAM on board to do their work. Yet, when a swap partition is enabled Linux starts swapping, which may degrade MySQL database performance…

An unwanted Linux swap partition can be the result of an automated and unattended Linux installation. Of course you can fiddle with the Linux kernel swapiness settings, located in /proc/sys/vm/swapiness and configurable in /etc/sysctl.conf, but one can turn off the swap completely too.

Here’s a tiny bash script to disable Linux’ swap – for which we use the swapoff command – and to comment out the swap partition in /etc/fstab

Bash script to turn off Linux swap #

# swapoff -a to disable swapping
swapoff -a
# sed to comment the swap partition in /etc/fstab
sed -i.bak -r 's/(.+ swap .+)/#\1/' /etc/fstab

Save this as and execute as root:

sh /root/

A backup of the /etc/fstab file is made just to be sure and to stay on the safe side.

Show Your Support

If you want to step in to help me cover the costs for running this website, that would be awesome. Just use this link to donate a cup of coffee ($5 USD for example). And please share the love and help others make use of this website. Thank you very much!

About the Author Jan Reilink

My name is Jan. I am not a hacker, coder, developer, programmer or guru. I am merely a system administrator, doing my daily thing at Vevida in the Netherlands. With over 15 years of experience, my specialties include Windows Server, IIS, Linux (CentOS, Debian), security, PHP, websites & optimization.

follow me on:

Leave a Reply

2 Comments on "Turn off swap"

Hi! Join the discussion, leave a reply!

newest oldest most voted
Notify of

Hi Jan,

Instead of using a complete partition as swap. You can also use a swapfile which is much more flexible. You can even use more than one swapfile on other devices. It makes your systems much more responsive. Without getting stuck on low free RAM.

# dd if=/dev/zero of=/swapfile bs=1M count=512
# chmod 600 /swapfile
# mkswap /swapfile
# swapon /swapfile

Edit the /etc/fstab to auto-enable the swapfile:
/swapfile none swap defaults 0

and for removing it:

# swapoff -a
# rm -f /swapfile
Jan Reilink

Hi Brian, thank you for your comment :)

Great point! However, in my opinion – and with current amounts of RAM available in systems – the need for swap often indicates a problem elsewhere. And on mission critical systems like our MySQL servers, you don’t want degraded performance because of swapping data in and out.

OTOH, if you have RAM to spare, here’s how to create a nifty RAM disk in Linux: :-)