Post

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:

  1. Type n to create a new partition.
  2. Select p for a primary partition.
  3. Choose a partition number (usually 1).
  4. Press Enter to accept default start and end sectors.
  5. Type t and select 82 (Linux Swap).
  6. 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.

This post is licensed under CC BY 4.0 by the author.