swapon Command in Linux

Introduction to Linux Swap Space Management

In this hands-on lab, delve into the world of swap space management in Linux utilizing the swapon command. Swap space acts as a crucial safety net, providing the operating system with extra memory when RAM resources are depleted. We'll begin by defining swap space, followed by an examination of your system's current swap utilization and setup. Lastly, you'll learn how to create and activate a new swap file. This tutorial provides practical, step-by-step instructions, empowering you to effectively manage swap space on your Linux system as a proficient systemadmin.

Understanding Swap Space Concepts in Linux

This section focuses on clarifying the role of swap space within a Linux environment. Simply put, swap space is a designated area on a hard drive used by the OS when RAM (Random Access Memory) is insufficient. When memory demands surpass available RAM, the system can temporarily relocate inactive memory pages from RAM to the swap space, thereby freeing up RAM for active processes.

Swap space can exist either as a dedicated partition on the hard disk or as a swap file. Swap partitions are generally established during the initial OS installation, while swap files can be created and configured at any time.

Utilizing swap space helps prevent scenarios where the system runs out of memory, which can cause performance bottlenecks or system crashes. However, over-reliance on swap space can negatively impact performance since hard disk access is significantly slower than RAM access.

To view the current swap space usage and configuration, execute 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

The output above shows that the system possesses 1.9 GiB of RAM and a swap space of 2.0 GiB.

Checking Your Current Linux Swap Space Usage and Configuration

This part will guide you on how to assess your Linux system's existing swap space utilization and its configuration details.

Start by using the free command to display overall memory usage, including the swap space status:

$ 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

This output indicates 1.9 GiB of RAM and 2.0 GiB of swap space are present, with 0 bytes of swap currently in use.

For a more detailed look at swap configuration, you can use the swapon command:

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

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

Further details can be gleaned by examining the contents of the /proc/swaps file using the cat command:

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

This file presents similar information as swapon --show, but in a different format.

Creating and Activating a Swap File on Linux

This section outlines the process of creating and enabling a swap file on your Linux system.

Let's begin by creating a 1 GB swap file:

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

The fallocate command creates a file of a specified size (1 GB here), and chmod sets the appropriate permissions for the swap file, enhancing system security.

Next, format and enable the swap file:

$ sudo mkswap /swapfile
$ sudo swapon /swapfile

The mkswap command initializes the swap file, and swapon activates it for system use.

Verify the swap file is being utilized with the free command:

$ 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 now shows a total swap space of 3.0 GB, reflecting the addition of the 1 GB swap file.

To ensure the swap file persists across reboots, add an entry to the /etc/fstab file, requiring root privileges:

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

This adds a line to /etc/fstab, automatically enabling the swap file during system startup. Remember to exercise caution when modifying system files.

Linux Swap Space Management: Summary

In this lab, we explored the concept of swap space in Linux, a designated area on the hard drive utilized by the operating system when RAM is insufficient. We also covered checking current swap space usage and configuration using the free command. Finally, you learned to create and enable a swap file, a valuable technique for adding extra swap space beyond that offered by a swap partition. This knowledge enhances your capabilities as a systemadmin working with Linux.

400+ Linux Commands