growpart expanded the root partition (/) to consume all space. Since filesystems cannot be shrunk, we will use a Hybrid Swap approach.
Verify your current disk layout and swap usage before starting.
df -h /swapon --showWe will use the dd utility to pre-allocate 4GB of space.
# Allocate the file (1024 * 4 = 4096)
sudo dd if=/dev/zero of=/swapfile bs=1M count=4096
# Restrict Permissions
sudo chmod 600 /swapfile
# Format as Swap
sudo mkswap /swapfile
# Enable the file
sudo swapon /swapfile
# Verify total swap (should show ~5GB total)
free -h
# Configure permanent mounting
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Adjust "swappiness" to ensure the kernel only uses swap when necessary.
# Check current value (Default 60)
cat /proc/sys/vm/swappiness
# Set to 10 and make permanent
sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
Can I delete it later?
Yes. Run sudo swapoff /swapfile, remove the line from /etc/fstab, and sudo rm /swapfile.
Is it slower than LVM?
No. On modern Linux kernels, there is no measurable performance difference between a swap partition and a swap file.