Linux swapoff Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the swapoff command in Linux to disable swap partitions or swap files. You will also learn how to identify the swap partitions and swap files on your system, and then disable them using the swapoff command. This can be useful in various scenarios, such as when you want to resize or remove a swap partition, or when you want to free up memory for other processes.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/SystemInformationandMonitoringGroup -.-> linux/free("`Memory Reporting`") linux/SystemInformationandMonitoringGroup -.-> linux/df("`Disk Space Reporting`") subgraph Lab Skills linux/cat -.-> lab-422941{{"`Linux swapoff Command with Practical Examples`"}} linux/sudo -.-> lab-422941{{"`Linux swapoff Command with Practical Examples`"}} linux/free -.-> lab-422941{{"`Linux swapoff Command with Practical Examples`"}} linux/df -.-> lab-422941{{"`Linux swapoff Command with Practical Examples`"}} end

Understand the Purpose of the swapoff Command

In this step, you will learn about the purpose of the swapoff command in Linux. The swapoff command is used to disable swap partitions or swap files, which are used by the operating system to provide additional memory when the physical RAM is exhausted.

When you run the swapoff command, it immediately stops the use of the specified swap space, freeing up the memory that was being used by the swap. This can be useful in various scenarios, such as when you want to resize or remove a swap partition, or when you want to free up memory for other processes.

Let's start by checking the current swap usage on your system:

free -h

Example output:

              total        used        free      shared  buff/cache   available
Mem:           1.9Gi       528Mi       1.1Gi       4.0Mi       298Mi       1.2Gi
Swap:          2.0Gi       0Bi         2.0Gi

In this example, we can see that there is a 2 GB swap partition currently in use.

Now, let's disable the swap using the swapoff command:

sudo swapoff -a

The -a option tells swapoff to disable all swap partitions and files on the system.

After running the swapoff command, let's verify that the swap is no longer in use:

free -h

Example output:

              total        used        free      shared  buff/cache   available
Mem:           1.9Gi       528Mi       1.1Gi       4.0Mi       298Mi       1.2Gi
Swap:          0Bi         0Bi         0Bi

As you can see, the swap space is now disabled and the "Swap" line shows 0 Bytes for both total and used.

Identify Swap Partitions and Swap Files

In this step, you will learn how to identify the swap partitions and swap files on your Linux system.

First, let's check the current swap usage using the free command:

free -h

Example output:

              total        used        free      shared  buff/cache   available
Mem:           1.9Gi       528Mi       1.1Gi       4.0Mi       298Mi       1.2Gi
Swap:          2.0Gi       0Bi         2.0Gi

From the output, we can see that there is a 2 GB swap partition currently in use.

To get more detailed information about the swap partitions and files, you can use the swapon command with the -s option:

sudo swapon -s

Example output:

Filename                                Type            Size    Used    Priority
/dev/sda2                                partition       2097148 0       -2

This output shows that the swap space is a partition located at /dev/sda2 with a size of 2 GB.

You can also use the cat command to view the contents of the /proc/swaps file, which provides similar information:

cat /proc/swaps

Example output:

Filename                                Type            Size    Used    Priority
/dev/sda2                                partition       2097148 0       -2

If you have any swap files configured on your system, they would also be listed in the output of these commands.

Disable Swap Using the swapoff Command

In this final step, you will learn how to disable the swap partition or swap file using the swapoff command.

First, let's confirm the current swap usage:

free -h

Example output:

              total        used        free      shared  buff/cache   available
Mem:           1.9Gi       528Mi       1.1Gi       4.0Mi       298Mi       1.2Gi
Swap:          2.0Gi       0Bi         2.0Gi

As you can see, there is a 2 GB swap partition currently in use.

To disable the swap, run the following command:

sudo swapoff -a

The -a option tells swapoff to disable all swap partitions and files on the system.

After running the swapoff command, let's verify that the swap is no longer in use:

free -h

Example output:

              total        used        free      shared  buff/cache   available
Mem:           1.9Gi       528Mi       1.1Gi       4.0Mi       298Mi       1.2Gi
Swap:          0Bi         0Bi         0Bi

Now, the "Swap" line shows 0 Bytes for both total and used, indicating that the swap is disabled.

Summary

In this lab, you learned about the purpose of the swapoff command in Linux, which is used to disable swap partitions or swap files. You started by checking the current swap usage on your system using the free command, and then proceeded to disable the swap using the swapoff -a command. After disabling the swap, you verified that the swap space was no longer in use. Additionally, you learned how to identify the swap partitions and swap files on your Linux system using the free command.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like