Linux mkswap Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux mkswap command, which is used to set up a swap area on a disk partition or in a file. Swap space is an essential component of the operating system, providing temporary storage when physical RAM memory is full. In this lab, we will learn how to create a swap file, initialize it using the mkswap command, and then enable and verify its usage. This lab covers the key steps to effectively manage swap space in a Linux system.

The lab consists of three main steps: understanding the purpose of the mkswap command, creating a swap file using the mkswap command, and enabling the swap file and verifying its usage. By the end of this lab, you will have a solid understanding of how to set up and manage swap space in your Linux environment.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/SystemInformationandMonitoringGroup -.-> linux/free("`Memory Reporting`") linux/SystemInformationandMonitoringGroup -.-> linux/dd("`File Converting/Copying`") subgraph Lab Skills linux/sudo -.-> lab-422813{{"`Linux mkswap Command with Practical Examples`"}} linux/free -.-> lab-422813{{"`Linux mkswap Command with Practical Examples`"}} linux/dd -.-> lab-422813{{"`Linux mkswap Command with Practical Examples`"}} end

Understand the Purpose of the mkswap Command

In this step, we will explore the purpose of the mkswap command in Linux. The mkswap command is used to set up a Linux swap area on a disk partition or in a file. Swap space is used by the operating system when the amount of physical RAM memory is full. When the system runs out of RAM, it can use the swap space on the disk to temporarily hold less-used data, freeing up RAM for more active processes.

To understand the purpose of the mkswap command, let's first check the current swap status on our system:

sudo swapon --show

Example output:

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

As you can see, there is currently a swap partition /dev/sda5 configured on the system with a size of 2GB. The mkswap command is used to initialize this swap partition or a swap file, so that the operating system can start using it.

In the next step, we will learn how to create a swap file using the mkswap command.

Create a Swap File Using the mkswap Command

In this step, we will create 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 will create a 2GB file named swapfile in the ~/project directory.

Next, we need to initialize the swap file using 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 sets up the swap file and assigns a UUID to it. This UUID will be used to reference the swap file later.

Now that the swap file is created and initialized, we can proceed to the next step to enable it.

Enable the Swap File and Verify Its Usage

In this final step, we will enable the swap file we created in the previous step and verify its usage.

First, let's enable the swap file using the swapon command:

sudo swapon ~/project/swapfile

Example output:

[sudo] password for labex:

You may be prompted for the sudo password, enter the password (if required) to enable the swap file.

Now, let's verify that the swap file is enabled and being used by the system:

sudo swapon --show

Example output:

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

The output shows that the swap file /home/labex/project/swapfile is now enabled and available for use by the system.

To further verify the swap usage, we can use 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

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

Now that the swap file is enabled and verified, you have successfully completed this lab.

Summary

In this lab, we first explored the purpose of the mkswap command in Linux, which is used to set up a swap area on a disk partition or in a file. We then learned how to create a 2GB swap file using the dd and mkswap commands, and finally, we enabled the swap file and verified its usage.

After completing these steps, we now have a better understanding of how to manage swap space on a Linux system, which can be useful for improving system performance when physical memory is limited.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like