| |

LFCS 90 🐧 Swap Space — Configuration

⏱️ Estimated read: 11 min | |

Configuring swap is the part of memory management that the administrator controls directly. The concepts from the previous chapter—inactive pages, the OOM killer, the three forms—become commands in this one. The sequence is consistent across all three forms: create the block device or file, format it with mkswap, protect it with permissions, activate it with swapon, and make it persistent in /etc/fstab. The differences are in the creation step and the size constraints.

This chapter covers the configuration workflow: creating a swap file, a swap partition, and a swap Logical Volume, then extending, reducing, and removing each. It assumes the LVM and filesystem concepts from the previous chapters. The focus is on the commands and the persistence that the LFCS exam tests, not the theory of paging.


Key point: mkswap writes the swap signature. swapon activates it. The fstab entry makes it survive a reboot. All three steps are required, and the order matters.


Why configuration needs a workflow

The signature requirement. The kernel does not treat a raw block device or a file as swap unless it has a swap signature. mkswap writes that signature, and swapon refuses to activate a device without it. The signature includes the swap area’s UUID, which is what the fstab entry uses for persistence .

The permission requirement for files. A swap file contains the contents of memory pages, which may include passwords, keys, and other sensitive data. The file must be readable only by root, or the kernel refuses to activate it. chmod 600 /swapfile is the required step .

The persistence requirement. swapon is temporary. It lasts until the next reboot. The /etc/fstab entry is what makes the swap active at boot. The format is /swapfile none swap defaults 0 0 for a file, or UUID=xxxx none swap defaults 0 0 for a partition or LV .

The deactivation requirement. A swap area cannot be resized or removed while it is active. swapoff deactivates it, and then lvextend, lvreduce, or lvremove can operate on the underlying device. The Red Hat documentation for extending swap on an LVM LV lists swapoff as the first step .


a. Creating a swap file

The swap file is the most flexible form because it does not require a partition table change. The Red Hat documentation for RHEL 10 shows the sequence: create the file with fallocate (preferred over dd on modern filesystems), set the permissions, format it with mkswap, add the fstab entry, reload systemd, and activate with swapon .

# Create a 4G file. fallocate is faster than dd on ext4 and XFS.
sudo fallocate -l 4G /swapfile

# The file must be root-only readable and writable.
sudo chmod 600 /swapfile

# Write the swap signature.
sudo mkswap /swapfile

# Add the fstab entry for persistence.
echo '/swapfile none swap defaults 0 0' | sudo tee -a /etc/fstab

# Reload systemd so it registers the new fstab entry.
sudo systemctl daemon-reload

# Activate the swap immediately.
sudo swapon /swapfile

# Verify.
swapon --show
free -h

The fallocate command is the modern approach. The older dd if=/dev/zero of=/swapfile bs=1024 count=N is still valid on filesystems that do not support fallocate, but it is slower and performs unnecessary disk I/O . The chmod 600 is not optional: the kernel checks the permissions and refuses to activate a world-readable swap file.


b. Creating a swap partition or Logical Volume

The partition and the LV are both raw block devices, so the sequence is simpler: mkswap, fstab entry, swapon. The difference is in how the device is created.

For a partition, the device is created with parted or fdisk, and the partition type is set to linux-swap. For an LV, the device is created with lvcreate.

# A swap partition. The partition must already exist.
sudo mkswap /dev/sdb2
sudo swapon /dev/sdb2

# A swap LV. The VG must have free extents.
sudo lvcreate -n lv_swap -L 4G vg_data
sudo mkswap /dev/vg_data/lv_swap
sudo swapon /dev/vg_data/lv_swap

The fstab entry for a partition or LV uses the UUID, not the device path. The UUID is obtained with blkid or lsblk -no UUID.

# Get the UUID of the swap device.
sudo blkid -s UUID -o value /dev/sdb2

# Add the fstab entry.
# UUID=xxxx none swap defaults 0 0

The none in the second field is deliberate: swap has no mount point. The swap in the third field tells mount and swapon that this is a swap area, not a filesystem.


c. Extending, reducing, and removing swap

A swap LV can be extended and reduced, but the swap must be deactivated first. The Red Hat documentation for extending swap on an LVM LV shows the sequence: swapoff, lvresize, mkswap, swapon .

# Extend a swap LV by 2G.
sudo swapoff -v /dev/vg_data/lv_swap
sudo lvresize -L +2G /dev/vg_data/lv_swap
sudo mkswap /dev/vg_data/lv_swap
sudo swapon -v /dev/vg_data/lv_swap

# Verify.
cat /proc/swaps
free -h

The mkswap after the resize is mandatory. The old swap signature does not cover the new extents, and swapon would fail or activate only the old size. The Red Hat documentation notes that the UUID may change after mkswap, and if the system uses the UUID for hibernation resume, the kernel parameter must be updated with grubby .

Reducing a swap LV follows the same pattern with lvreduce instead of lvresize, and the Red Hat documentation for RHEL 7 shows the lvreduce -L -512M form .

Removing a swap LV requires swapoff, lvremove, removal of the fstab entry, and (if the UUID was used for hibernation) removal of the kernel resume parameter .

# Remove a swap LV.
sudo swapoff -v /dev/vg_data/lv_swap
sudo lvremove /dev/vg_data/lv_swap

# Remove the fstab entry.
# Remove the resume=UUID=... from the kernel parameters if present.

For a swap file, removal is swapoff, remove the fstab entry, and rm /swapfile .


Complete Example Session

# ============================================
# PART 1: CHECK THE CURRENT STATE
# ============================================
# What swap is active now?

swapon --show
free -h
cat /proc/swaps

# ============================================
# PART 2: CREATE A SWAP FILE
# ============================================
# The most flexible form. No partition change.

sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile

# ============================================
# PART 3: PERSIST AND ACTIVATE
# ============================================
# fstab makes it survive a reboot.

echo '/swapfile none swap defaults 0 0' | sudo tee -a /etc/fstab
sudo systemctl daemon-reload
sudo swapon /swapfile

# ============================================
# PART 4: VERIFY
# ============================================

swapon --show
free -h

# ============================================
# PART 5: CREATE A SWAP LV
# ============================================
# The LV form combines flexibility with a raw device.

sudo lvcreate -n lv_swap -L 2G vg_data
sudo mkswap /dev/vg_data/lv_swap
sudo swapon /dev/vg_data/lv_swap

# ============================================
# PART 6: THE FSTAB ENTRY FOR AN LV
# ============================================
# Use the UUID, not the device path.

sudo blkid -s UUID -o value /dev/vg_data/lv_swap
# Add: UUID=xxxx none swap defaults 0 0

# ============================================
# PART 7: EXTEND THE SWAP LV
# ============================================
# Deactivate, resize, reformat, reactivate.

sudo swapoff -v /dev/vg_data/lv_swap
sudo lvresize -L +1G /dev/vg_data/lv_swap
sudo mkswap /dev/vg_data/lv_swap
sudo swapon -v /dev/vg_data/lv_swap

# ============================================
# PART 8: THE SWAPPINESS PARAMETER
# ============================================
# The dial between RAM and swap.

cat /proc/sys/vm/swappiness
# Set to 10 for a server.
sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

# ============================================
# PART 9: REMOVE THE SWAP FILE
# ============================================
# Disable, remove the fstab entry, delete the file.

sudo swapoff -v /swapfile
# Remove the /swapfile line from /etc/fstab.
sudo systemctl daemon-reload
sudo rm /swapfile

# ============================================
# PART 10: THE FULL PICTURE
# ============================================
# mkswap writes the signature.
# swapon activates.
# fstab persists.
# swapoff deactivates before resize or remove.

echo "Swap configuration complete."

The ten parts walk through the file and LV forms, the persistence, the extension, and the removal. The pattern is consistent: format, activate, persist, and deactivate before any change.


Quick Reference

Creation Commands

FormCreateFormatActivate
Filefallocate -l SIZE /swapfilemkswap /swapfileswapon /swapfile
Partitionparted / fdiskmkswap /dev/sdXswapon /dev/sdX
LVlvcreate -n lv -L SIZE vgmkswap /dev/vg/lvswapon /dev/vg/lv

Persistence

Formfstab Entry
File/swapfile none swap defaults 0 0
PartitionUUID=xxxx none swap defaults 0 0
LVUUID=xxxx none swap defaults 0 0

Extend/Reduce/Remove

OperationSequence
Extendswapoff → lvresize → mkswap → swapon
Reduceswapoff → lvreduce → mkswap → swapon
Removeswapoff → lvremove → remove fstab entry

Verification

CommandShows
swapon --showActive swap devices
cat /proc/swapsSwap details
free -hMemory and swap usage
cat /proc/sys/vm/swappinessCurrent swappiness

Best Practices

✅ Do This:

# Set permissions on a swap file before activating.
sudo chmod 600 /swapfile                                              // ✅
# Reload systemd after editing fstab.
sudo systemctl daemon-reload                                          // ✅
# Deactivate before resizing or removing.
sudo swapoff -v /dev/vg_data/lv_swap                                  // ✅
# Use the UUID in fstab for partitions and LVs.
UUID=xxxx none swap defaults 0 0                                     // ✅

❌ Don’t Do This:

# Don't skip mkswap after resizing.
sudo lvresize -L +2G /dev/vg/lv && sudo swapon /dev/vg/lv             // ❌
# Don't leave a swap file world-readable.
chmod 644 /swapfile                                                   // ❌
# Don't remove an LV while swap is active.
sudo lvremove /dev/vg_data/lv_swap                                    // ❌

Common Pitfalls

PitfallWhy It HappensFix
swapon failsNo swap signaturemkswap first
swapon refuses filePermissions too openchmod 600
Swap not active at bootNo fstab entryAdd the entry and reload systemd
Resize doesn’t take effectForgot mkswap after resizeReformat, then swapon
lvremove failsSwap still activeswapoff first

Real-World Examples

1. Create Swap File

sudo fallocate -l 4G /swapfile && sudo chmod 600 /swapfile && sudo mkswap /swapfile && sudo swapon /swapfile

2. Create Swap LV

sudo lvcreate -n lv_swap -L 4G vg_data && sudo mkswap /dev/vg_data/lv_swap && sudo swapon /dev/vg_data/lv_swap

3. Persist a Swap File

echo '/swapfile none swap defaults 0 0' | sudo tee -a /etc/fstab && sudo systemctl daemon-reload

4. Persist a Swap LV

# Get UUID: sudo blkid -s UUID -o value /dev/vg_data/lv_swap
# Add: UUID=xxxx none swap defaults 0 0

5. Extend a Swap LV

sudo swapoff -v /dev/vg_data/lv_swap && sudo lvresize -L +2G /dev/vg_data/lv_swap && sudo mkswap /dev/vg_data/lv_swap && sudo swapon -v /dev/vg_data/lv_swap

6. Reduce a Swap LV

sudo swapoff -v /dev/vg_data/lv_swap && sudo lvreduce -L -1G /dev/vg_data/lv_swap && sudo mkswap /dev/vg_data/lv_swap && sudo swapon -v /dev/vg_data/lv_swap

7. Remove a Swap LV

sudo swapoff -v /dev/vg_data/lv_swap && sudo lvremove /dev/vg_data/lv_swap

8. Remove a Swap File

sudo swapoff -v /swapfile && sudo rm /swapfile

9. Set Swappiness

sudo sysctl vm.swappiness=10

10. Verify Swap

swapon --show && free -h

Visual

The Configuration Sequence

┌─────────────────────────────────────────────────────────┐
│  CREATE (file, partition, or LV)                        │
│         │                                               │
│         ▼                                               │
│  mkswap → write swap signature                          │
│         │                                               │
│         ▼                                               │
│  chmod 600 (file only) → root-only access               │
│         │                                               │
│         ▼                                               │
│  fstab entry → persistence                              │
│         │                                               │
│         ▼                                               │
│  systemctl daemon-reload → systemd registers            │
│         │                                               │
│         ▼                                               │
│  swapon → activate                                      │
│         │                                               │
│         ▼                                               │
│  swapon --show + free -h → verify                       │
└─────────────────────────────────────────────────────────┘

The Extend Sequence

┌─────────────────────────────────────────────────────────┐
│  swapoff -v /dev/vg/lv                                  │
│         │  swap deactivated                             │
│         ▼                                               │
│  lvresize -L +2G /dev/vg/lv                             │
│         │  LV grown                                     │
│         ▼                                               │
│  mkswap /dev/vg/lv                                      │
│         │  new signature for new size                   │
│         ▼                                               │
│  swapon -v /dev/vg/lv                                   │
│         │  swap active at new size                      │
│         ▼                                               │
│  cat /proc/swaps → verify                               │
└─────────────────────────────────────────────────────────┘

The Three Forms

┌─────────────────────────────────────────────────────────┐
│  FILE                    PARTITION              LV      │
│  fallocate               parted/fdisk           lvcreate│
│  chmod 600               —                      —       │
│  mkswap                  mkswap                 mkswap  │
│  /swapfile in fstab      UUID= in fstab         UUID=   │
│                                                         │
│  Most flexible           Fastest                Flexible│
│  No partition change     Fixed size             Raw dev │
└─────────────────────────────────────────────────────────┘

Persistence

┌─────────────────────────────────────────────────────────┐
│  /etc/fstab                                             │
│                                                         │
│  /swapfile none swap defaults 0 0      ← file           │
│  UUID=xxxx none swap defaults 0 0      ← partition/LV   │
│                                                         │
│  Field 2 is `none`: swap has no mount point.            │
│  Field 3 is `swap`: tells swapon this is swap.          │
│                                                         │
│  After editing: systemctl daemon-reload                 │
└─────────────────────────────────────────────────────────┘

Summary

ItemValue
File creationfallocate -l SIZE /swapfile
File permissionschmod 600 /swapfile
Formatmkswap /dev/sdX or /swapfile
Activateswapon /dev/sdX or /swapfile
Persist/etc/fstab: UUID=... none swap defaults 0 0
Reload systemdsystemctl daemon-reload
Extend LVswapoff → lvresize → mkswap → swapon
Remove LVswapoff → lvremove
Swappinessvm.swappiness, default 60

Key takeaways:

  • The three steps are mandatory. mkswap writes the signature, swapon activates, and the fstab entry persists. Skipping any of them leaves the swap inactive or temporary .
  • The swap file needs chmod 600. The kernel refuses to activate a world-readable swap file because it contains memory contents .
  • The fstab entry uses none for the mount point and swap for the type. For a file, the path is the first field; for a partition or LV, the UUID is .
  • systemctl daemon-reload registers the new fstab entry. Without it, systemd does not know about the swap until the next boot .
  • Resizing requires swapoff, then mkswap. The old signature does not cover the new extents. The sequence is swapoff, lvresize or lvreduce, mkswap, swapon .
  • Removing an LV requires swapoff first. The lvremove command fails if the swap is still active .

Remember: Swap configuration is a sequence, not a single command. The block device or file must be formatted, activated, and persisted. The file form adds the permission step. The LV form adds the resize sequence. The fstab entry is what makes the swap survive a reboot, and the swapoff is what makes a resize possible. The commands are simple; the discipline is the order.


Was this helpful?


Stop using slow, ad-bloated tool sites! 🤮

🔎 Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)

⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free

🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!