CertGrid CertGrid
Hands-on Lab·CompTIA Linux+

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

One host. Swap works identically on the RPM family; only the file that makes a setting persist differs, and the page says where it lives.
Server NameIP AddressOSRolesCPURAMHDD
LPLUS-A01192.168.0.73Ubuntu 26.04 LTSDebian-family host - apt, ufw, netplan, AppArmor2 Core4 GB50 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.

Before you start

  1. 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 spellings

    Expected result3.8Gi of swap, none used - swapon --show naming /swap.img as type file with priority -1, /proc/swaps saying the same in blocks - and a swappiness of 60.

    Success conditionYou can describe a machine's swap configuration before changing it.

  2. 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.1Gi

    Expected resultA 256M file created -rw-r--r--, then -rw------- after the chmod, mkswap reporting swapspace version 1 with a UUID - and swap totalling 4.1Gi once it is on.

    Success conditionYou can add swap to a running machine without a reboot.

  3. 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 decides

    Expected resultThree areas: /swap.img and /var/tmp/swapfile at priority -1, and /dev/loop0 at 10 - and its type reported as partition.

    Success conditionYou can control which swap area the kernel reaches for first.

  4. 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 default

    Expected resultvm.swappiness = 10 and the proc file agreeing - /etc/sysctl.d/ holding only README.sysctl - then back to 60.

    Success conditionYou can change swappiness and say what would make it survive a reboot.

  5. 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 now

    Expected result3,399 MB of memory against 4,294 MB of swap, a CommitLimit of 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.

  6. 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.8Gi

    Expected resultOnly /swap.img left in swapon --show and /proc/swaps, 0 loop devices, and swap back to 3.8Gi.

    Success conditionThe machine has exactly the swap it started with.

Troubleshooting

Official sources