How to check if swap space is enabled in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to check if swap space is enabled on your Linux system. You will explore three different methods to verify the swap configuration: using the swapon --show command to display active swap devices, inspecting the /proc/swaps file for detailed information, and examining the /etc/fstab file to understand how swap is configured to be enabled at boot time. By completing these steps, you will gain a comprehensive understanding of how to determine the swap status and configuration on your Linux system.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/SystemInformationandMonitoringGroup(["System Information and Monitoring"]) linux/BasicSystemCommandsGroup -.-> linux/help("Command Assistance") linux/BasicFileOperationsGroup -.-> linux/cat("File Concatenating") linux/SystemInformationandMonitoringGroup -.-> linux/ps("Process Displaying") linux/SystemInformationandMonitoringGroup -.-> linux/service("Service Managing") subgraph Lab Skills linux/help -.-> lab-558805{{"How to check if swap space is enabled in Linux"}} linux/cat -.-> lab-558805{{"How to check if swap space is enabled in Linux"}} linux/ps -.-> lab-558805{{"How to check if swap space is enabled in Linux"}} linux/service -.-> lab-558805{{"How to check if swap space is enabled in Linux"}} end

Check swap status with swapon --show

In this step, you will learn how to check the current swap status on your Linux system using the swapon --show command.

Swap space is a portion of a hard drive that is used when the amount of physical RAM is full. When the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. This allows the system to free up RAM for other processes.

The swapon command is used to specify devices on which paging and swapping are to be done. The --show option displays a summary of swap usage.

Open your terminal if it's not already open. You can do this by clicking the Xfce Terminal icon on the left side of your desktop.

Now, type the following command and press Enter:

swapon --show

This command will display information about the active swap devices on your system. The output might look something like this:

NAME      TYPE      SIZE USED PRIO
/swapfile file      512M   0B   -2

Let's break down the output:

  • NAME: The name of the swap device or file. In this example, it's /swapfile.
  • TYPE: The type of swap device. It can be a partition or a file. Here, it's a file.
  • SIZE: The total size of the swap space. In this case, it's 512 Megabytes (512M).
  • USED: The amount of swap space currently being used. Here, it's 0 Bytes (0B), meaning no swap is currently in use.
  • PRIO: The priority of the swap space. Higher priority swap is used first.

If the command doesn't show any output, it means there is no swap space currently active on your system.

Understanding your swap configuration is important for monitoring system performance and ensuring your system has enough memory resources.

Click Continue to proceed to the next step.

Verify swap in /proc/swaps

In this step, you will learn another way to verify the active swap space on your system by inspecting the /proc/swaps file.

The /proc filesystem is a virtual filesystem in Linux that provides information about processes and other system information. It doesn't contain real files on disk but rather provides a view into the kernel's data structures.

The /proc/swaps file specifically contains information about the swap areas currently in use by the system.

To view the contents of this file, you can use the cat command. cat is a command-line utility that reads file contents and prints them to the standard output.

Open your terminal if it's not already open.

Type the following command and press Enter:

cat /proc/swaps

The output of this command should be similar to the output of swapon --show from the previous step. It will list the active swap devices or files and their details.

You should see something like this:

Filename				Type		Size	Used	Priority
/swapfile                               file		524284	0	-2

Let's look at the columns:

  • Filename: The path to the swap file or device.
  • Type: The type of swap (file or partition).
  • Size: The total size of the swap space in kilobytes.
  • Used: The amount of swap space currently used in kilobytes.
  • Priority: The priority of the swap space.

Comparing the output of swapon --show and cat /proc/swaps confirms the active swap configuration on your system. Both commands provide similar information but in slightly different formats.

Using cat /proc/swaps is a quick way to check swap status, especially if you are scripting or need to parse the output easily.

Click Continue to move on to the next step.

Inspect swap config in /etc/fstab

In this step, you will learn how to inspect the /etc/fstab file to understand how swap space is configured to be automatically activated during system startup.

The /etc/fstab file (file system table) is a configuration file that contains information about all the disk partitions and network shares that the system can mount. It tells the system which filesystems to mount, where to mount them, and with what options, including swap space.

When the system boots up, it reads /etc/fstab to determine which filesystems and swap areas should be made available.

To view the contents of /etc/fstab, we will again use the cat command.

Open your terminal if it's not already open.

Type the following command and press Enter:

cat /etc/fstab

The output will show lines describing different filesystems and potentially a line for swap. Look for a line that mentions swap in the filesystem type column.

The output might look something like this:

## /etc/fstab: static file system information.
#
## Use 'blkid' to print the universally unique identifier for a
## device; this may be used with UUID= as a more robust way to name devices
## that works even if disks are added or removed. See fstab(5).
#
## <file system> <mount point>   <type>  <options>       <dump>  <pass>
/dev/sda1       /               ext4    errors=remount-ro 0       1
/swapfile       none            swap    sw              0       0

In this example output, the line /swapfile none swap sw 0 0 is the entry for the swap file.

Let's break down the columns for the swap entry:

  • /swapfile: The file or device path for the swap space.
  • none: The mount point. For swap, this is typically none.
  • swap: The filesystem type. This indicates it's a swap area.
  • sw: Mount options. sw is a common option for swap, meaning it should be swapped.
  • 0: Dump option. This is usually 0 for swap.
  • 0: Pass option. This is usually 0 for swap, meaning it's not checked during boot.

This entry in /etc/fstab ensures that the /swapfile is automatically activated as swap space every time the system starts.

By checking /etc/fstab, you can see how your swap is configured for persistence across reboots.

Click Continue to complete this lab.

Summary

In this lab, you learned how to check if swap space is enabled in Linux using two methods. First, you used the swapon --show command to display a summary of active swap devices, including their name, type, size, used space, and priority. This command provides a quick overview of the current swap configuration.

Secondly, you learned how to verify active swap space by inspecting the /proc/swaps file. This virtual file provides similar information to swapon --show and is another way to confirm which swap devices are currently in use by the system. Understanding these methods is crucial for monitoring system memory usage and performance.