Swap: a file, a partition, and how eagerly it is used
Domain 1 names swap management. This guide reads what a machine already has, adds a swap file by hand with the permission that matters, adds a second area with a priority so you can see which one wins, changes how eagerly the kernel pages, and removes everything - with the numbers that say whether swap is being used at all.
System Management Guide 18 of 28 Beginner
- OSUbuntu 26.04 LTS
- Kernel7.0.0-30-generic
- systemd259
- Mandatory access controlAppArmor on Ubuntu, SELinux enforcing on Alma
- TimeAbout 22 min
- Firewallufw / nftables 1.1.6 - firewalld / nftables 1.1.5
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| LPLUS-A01 | 192.168.0.73 | Ubuntu 26.04 LTS | Debian-family host - apt, ufw, netplan, AppArmor | 2 Core | 4 GB | 50 GB |
This guide includes
Use this when a machine needs more swap without a reboot, or when someone proposes changing swappiness to fix a performance problem. This matters because a swap file created the obvious way is world-readable until you fix it, and because swappiness is a weighting rather than a percentage.
- reading the existing swap areas three ways, and the swappiness that governs them
- creating a swap file with the right permission before it is activated
- adding a second area and setting the priority that decides which is used first
- changing swappiness in the running kernel and knowing where it persists
- reading whether the machine is actually paging, rather than assuming
Before you start
- lvm-from-physical-volume-to-a-filesystem-that-grows
-
What the machine has now
Three views of the same thing, and the number that governs it.
bash Example session free -h | head -3; echo "---"; swapon --show; echo "---"; cat /proc/swaps total used free shared buff/cache availableMem: 3.3Gi 548Mi 1.7Gi 7.9Mi 1.4Gi 2.8GiSwap: 3.8Gi 0B 3.8Gi---NAME TYPE SIZE USED PRIO/swap.img file 3.8G 0B -1---Filename Type Size Used Priority/swap.img file 4003836 0 -1cat /proc/sys/vm/swappiness; sysctl vm.swappiness; echo "--- the same number, two spellings"60vm.swappiness = 60--- the same number, two spellingsExpected result3.8Gi of swap, none used -
swapon --shownaming/swap.imgas type file with priority -1,/proc/swapssaying the same in blocks - and a swappiness of 60.Success conditionYou can describe a machine's swap configuration before changing it.
-
A swap file, made by hand
Four steps, and the one that must come before the others.
bash Example session sudo fallocate -l 256M /var/tmp/swapfile && sudo ls -lh /var/tmp/swapfile | awk '{print $1, $5, $9}'-rw-r--r-- 256M /var/tmp/swapfilesudo chmod 600 /var/tmp/swapfile && sudo mkswap /var/tmp/swapfile 2>&1 | tail -2; sudo ls -l /var/tmp/swapfile | awk '{print $1, $9}'Setting up swapspace version 1, size = 256 MiB (268431360 bytes)no label, UUID=366425a0-a049-4472-8e14-d3ca538144c5-rw------- /var/tmp/swapfilesudo swapon /var/tmp/swapfile && swapon --show; free -h | grep -i swapNAME TYPE SIZE USED PRIO/swap.img file 3.8G 0B -1/var/tmp/swapfile file 256M 0B -1Swap: 4.1Gi 0B 4.1GiExpected resultA 256M file created -rw-r--r--, then -rw------- after the chmod,
mkswapreportingswapspace version 1with a UUID - and swap totalling 4.1Gi once it is on.Success conditionYou can add swap to a running machine without a reboot.
-
A second area, and which one is used first
Priority, made visible.
bash Example session sudo truncate -s 128M /var/tmp/swap.img && D=$(sudo losetup -f --show /var/tmp/swap.img) && echo $D | sudo tee /tmp/swap.dev && sudo mkswap $D > /dev/null && sudo swapon -p 10 $D && swapon --show/dev/loop0NAME TYPE SIZE USED PRIO/swap.img file 3.8G 0B -1/var/tmp/swapfile file 256M 0B -1/dev/loop0 partition 128M 0B 10cat /proc/swaps; echo "--- higher priority is used first, and -1 means the kernel decides"Filename Type Size Used Priority/swap.img file 4003836 0 -1/var/tmp/swapfile file 262140 0 -1/dev/loop0 partition 131068 0 10--- higher priority is used first, and -1 means the kernel decidesExpected resultThree areas:
/swap.imgand/var/tmp/swapfileat priority -1, and/dev/loop0at 10 - and its type reported as partition.Success conditionYou can control which swap area the kernel reaches for first.
-
How eagerly the kernel swaps
One number, changed twice, and where it would persist.
bash Example session sudo sysctl -w vm.swappiness=10; cat /proc/sys/vm/swappiness; echo "--- changed in the running kernel only"vm.swappiness = 1010--- changed in the running kernel onlyls /etc/sysctl.d/ | head -4; echo "--- and a file here is what makes it survive a reboot"README.sysctl--- and a file here is what makes it survive a rebootsudo sysctl -w vm.swappiness=60 > /dev/null; sysctl vm.swappiness; echo "--- put back to the default"vm.swappiness = 60--- put back to the defaultExpected result
vm.swappiness = 10and the proc file agreeing -/etc/sysctl.d/holding onlyREADME.sysctl- then back to 60.Success conditionYou can change swappiness and say what would make it survive a reboot.
-
What swap is actually for
The numbers that say whether any of this matters.
bash Example session free -m | head -2; awk '/^(MemTotal|SwapTotal|SwapFree|Committed_AS|CommitLimit):/ {printf "%-14s %8.0f MB\n", $1, $2/1024}' /proc/meminfo total used free shared buff/cache availableMem: 3398 534 1715 7 1400 2864MemTotal: 3399 MBSwapTotal: 4294 MBSwapFree: 4294 MBCommitLimit: 5993 MBCommitted_AS: 361 MBvmstat 1 2 | tail -1 | awk '{print "si (swap in):", $7, " so (swap out):", $8}'; echo "--- zero on both means nothing is being paged right now"si (swap in): 0 so (swap out): 0--- zero on both means nothing is being paged right nowExpected result3,399 MB of memory against 4,294 MB of swap, a
CommitLimitof 5,993 MB with only 361 MB committed - and si 0, so 0 from vmstat.Success conditionYou can say whether a machine is paging before changing anything about swap.
-
Putting the machine back
Both areas removed, and the original left alone.
bash Example session sudo swapoff /var/tmp/swapfile && sudo swapoff $(cat /tmp/swap.dev) && swapon --show; echo "--- exit $? : no swap areas of ours left"NAME TYPE SIZE USED PRIO/swap.img file 3.8G 0B -1--- exit 0 : no swap areas of ours leftsudo losetup -d $(cat /tmp/swap.dev); sudo rm -f /var/tmp/swapfile /var/tmp/swap.img /tmp/swap.dev; cat /proc/swaps; losetup -a | wc -l; echo "loop devices left"; free -h | grep -i swapFilename Type Size Used Priority/swap.img file 4003836 0 -10loop devices leftSwap: 3.8Gi 0B 3.8GiExpected resultOnly
/swap.imgleft inswapon --showand/proc/swaps, 0 loop devices, and swap back to 3.8Gi.Success conditionThe machine has exactly the swap it started with.
Troubleshooting
swaponwarns that a swap file has insecure permissions.Why: The file was created 644. A swap file holds the contents of memory, so anyone able to read it can read whatever was paged out.
Fix:
chmod 600 <file>beforemkswapandswapon. If it is already active,swapoff, fix the mode, andswaponagain.Swap added by hand is gone after a reboot.
Why:
swaponis a runtime action. Nothing records it.Fix:Add a line to
/etc/fstab:/path/to/swapfile none swap sw 0 0, orUUID=<uuid> none swap sw,pri=10 0 0for a device. Test withswapoff -a && swapon -abefore trusting it to a reboot.Swap is in use and the machine is assumed to be short of memory.
Why: Allocated swap and active paging are different things. The kernel pages out long-idle memory as a matter of course.
Fix:
vmstat 1and readsiandso. Sustained non-zero values mean real paging; zeros with swapusedmean the kernel tidied up earlier and nothing is wrong.swapoffhangs or fails.Why: Everything paged out must come back into RAM, and there may not be room.
Fix:Free memory first, or swap off one area at a time rather than
swapoff -a. On a machine under pressure, plan the removal rather than issuing it interactively.