Linux Swap Management — Part 1

🖥️ Linux Swap Management — Part 1
📌 Fixed Partition & File-Based Swap in Debian/Linux

This guide covers:

✅ Creating swap using a disk partition
✅ Creating swap using a file
✅ Increasing swap size safely
✅ Verifying active swap usage

In the next part, I’ll cover swap management using LVM.

1️⃣ Check Existing Disk Layout

fdisk -l

Displays all available disks and existing partitions.

2️⃣ Open Disk for Partitioning

fdisk /dev/sda

Starts the interactive partition management tool for /dev/sda.

3️⃣ Create a New Partition

Command (m for help): n

Creates a new partition.

4️⃣ Select Partition Type

Command (m for help): p

Creates a primary partition.

Options:

  • p → Primary partition
  • e → Extended partition

5️⃣ Select Partition Number

Command (m for help): 1

Assigns partition number 1.

Available range:

  • 1–4 for primary partitions

6️⃣ Select Partition Size

First sector (2048 - 104857599, default 2048):

Last sector (default - 104857599): +5G

Allocates 5 GB for the new partition.

7️⃣ List Free Space Information

Command (m for help): F

Displays unpartitioned free space available on the disk.

8️⃣ Change Partition Type to Linux Swap

Command (m for help): t

Changes the partition type.

List supported partition types:

Hex code or alias (type L to list all): L

Linux swap type:

swap - 82

Assign swap type:

Hex code or alias (type L to list all): swap

 

9️⃣ Print Partition Table

Command (m for help): p

Displays the updated partition layout before saving.

🔟 Save Partition Changes

Command (m for help): w

Writes partition changes to disk and exits fdisk.

1️⃣1️⃣ Inform the Operating System About New Partition

partprobe /dev/sda

Reloads the partition table without rebooting.

Useful especially in:

  • RHEL 6
  • RHEL 7
  • Older Linux environments

1️⃣2️⃣ Verify Partition Creation

lsblk

Displays block devices and confirms the new partition exists.

1️⃣3️⃣ Create Swap Signature

mkswap /dev/sda1

Formats the partition as Linux swap space.

1️⃣4️⃣ Configure Persistent Swap

Edit /etc/fstab:

vim /etc/fstab

Add:

/dev/sda1 none swap defaults 0 0

To keep swap memory persistent even after reboot.

1️⃣5️⃣ Enable Swap

swapon /dev/sda1

Activates the swap partition immediately.

1️⃣6️⃣ Verify Swap Status

free -h

Displays RAM and swap usage.

swapon -s

Shows active swap devices/files.

📂 File-Based Swap

1️⃣ Create Swap File

fallocate -l 2G /opt/myswap.txt

Creates a 2 GB swap file instantly.

2️⃣ Secure File Permissions

chmod 0600 /opt/myswap.txt

Restricts access to root only.

3️⃣ Format File as Swap

mkswap /opt/myswap.txt

Initializes the file as swap space.

4️⃣ Configure Persistent Swap

Edit /etc/fstab:

vim /etc/fstab

Add:

/opt/myswap.txt none swap defaults 0 0

Ensures the swap file loads automatically during boot.

5️⃣ Enable Swap File

swapon /opt/myswap.txt

Activates the swap file.

6️⃣ Verify Swap Usage

free -h

Displays memory and swap usage.

swapon -s

Lists all active swap areas.

📈 Increase Existing Swap File Size

1️⃣ Disable Swap

swapoff /opt/myswap.txt

Temporarily disables the swap file before resizing.

2️⃣ Resize Swap File

fallocate -l 4G /opt/myswap.txt

Expands the swap file to 4 GB.

3️⃣ Recreate Swap Signature

mkswap /opt/myswap.txt

Reinitializes the resized file as swap.

4️⃣ Re-enable Swap

swapon /opt/myswap.txt

Activates the resized swap file.

5️⃣ Verify Updated Swap Size

free -mh

Confirms the updated swap capacity.

❌ Disable Swap

swapoff /opt/myswap.txt

Turns off the swap file manually.

⚠️ Production Tip

If swap is actively being used on a production server, avoid disabling swap during peak usage hours. Always verify sufficient free RAM before running swapoff.

🔜 Next Part: Managing Swap with LVM

 

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *