Creating a Swap Drive on Amazon Linux 2023
Creating a Swap Drive on Amazon Linux 2023
1. Introduction
Using a swap drive can help improve the performance of your Amazon Linux 2023 virtual machine (VM). This guide explains how to create and configure a 4GB swap drive.
2. Check Available Disks
Before making any changes, check which disks are available on your system:
1
lsblk
This command lists the disks and partitions. Identify the correct disk (e.g., /dev/sdc
).
3. Create a Partition on the Disk
If the disk does not have a partition, create one using fdisk
:
1
sudo fdisk /dev/sdc
Follow these steps in fdisk
:
- Type
n
to create a new partition. - Select
p
for a primary partition. - Choose a partition number (usually
1
). - Press
Enter
to accept default start and end sectors. - Type
t
and select82
(Linux Swap). - Type
w
to write changes and exit.
The new partition will be available as /dev/sdc1
.
4. Format the Swap Partition
Format the newly created partition as swap:
1
sudo mkswap /dev/sdc1
5. Enable Swap
Activate the swap with:
1
sudo swapon /dev/sdc1
Verify if swap is active:
1
sudo swapon --show
or
1
free -h
6. Make Swap Persistent
To ensure the swap remains active after a reboot, add it to /etc/fstab
:
1
sudo vi /etc/fstab
Append the following line at the end of the file:
1
/dev/sdc1 none swap sw 0 0
Save and exit.
7. Restart and Verify
Restart your system to apply the changes:
1
sudo reboot
After rebooting, verify the swap configuration:
1
2
sudo swapon --show
free -h
Conclusion
You have successfully created and configured a swap drive on Amazon Linux 2023. This additional swap space can improve system performance, especially when dealing with memory-intensive applications.