Resize swap in Ubuntu 18.04 and later (Linux)

PROBLEM: My swap is too small 🙁
SOLUTION: Disable, resize and enable swap again!

1. Disable swap

sudo swapoff -a


2. Resize swap

With goal of 2 GB (1 GB * 2)

sudo dd if=/dev/zero of=/swapfile bs=1G count=2

Where:
if – input file
of – output file
bs – block size
count – block multiplier

Note: If you get error:
dd: memory exhausted by input buffer of size 1073741824 bytes (1.0 GiB)
just try to change the bs to MB and increase the count size

sudo dd if=/dev/zero of=/swapfile bs=1M count=2000


3. Make the file swap again!

sudo mkswap /swapfile


4. Activate swap

sudo swapon /swapfile


(5.) Check the swap file size using top or grep

grep SwapTotal /proc/meminfo

Leave a Reply

Your email address will not be published. Required fields are marked *