Here are the common Debian Swap recovery scenarios and steps:
1. Check Swap Status
Before starting recovery, verify the current Swap configuration using either:
sudo swapon --show (shows enabled Swap partitions/files and their sizes)cat /etc/fstab (displays system-configured Swap entries for auto-mount at boot)./dev/sda2) or a Swap file (e.g., /swapfile) and its current status.2. Recover a Swap Partition
If your Swap partition is corrupted, deleted, or not mounting automatically:
fdisk or gparted to create a new partition on the target disk (e.g., /dev/sda). Set the partition type to Linux Swap (ID 82)—this is critical for the system to recognize it as Swap space. Save changes and exit the tool.sudo mkswap /dev/sdXY (replace sdXY with your actual partition, e.g., /dev/sda2) to format the partition as Swap. This initializes the partition with the correct file system structure for Swap use.sudo swapon /dev/sdXY to activate the newly formatted partition. This makes the Swap space available immediately for system use./etc/fstab for Persistence:sudo nano /etc/fstab and add an entry to ensure the Swap partition mounts automatically at boot:/dev/sdXY none swap sw 0 0
Save changes (Ctrl+O → Enter → Ctrl+X in nano). The system will now enable the Swap partition on every restart.3. Recover a Swap File
If your Swap file (commonly /swapfile) is missing, deleted, or corrupted:
/path/to/backup/swapfile:sudo cp /path/to/backup/swapfile /swapfile
If the backup is compressed (e.g., swapfile.img.gz), decompress it first:sudo gunzip /path/to/backup/swapfile.img.gz
sudo cp /path/to/backup/swapfile.img /swapfile
dd to create a new Swap file of the desired size (e.g., 4GB). Replace /swapfile with your preferred path:sudo fallocate -l 4G /swapfile # Faster alternative to dd
sudo chmod 600 /swapfile # Set strict permissions (root-only access)
sudo mkswap /swapfile # Format the file as Swap
sudo swapon /swapfile. Verify it’s enabled using sudo swapon --show.4. Fix Existing but Non-Functional Swap Partitions
If the Swap partition exists but isn’t working (e.g., due to corruption), try repairing it:
sudo fsck -f /dev/sdXY (replace sdXY with your partition) to check and fix file system errors. The -f flag forces a check even if the partition appears clean.sudo swapon /dev/sdXY.5. Post-Recovery Verification
Regardless of the recovery method, always confirm the Swap is active:
sudo swapon --show to list all enabled Swap spaces. You should see your recovered partition or file listed with its size and usage.free -h to check total Swap space available. The output should reflect the restored Swap capacity.