Linux swapon Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to manage swap space in Linux using the swapon command. Swap space is a reserved area on the hard disk that the operating system can use when the system runs out of RAM. You will start by understanding the concept of swap space, then check the current swap space usage and configuration, and finally create and enable a new swap file. The lab covers practical examples and provides step-by-step guidance to help you effectively manage swap space on your Linux system.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") linux/SystemInformationandMonitoringGroup -.-> linux/free("`Memory Reporting`") linux/SystemInformationandMonitoringGroup -.-> linux/df("`Disk Space Reporting`") subgraph Lab Skills linux/cat -.-> lab-422942{{"`Linux swapon Command with Practical Examples`"}} linux/chmod -.-> lab-422942{{"`Linux swapon Command with Practical Examples`"}} linux/free -.-> lab-422942{{"`Linux swapon Command with Practical Examples`"}} linux/df -.-> lab-422942{{"`Linux swapon Command with Practical Examples`"}} end

Understand the Concept of Swap Space in Linux

In this step, you will learn about the concept of swap space in Linux. Swap space is a reserved area on a hard disk that the operating system can use when the system runs out of RAM (Random Access Memory). When the system requires more memory than the available RAM, it can temporarily transfer inactive pages from RAM to the swap space, freeing up RAM for more active processes.

Swap space can be either a dedicated partition on the hard disk or a swap file. Swap partitions are typically created during the installation of the operating system, while swap files can be created and managed later on.

The use of swap space helps to prevent the system from running out of memory, which can lead to performance degradation or even system crashes. However, excessive use of swap space can also have a negative impact on system performance, as accessing data on the hard disk is much slower than accessing data in RAM.

To check the current swap space usage and configuration, you can use the free command:

$ free -h
              total        used        free      shared  buff/cache   available
Mem:           1.9Gi       144Mi       1.6Gi       2.7Mi       141Mi       1.6Gi
Swap:          2.0Gi          0B       2.0Gi

In the example output, the system has 1.9 GiB of RAM and 2.0 GiB of swap space configured.

Check Current Swap Space Usage and Configuration

In this step, you will learn how to check the current swap space usage and configuration on your Linux system.

First, you can use the free command to display the current memory usage, including the swap space:

$ free -h
              total        used        free      shared  buff/cache   available
Mem:           1.9Gi       144Mi       1.6Gi       2.7Mi       141Mi       1.6Gi
Swap:          2.0Gi          0B       2.0Gi

The output shows that the system has 1.9 GiB of RAM and 2.0 GiB of swap space, and currently, 0 bytes of swap space is being used.

You can also use the swapon command to get more detailed information about the swap space configuration:

$ sudo swapon --show
NAME      TYPE SIZE USED PRIO
/dev/sda2 partition 2.0G 0B -2

This command displays the swap space devices, their type, size, current usage, and priority. In this example, the swap space is configured as a partition with a size of 2.0 GB and a priority of -2.

To get even more detailed information, you can use the cat command to read the contents of the /proc/swaps file:

$ cat /proc/swaps
Filename				Type		Size	Used	Priority
/dev/sda2                               partition	2097148	0	-2

This file provides the same information as the swapon --show command, but in a different format.

Create and Enable a Swap File

In this step, you will learn how to create and enable a swap file on your Linux system.

First, let's create a swap file with a size of 1 GB:

$ sudo fallocate -l 1G /swapfile
$ sudo chmod 600 /swapfile

The fallocate command is used to create a file of the specified size (1 GB in this case), and the chmod command sets the appropriate permissions for the swap file.

Next, you need to format the swap file and enable it:

$ sudo mkswap /swapfile
$ sudo swapon /swapfile

The mkswap command initializes the swap file, and the swapon command enables the swap file.

To verify that the swap file is now being used, you can use the free command again:

$ free -h
              total        used        free      shared  buff/cache   available
Mem:           1.9Gi       144Mi       1.6Gi       2.7Mi       141Mi       1.6Gi
Swap:          3.0Gi          0B       3.0Gi

The output shows that the total swap space is now 3.0 GB, which includes the 1 GB swap file you just created.

To make the swap file persistent across system reboots, you need to add an entry for it in the /etc/fstab file:

$ echo '/swapfile swap swap defaults 0 0' | sudo tee -a /etc/fstab

This adds a line to the /etc/fstab file that will automatically enable the swap file when the system boots up.

Summary

In this lab, you learned about the concept of swap space in Linux, which is a reserved area on a hard disk that the operating system can use when the system runs out of RAM. You also learned how to check the current swap space usage and configuration using the free command. Finally, you learned how to create and enable a swap file, which can be useful if you need additional swap space beyond what is provided by a swap partition.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like