Add Swap on a VPS (Chosen Size, Persisted in fstab)
This tutorial shows how to create and persist a swap file on a Linux VPS with xyz.sh/swap, and how to remove it safely.
$ curl -fsSL xyz.sh/swap | bash
Details · xyz.sh/swap · Last updated:
Prerequisites
Root is required. The default is a 2G file at /swapfile. The script refuses if fewer than about 512M would remain free after creation.
Steps
Default 2G: curl -fsSL xyz.sh/swap | bash
Custom size: curl -fsSL xyz.sh/swap | bash -s -- 4G or --size 1G --path /swapfile
The script uses fallocate (falling back to dd), sets mode 600, runs mkswap and swapon, and appends an /etc/fstab entry if one is not already present. An existing file at the same path is not replaced without confirmation.
How to verify
Run free -h or swapon --show; the new swap file should be listed.
How to remove
Run curl -fsSL xyz.sh/swap | bash -s -- --remove (add --path if you used a custom file). It swapoffs, removes the matching /etc/fstab entry, and deletes the file.