Resolving Linux swap partition not mounting automatically on system reboot

Root Cause Diagnosis

Swap is activated at boot by systemd from /etc/fstab entries (via systemd-fstab-generator, which creates dev-disk-by\x2duuid-<UUID>.swap units) or from native .swap units. If the swap area is absent from /etc/fstab, the fstab entry references a stale UUID or device name (/dev/sdXN names change between boots), or the swap signature is missing, the unit never starts and swapon is never called. Manual swapon works until the next reboot because it modifies only the running kernel state, not persistent configuration.

Quick Fix

Find the swap partition’s UUID, add it to /etc/fstab, and activate it:

lsblk -f
sudo blkid | grep -i swap
echo 'UUID=<your-swap-uuid> none swap sw 0 0' | sudo tee -a /etc/fstab
sudo systemctl daemon-reload
sudo swapon -a
swapon --show

Replace <your-swap-uuid> with the value from blkid. If blkid shows no TYPE="swap", the partition has no swap signature; run sudo mkswap /dev/sdXN first (destroys any data on it) and use the new UUID.

Step-by-Step Resolution

1. Check current swap state and boot logs

swapon --show
free -h
systemctl list-units --type=swap --all
journalctl -b -u '*.swap' --no-pager
journalctl -b | grep -iE "swap|fstab"

No output from swapon --show confirms swap is inactive. Failed or missing .swap units in the journal identify whether the entry is absent or broken.

2. Identify the swap partition and its UUID

lsblk -o NAME,SIZE,TYPE,FSTYPE,UUID,PARTUUID,MOUNTPOINTS
sudo fdisk -l | grep -i swap

A valid swap partition shows FSTYPE = swap. Partition type GUID on GPT disks is 0657FD6D-A4AB-43C4-84E5-0933C84B4F4F; on MBR it is type 82. The type flag is informational, and the signature is what matters.

3. Create the swap signature if it is missing

Only when FSTYPE is empty and you have confirmed the partition holds no data:

sudo mkswap /dev/sdXN

Note the UUID= printed in the output. To set a label as an alternative identifier:

sudo mkswap -L swap0 /dev/sdXN

4. Add or correct the entry in /etc/fstab

Edit /etc/fstab and keep exactly one line per swap area:

UUID=3f7c1a2e-8b4d-4e6a-9c21-5d0b7a1e9f44  none  swap  sw  0  0

Use UUID= or PARTUUID= instead of /dev/sdXN. Device names are assigned in probe order and change with added disks, NVMe/SATA enumeration order, or USB devices. Fields must be separated by whitespace, and none is the mount point for swap.

See also  How to remove stubborn immutable Linux file when rm -f fails with Operation not permitted

For a swap file:

/swapfile  none  swap  sw  0  0

5. Validate before rebooting

sudo systemctl daemon-reload
sudo findmnt --verify --verbose
sudo swapoff -a
sudo swapon -a
swapon --show

findmnt --verify reports syntax errors and unresolvable UUIDs. swapon -a activates everything in fstab with the sw option. Errors here are the same errors that occur at boot.

6. Optionally set priority and boot behavior

UUID=3f7c1a2e-8b4d-4e6a-9c21-5d0b7a1e9f44  none  swap  sw,pri=10,nofail  0  0

nofail lets boot continue if the device is absent, and pri= orders multiple swap areas (higher is used first; equal priority values stripe across areas).

7. Use a native systemd unit as an alternative

Create /etc/systemd/system/dev-disk-by\x2duuid-3f7c1a2e\x2d8b4d\x2d4e6a\x2d9c21\x2d5d0b7a1e9f44.swap. Generate the escaped name with:

systemd-escape -p --suffix=swap /dev/disk/by-uuid/3f7c1a2e-8b4d-4e6a-9c21-5d0b7a1e9f44
[Unit]
Description=Swap partition

[Swap]
What=/dev/disk/by-uuid/3f7c1a2e-8b4d-4e6a-9c21-5d0b7a1e9f44

[Install]
WantedBy=swap.target
sudo systemctl daemon-reload
sudo systemctl enable --now dev-disk-by\\x2duuid-3f7c1a2e\\x2d8b4d\\x2d4e6a\\x2d9c21\\x2d5d0b7a1e9f44.swap

8. Reboot and verify

sudo reboot
# after boot
swapon --show
systemctl status '*.swap'

Why This Fails for Some Users

  • Reinstalled or cloned system with old UUID: Reformatting swap with mkswap generates a new UUID. The old UUID in /etc/fstab no longer resolves, and systemd waits for a device that never appears (up to 90 s by default). Update fstab with the new UUID, or pass -U <old-uuid> to mkswap to reuse it.
  • Debian/Ubuntu initramfs resume config: The file /etc/initramfs-tools/conf.d/resume contains RESUME=UUID=.... If it points at a stale swap UUID, boot delays by several seconds and hibernate breaks. Fix and rebuild:sudo update-initramfs -u -k all On Fedora, RHEL, and Arch, check the resume= kernel parameter in /etc/default/grub and regenerate the bootloader config (grub2-mkconfig -o /boot/grub2/grub.cfg or update-grub).
  • Swap on LVM: Use the logical volume path (/dev/mapper/vg0-swap) or its UUID. The volume group must be active early; a broken lvm2 initramfs hook or a missing rd.lvm.lv= parameter (Fedora/RHEL) leaves the LV inactive at swap activation.
  • Encrypted swap (crypttab): Random-key swap requires a /etc/crypttab entry and an fstab entry that points at the mapper device:cryptswap /dev/disk/by-partuuid/<partuuid> /dev/urandom swap,cipher=aes-xts-plain64,size=512 /dev/mapper/cryptswap none swap sw 0 0 Use PARTUUID or a stable by-id path in crypttab, since a random-key swap has no persistent filesystem UUID (mkswap runs on every boot and generates a new one).
  • Swap file on Btrfs: Btrfs swap files must be created with copy-on-write disabled and no compression. Use btrfs filesystem mkswapfile --size 4g /swapfile (btrfs-progs 6.1+) or chattr +C on an empty file before allocating. Swap files also cannot span multiple devices or sit on a snapshotted subvolume.
  • Swap file with holes or wrong permissions: Files created with fallocate fail on some filesystems (XFS on older kernels, F2FS) and produce swapon: skipping - it appears to have holes. Create with dd if=/dev/zero of=/swapfile bs=1M count=4096, then chmod 600 /swapfile and mkswap /swapfile. Permissions looser than 0600 produce a warning; and on newer util-linux, the swap file must be owned by root.
  • Filesystem mounted late: If the swap file lives on a separate mount (/data/swapfile), systemd starts the swap unit only after that mount is ready. A mount failure or noauto on the parent silently prevents swap activation. Add x-systemd.requires-mounts-for=/data if ordering is wrong.
  • Cloud images and containers: Many cloud images ship without swap and manage it through cloud-init. The mounts: or swap: module in /etc/cloud/cloud.cfg can overwrite /etc/fstab on each boot. Also, cloud-init and Azure/WALinuxAgent (ResourceDisk.EnableSwap=y in /etc/waagent.conf) may recreate swap on the ephemeral disk, which is wiped on redeploy.
  • Swap as ZRAM: zram-generator (Fedora, Arch) or zram-config (Ubuntu) provides in-RAM swap and may replace or coexist with disk swap. Check zramctl and /etc/systemd/zram-generator.conf. Priority conflicts cause the disk swap to be unused even when active.
  • swapoff on shutdown and nofail hidden failures: With nofail, activation errors do not surface in boot output. Check journalctl -b -u '*.swap' explicitly.
  • SELinux/AppArmor: Not a normal cause, but a mislabeled swap file (swapfile_t missing on RHEL-family) may generate AVC denials. Check ausearch -m avc -ts boot and restore with restorecon -v /swapfile.
  • Kernel or partition table changes: Resizing the partition or changing the GPT table after mkswap invalidates the swap header size. swapon fails with read swap header failed. Re-run mkswap on the resized partition.
See also  Resolving Linux Error: "No Space Left on Device" When Disk Space Shows 50% Free (Inodes Full)

Leave a Comment