RHCSA Practical Lab Series – Adding a Swap Partition

Expertise in Cloud, Networking & DevOps
Photo by Adi Goldstein / Unsplash

πŸ” Lab 19: Configuring a Swap Partition in RHEL 9

πŸ“Œ Objective

In this lab, you will:

βœ” Create a new 512 MiB swap partition
βœ” Format it as swap space
βœ” Configure the system to automount swap at boot


πŸ“Œ Step 1: Identify Available Disks and Partitions

πŸ”Ή List the current disk layout:

[root@node2 ~]# lsblk

πŸ“Œ Expected Output (Example):

NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda    252:0    0   20G  0 disk
β”œβ”€vda1 252:1    0    1G  0 part /boot
└─vda2 252:2    0   19G  0 part /
vdb    252:16   0    5G  0 disk

βœ… Ensure vdb is available before proceeding.


πŸ“Œ Step 2: Create a Swap Partition

πŸ”Ή Use fdisk to create a new partition:

[root@node2 ~]# fdisk /dev/vdb

πŸ“Œ Inside the fdisk prompt, perform the following:

  1. Press n β†’ Create a new partition.
  2. Press p β†’ Make it a primary partition.
  3. Press 3 β†’ Assign partition number 3.
  4. Press Enter twice β†’ Accept the default start and end sectors.
  5. Press t β†’ Change partition type.
  6. Enter 82 β†’ Set the type to Linux swap.
  7. Press w β†’ Write changes and exit.

βœ… Re-run lsblk to confirm the partition exists as /dev/vdb3.


πŸ“Œ Step 3: Format the Partition as Swap

πŸ”Ή Run the mkswap command to format the partition:

[root@node2 ~]# mkswap /dev/vdb3

βœ… Expected Output:

Setting up swapspace version 1, size = 512 MiB

πŸ”Ή Enable the swap partition immediately:

[root@node2 ~]# swapon /dev/vdb3

βœ… Verify if swap is active:

[root@node2 ~]# free -h

πŸ“Œ Expected Output:

              total        used        free
Mem:          3.8G        1.2G        2.6G
Swap:         512M          0B        512M

βœ… If Swap shows 512M, the swap is successfully added.


πŸ“Œ Step 4: Persist Swap Across Reboots

πŸ”Ή Add the swap entry to /etc/fstab:

[root@node2 ~]# vim /etc/fstab

πŸ”Ή Append this line at the end:

/dev/vdb3 swap swap defaults 0 0

βœ… Save and exit (ESC + :wq).

πŸ”Ή Reload the fstab file and verify:

[root@node2 ~]# swapon -a
[root@node2 ~]# swapon --show

πŸ“Œ Expected Output:

NAME       TYPE  SIZE USED PRIO
/dev/vdb3  swap 512M   0B   -2

βœ… If /dev/vdb3 appears, the swap is correctly configured.


πŸ“Œ Step 5: Verify After Reboot (Optional)

πŸ”Ή Reboot the system:

[root@node2 ~]# reboot

πŸ”Ή After reboot, confirm swap is active:

[root@node2 ~]# free -h

βœ… If Swap still shows 512M, everything is correctly set!


πŸ“Œ Troubleshooting Tips

πŸ”Ή If the swap partition does not mount after reboot, check /etc/fstab syntax:

[root@node2 ~]# cat /etc/fstab

πŸ”Ή If mkswap fails, ensure the partition type is 82 (Linux swap):

[root@node2 ~]# fdisk -l /dev/vdb

πŸ”Ή To disable swap temporarily:

[root@node2 ~]# swapoff -a

πŸ”Ή To remove swap permanently, delete the line from /etc/fstab and turn swap off.


βœ… Final Summary

βœ” Created a 512 MiB swap partition on /dev/vdb3
βœ” Formatted it using mkswap
βœ” Enabled swap immediately using swapon
βœ” Persisted swap configuration in /etc/fstab
βœ” Verified swap functionality after a system reboot


πŸ“Œ Next Lab: Configuring Logical Volumes in RHEL 9

πŸ“© Subscribe for more RHCSA exam labs and hands-on tutorials! πŸš€


Read more