RHCSA Practical Lab Series β Adding a Swap Partition
π 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:
- Press
n
β Create a new partition. - Press
p
β Make it a primary partition. - Press
3
β Assign partition number 3. - Press
Enter
twice β Accept the default start and end sectors. - Press
t
β Change partition type. - Enter
82
β Set the type to Linux swap. - 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! π