10+ swapon command examples in Linux [Cheat Sheet]

Written by - Rohan Timalsina
Reviewed by - Deepak Prasad

Introduction to swapon and swapoff command

Linux uses swap space when the physical memory (RAM) reaches its maximum capacity. If the system requires more memory and the RAM is insufficient, inactive pages in memory are moved to the swap space.

Swap space is a part of virtual memory that stores inactive pages or processes. It can be a partition or file on a hard disk.

In this tutorial, we will discuss examples of using swapon and swapoff commands in Linux. The swapon command enables swapping and the swapoff command disables swapping on specified devices and files.


Syntax to use swapon and swapoff command

The syntax for using the swapon and swapoff command is as follows:

$ swapon <option>
$ swapoff <option>

You will require Sudo privileges to enable or disable swap devices and files.


How to create a swap file in Linux?

Before using the swapon and swapoff command, it would be a good idea to check whether the swap space is available in your system.

Run the following command to check for the swap space.

golinux@ubuntu-PC:~$ free -h
              total        used        free      shared  buff/cache   available
Mem:          1.9Gi       812Mi        84Mi        15Mi       1.1Gi       979Mi
Swap:         3.5Gi       1.0Mi       3.5Gi

If you do not find swap space, let's have a look at how you can create a new swap file.

The following command adds a new swapfile /testswap of 1GB in the system.

$ sudo fallocate -l 1G /testswap

Next, secure the swap file so only the root user can read and write the swap file. Set the 600 permission as shown below.

$ sudo chmod 600 /testswap

Then enable the swap area with the mkswap command on a file.

$ sudo mkswap /testswap

Lastly, you have to add the below entry in /etc/fstab file to make the changes permanent.

/testswap swap swap defaults 0 0


Different examples to use swapon command

1. Enable all swaps devices and files

The swapon command with -a or --all flag enables all swap devices and files listed in the /etc/fstab file.

$ sudo swapon -a


$ sudo swapon -all

Sample Output:

swapon command to enable all swap devices

The -v option is used to display verbose information.

If you want to enable a particular device, you can specify its name to the swapon command.

$ sudo swapon /testswap

The information about swap devices and files is stored in a /proc/swaps file.

display swap devices and files information


2. Display swap usage summary

The  -s or --summary option displays the swap usage summary of all enabled swap devices and files.

$ swapon -s


$ swapon --summary

Sample Output:

swapon command to display swap usage summary


3. Disable all swap devices and files

The swapoff command with -a or --all option disable swapping in all devices and files from /proc/swaps.

$ sudo swapoff -a


$ sudo swapoff --all

Sample Output:

swapoff command to disable all swap devices and files

Use the following syntax to disable a specific swap file or device.

$ sudo swapoff swap_name


4. Enable swap discards

The -d or --discard option enables the swap discards if the device supports the discard operation.

$ swapon -d


$ swapon --discard

Sample Output:

swapon command to enable swap discards


5. Skip devices that do not exist

You can silently skip devices that do not exist using the -e or --ifexists option.

$ sudo swapon -e


$ sudo swapon --ifexists

Sample Output:

golinux@ubuntu-PC:~$ sudo swapon -e
/swapfile file         2G 2.3M   -2
/testswap file      1024M   0B   -3
/dev/sdc1 partition  500M   0B   -4


6. Reinitialize the swap space

You can use swapon command with -f or --fixpgsz flag to reinitialize the swap space.

$ swapon -f


$ swapon --fixpgsz

Sample Output:

golinux@ubuntu-PC:~$ sudo swapon -f
/swapfile file         2G   0B   -2
/testswap file      1024M   0B   -3
/dev/sdc1 partition  500M   0B   -4


7. Display a summary table with selected columns

The --show option allows you to specify columns to display in the summary table. For instance, you can run the following command to display NAME and SIZE only.

$ swapon --show=NAME,SIZE

Sample Output:

display swapon summmary with specific columns


8. Don't print table heading

You can hide the table heading using the --noheadings option.

$ swapon --noheadings

Sample Output:

hide table headings in summary table


9. Use the raw output format

The --raw option uses the raw format to display the summary table.

$ swapon --raw

Sample Output:

display summary in raw format


10. Display swap size in bytes

With the --bytes option, you can view the swap sizes in bytes in the output.

$ swapon --bytes

Sample Output:

swapon command to display sizes in bytes


11. Display verbose output

You can print the verbose information using the -v or --verbose option.

$ swapon -v


$ swapon --verbose



Now you know how to enable and disable swap devices and files using the swapon and swapoff command in Linux. You have also learned to display the swap usage summary. We hope you found this article useful. If you have any questions, please let us know in the comment section below.


What's Next

[Solved] Found a swap file by the name .XXX.swp
10 different methods to check disk space in Linux


Further Reading

man page for swapon and swapoff command


Rohan Timalsina

He is proficient in a wide range of skills, including Page Builder Plugins such as Elementor, Beaver Builder, Visual Composer, and Divi Builder. His expertise extends to Front End Development with HTML5/CSS3, JavaScript, Bootstrap, and React.js. You can reach out to him on LinkedIn or check his projects on GitHub page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment