Hands-on shell recipe to add swap space quickly and safely on a Linux host. 16.11.2025 | reading time: 2 min Need more virtual memory? This short guide shows how to create a swap file from the shell, enable it immediately and make the change persistent across reboots; follow exact commands and outputs to replicate the result on your machine. Practical Step-by-Step Run these commands as root or with sudo and watch the system gain virtual memory; copy-paste exactly: ```sudo swapoff -a sudo fallocate -l 1G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile free -h echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab``` Example outputs you will see: ```mkswap: Setting up swapspace version 1, size = 1 GiB (1073741824 bytes) mkswap: no label, UUID=11111111-2222-3333-4444-555555555555 free -h total used free shared buff/cache available Mem: 3.8G 1.2G 1.8G 123M 800M 2.3G Swap: 1.0G 0B 1.0G``` Tuning and Edge Cases Prefer `fallocate` for speed but fall back to `dd if=/dev/zero of=/swapfile bs=1M count=1024` if the filesystem does not support fallocate; always set permissions to 600 before `mkswap`; adjust kernel swapping behavior with `sysctl vm.swappiness=60` for testing and persist that change in `/etc/sysctl.conf`; note that swapfiles on some copy-on-write filesystems require special handling and that encrypted swap can be created with `cryptsetup` for confidentiality. Related Utilities The workflow centers on `mkswap`, `swapon` and `swapoff` but also touches `fallocate` and `dd` for file creation, `sysctl` for tuning and `cryptsetup` for encrypted swap; `free` and `swapon -s` are useful to verify the result and `tee` helps safely append the `/etc/fstab` entry. Final Step You added swap, verified it and made it persistent; now observe how your workloads behave and tune swappiness if needed; to deepen system skills and prepare for certs like CompTIA Linux+ or LPIC-1 consider intensive exam preparation at bitsandbytes.academy. Join Bits & Bytes Academy First class LINUX exam preparation. setup storage utilities filesystem processes troubleshooting