swapoff Command in Linux

Introduction

This tutorial provides a comprehensive guide on utilizing the swapoff command within a Linux environment. You'll discover how to deactivate swap partitions or swap files, essential components for memory management. Furthermore, you'll gain the skills to pinpoint active swap partitions and files within your system and subsequently disable them using the swapoff command. This knowledge proves valuable in scenarios like resizing or removing a swap partition or optimizing memory allocation for other processes by temporarily halting swap usage.

Understand the Purpose of the swapoff Command

This section elucidates the role of the swapoff command in Linux system administration. The swapoff command serves to deactivate swap partitions or swap files. These files and partitions are crucial for extending available memory when physical RAM reaches its limit, a common strategy in Linux memory management.

Executing the swapoff command leads to an immediate cessation of swap space utilization. The memory formerly occupied by the swap becomes available for other processes. This action is particularly useful when you need to modify, resize, or eliminate a swap partition. Systemadmin may also use this command to free memory to give other applications more resources.

Begin by assessing your system's 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

The displayed output illustrates a system with an active 2 GB swap partition.

Now, proceed to disable the swap functionality using the swapoff command:

sudo swapoff -a

The -a flag instructs swapoff to disable every swap partition and file present on the system.

Post-execution of the swapoff command, verify the deactivation of swap functionality:

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

The output now confirms the disabling of swap space. The "Swap" line indicates 0 Bytes for both total and utilized space.

Identify Swap Partitions and Swap Files

This section guides you through the process of identifying swap partitions and swap files within your Linux system.

Initially, verify 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

As evident from the output, a 2 GB swap partition is currently active.

For more comprehensive details regarding swap partitions and files, utilize the swapon command alongside the -s flag:

sudo swapon -s

Example output:

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

The output reveals that the swap space is located on a partition at /dev/sda2 and possesses a capacity of 2 GB.

Alternatively, you can inspect the /proc/swaps file using the cat command to obtain similar information:

cat /proc/swaps

Example output:

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

If your system has swap files configured, they will also appear in the output of these commands. This is crucial for a systemadmin to keep in mind.

Disable Swap Using the swapoff Command

In this concluding section, you'll learn how to effectively disable a swap partition or swap file employing the swapoff command.

First, reconfirm the present 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

The output indicates a 2 GB swap partition in active use.

To disable the swap, execute the subsequent command:

sudo swapoff -a

The -a option instructs swapoff to deactivate all swap partitions and files within the system. This requires root privileges.

Following the execution of the swapoff command, validate that the swap is indeed inactive:

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

With the "Swap" line reflecting 0 Bytes for both total and used, it confirms that the swap has been successfully disabled. If after using the free command you still see swap usage, there could be a problem with the system configuration. Re-examine that the swap partition is the correct one for the system. Consider remaking the swap partition for the Linux system.

Summary

This tutorial covered the purpose and utilization of the swapoff command in Linux. As a systemadmin, you can manage memory allocations. The tutorial began by demonstrating how to check current swap usage using the free command, followed by deactivating the swap with the swapoff -a command. Post-deactivation, you verified the inactive state of the swap space. Furthermore, the tutorial detailed the process of identifying swap partitions and swap files on a Linux system using the free command. This information is helpful when using Linux.

400+ Linux Commands