Exercise: swap on Linux

  1. Step 1 — Phase 1 — Read the current swap state
  2. Step 2 — Phase 2 — Create and enable an extra swapfile, then clean up (172)
  3. Step 3 — Phase 3 — swappiness + paging monitoring (deciding when to use swap)
  4. Illustration 1 — Current swap state
  5. Illustration 2 — Creating and enabling an extra swapfile
  6. Illustration 3 — swappiness + paging monitoring

Estimated time: 35 minutes.

Learn about and configure swap on Linux: read the current swap state, create and enable an extra swapfile, tune vm.swappiness, monitor paging (vmstat), and explain when swap should and should not be used.

The IP addresses in this exercise are illustrative addresses from the range reserved for documentation (RFC 5737): dattqh-nat = 192.0.2.21, dattqh-client = 192.0.2.22; when you do the exercise, replace them with the real IPs of your machines.

Step 1 — Phase 1 — Read the current swap state

Before changing anything, you must be able to read the current state: swap devices, size, priority, swappiness, and the fstab entry (swapon(8), proc(5)).

# On dattqh-nat (192.0.2.21)
swapon --show
free -h
cat /proc/swaps
echo "swappiness=$(cat /proc/sys/vm/swappiness)"
grep -i swap /etc/fstab
grep -E 'MemTotal|MemAvailable|SwapTotal|SwapFree|SwapCached' /proc/meminfo

Note: Observation: swapon --show → /swap.img file 2G <used> -2. free -h → Swap 2.0Gi. swappiness=60. fstab: /swap.img none swap sw 0 0. meminfo: SwapTotal 2044924 kB.

Step 2 — Phase 2 — Create and enable an extra swapfile, then clean up (172)

The standard procedure for adding swap: fallocate → chmod 600 → mkswap → swapon.

# On dattqh-client (192.0.2.22)
sudo fallocate -l 512M /swapfile-demo
sudo chmod 600 /swapfile-demo
sudo mkswap /swapfile-demo
sudo swapon /swapfile-demo
swapon --show
free -h | grep -i swap
# --- CLEAN UP back to baseline ---
sudo swapoff /swapfile-demo
sudo rm -f /swapfile-demo
swapon --show

Note: Observation: After swapon: swapon --show lists 2 lines — /swap.img 2G -2 and /swapfile-demo 512M -3; free -h shows Swap total 2.5Gi.

After cleanup: only /swap.img 2G -2 remains (the baseline).

Step 3 — Phase 3 — swappiness + paging monitoring (deciding when to use swap)

The swappiness parameter tunes reclaim behavior; vmstat si/so shows real paging.

# On dattqh-client (192.0.2.22)
cat /proc/sys/vm/swappiness          # 60
sudo sysctl vm.swappiness=10         # temporary change
cat /proc/sys/vm/swappiness          # 10
sudo sysctl vm.swappiness=60         # revert
vmstat 1 3                           # si/so columns
# Persistent (for reference, not required on the lab VM):
#   echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf

Note: Observation: sysctl vm.swappiness=10 → vm.swappiness = 10, reading it back shows 10; reverted to 60. vmstat 1 3 shows si/so columns = 0 when the VM is idle (no thrashing).

Illustration 1 — Current swap state

swapon --show /swap.img 2G; free -h; swappiness 60

swapon --show: swap = the /swap.img file, 2G, priority -2; swappiness 60.

Illustration 2 — Creating and enabling an extra swapfile

fallocate+mkswap+swapon; swapon --show 2 swap; free 2.5Gi

The extra 512M swapfile gets priority -3; total swap goes from 2.0Gi to 2.5Gi.

Illustration 3 — swappiness + paging monitoring

sysctl vm.swappiness 10 then 60; vmstat si/so 0

sysctl changes swappiness at runtime; vmstat shows si/so=0 (no thrashing).

Exercise: swap on Linux