LFCS 90 🐧 Swap Space — Configuration
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
| Form | Create | Format | Activate |
|---|---|---|---|
| File | fallocate -l SIZE /swapfile | mkswap /swapfile | swapon /swapfile |
| Partition | parted / fdisk | mkswap /dev/sdX | swapon /dev/sdX |
| LV | lvcreate -n lv -L SIZE vg | mkswap /dev/vg/lv | swapon /dev/vg/lv |
Persistence
| Form | fstab Entry |
|---|---|
| File | /swapfile none swap defaults 0 0 |
| Partition | UUID=xxxx none swap defaults 0 0 |
| LV | UUID=xxxx none swap defaults 0 0 |
Extend/Reduce/Remove
| Operation | Sequence |
|---|---|
| Extend | swapoff → lvresize → mkswap → swapon |
| Reduce | swapoff → lvreduce → mkswap → swapon |
| Remove | swapoff → lvremove → remove fstab entry |
Verification
| Command | Shows |
|---|---|
swapon --show | Active swap devices |
cat /proc/swaps | Swap details |
free -h | Memory and swap usage |
cat /proc/sys/vm/swappiness | Current 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
| Pitfall | Why It Happens | Fix |
|---|---|---|
swapon fails | No swap signature | mkswap first |
swapon refuses file | Permissions too open | chmod 600 |
| Swap not active at boot | No fstab entry | Add the entry and reload systemd |
| Resize doesn’t take effect | Forgot mkswap after resize | Reformat, then swapon |
lvremove fails | Swap still active | swapoff 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
| Item | Value |
|---|---|
| File creation | fallocate -l SIZE /swapfile |
| File permissions | chmod 600 /swapfile |
| Format | mkswap /dev/sdX or /swapfile |
| Activate | swapon /dev/sdX or /swapfile |
| Persist | /etc/fstab: UUID=... none swap defaults 0 0 |
| Reload systemd | systemctl daemon-reload |
| Extend LV | swapoff → lvresize → mkswap → swapon |
| Remove LV | swapoff → lvremove |
| Swappiness | vm.swappiness, default 60 |
Key takeaways:
- The three steps are mandatory.
mkswapwrites the signature,swaponactivates, and thefstabentry 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
fstabentry usesnonefor the mount point andswapfor the type. For a file, the path is the first field; for a partition or LV, the UUID is . systemctl daemon-reloadregisters the new fstab entry. Without it, systemd does not know about the swap until the next boot .- Resizing requires
swapoff, thenmkswap. The old signature does not cover the new extents. The sequence isswapoff,lvresizeorlvreduce,mkswap,swapon. - Removing an LV requires
swapofffirst. Thelvremovecommand 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.
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!