Note: /dev/sdb2 is used as an example. Replace it with your actual disk/partition identifier.
- Check existing swap space:
$ swapon --show
$ free -h
If no swap is active, proceed with creation.
- Create a swap partition:
Launch fdisk with administrative privileges:
$ sudo fdisk /dev/sdb
Within fdisk, execute these steps:
- Press n for new partition
- Select p for primary
- Accept default partition number or specify
- Set size (e.g. +2G for 2GB)
- Press t and choose type 82 (Linux swap)
- Press w to write changes
Tip: Reboot or run partprobe to reload partition table.
- Format partition as swap:
$ sudo mkswap /dev/sdb2
- Activate the swap space:
$ sudo swapon /dev/sdb2
- Enable permanent swap on boot:
Edit the fstab file:
$ sudo nano /etc/fstab
Add this entry at the end:
/dev/sdb2 none swap defaults 0 0Verify the entry is correct:
$ sudo mount -a
- Verify swap configuration:
$ swapon --show
$ free -h
- Optional: Tune swap usage (0-100):
Check current value:
$ cat /proc/sys/vm/swappiness
Set temporary new value (lower = less swap usage):
$ sudo sysctl vm.swappiness=10
Make permanent by adding to /etc/sysctl.conf:
$ echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf