mkswap Command in Linux

Introduction

In this tutorial, we will delve into the Linux mkswap command, a vital tool for system administration. The mkswap command is used to designate a partition or file as swap space. Swap space acts as an extension of your system's RAM, providing crucial temporary storage when physical RAM is exhausted. This lab will guide you through creating a swap file, initializing it using mkswap, and then activating and verifying its operation. This tutorial covers the essential steps for efficient swap space management in a Linux environment, beneficial for any systemadmin.

This tutorial is structured into three primary sections: understanding the function of the mkswap command, creating a swap file with mkswap, and enabling and validating the swap file's usage. By the conclusion of this lab, you will possess a firm grasp of configuring and managing swap space within your Linux system.

Understand the Purpose of the mkswap Command

This section focuses on explaining the role of the mkswap command in Linux system administration. The mkswap command prepares a disk partition or file for use as Linux swap space. When the system's RAM is fully utilized, swap space is utilized by the operating system. The system transfers infrequently accessed data to swap, freeing up RAM for active processes, and preventing system crashes. Understanding swap is crucial for any systemadmin.

To illustrate the purpose of the mkswap command, let's start by checking the current swap configuration:

sudo swapon --show

Example output:

NAME      TYPE SIZE USED PRIO
/dev/sda5 partition 2G 0B -2

The output indicates that /dev/sda5 is currently set up as a swap partition, having a size of 2GB. The mkswap command is used to initialize this partition or a swap file, making it ready for the operating system to use as virtual memory.

The next section will demonstrate how to create a swap file using the mkswap command.

Create a Swap File Using the mkswap Command

In this section, we will walk through the process of creating a swap file using the mkswap command.

First, let's create a 2GB swap file in the ~/project directory:

sudo dd if=/dev/zero of=~/project/swapfile bs=1024k count=2048

This command generates a 2GB file named swapfile within the ~/project directory.

Now, we need to initialize the swap file with the mkswap command:

sudo mkswap ~/project/swapfile

Example output:

Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=a4a4d2d9-6f1f-4fca-9c2d-7f8d5c3d4c2a

The mkswap command configures the swapfile, assigns it a UUID. This UUID will then be used to reference this file.

With the swap file successfully created and initialized, we can proceed to enabling it.

Enable the Swap File and Verify Its Usage

In the concluding section, we will activate the swap file and confirm that the system is utilizing it.

First, we will enable the swap file using the swapon command:

sudo swapon ~/project/swapfile

Example output:

[sudo] password for labex:

If prompted, enter the sudo password to authorize the activation of the swap file.

Now, let's check that the swap file is active and available for system use:

sudo swapon --show

Example output:

NAME      TYPE SIZE USED PRIO
/dev/sda5 partition 2G 0B -2
/home/labex/project/swapfile file 2G 0B -3

This output confirms that /home/labex/project/swapfile is now active and can be used by the system.

To further verify the swap utilization, execute the free command:

free -h

Example output:

              total        used        free      shared  buff/cache   available
Mem:           1.9Gi       561Mi       977Mi        17Mi       375Mi       1.2Gi
Swap:          2.0Gi         0B        2.0Gi

This output validates the 2GB swap file is available. The output also shows that the swap is currently not in use (0B used).

Now the swap file is enabled and the system is ready to use it. This completes the tutorial.

Summary

This tutorial covered the mkswap command within Linux, which prepares a disk partition or file to be used for swap. You learned how to create a 2GB swap file using the dd and mkswap commands. You also enabled the file and verified that the system recognizes and can use it.

These steps provide a better understanding of swap management on Linux system. Effective swap management is useful for enhancing system performance when physical memory is limited. This is especially important for any systemadmin.

400+ Linux Commands