Here are simple, step-by-step instructions to create a 1 GB swap file at /swapfile1 on a typical Linux system (e.g., Ubuntu, Debian, or similar distributions). Run these commands as root or with sudo. Ensure you have at least 1 GB of free disk space on the root filesystem (check with df -h /).
- Create the 1 GB swap file
Use fallocate (fastest and preferred on modern filesystems like ext4) or dd as a fallback:If fallocate fails (e.g., "Operation not supported"), use:sudo fallocate -l 1G /swapfile1sudo dd if=/dev/zero of=/swapfile1 bs=1M count=1024 - Set secure permissions (critical for security - swap may contain sensitive data):
sudo chmod 600 /swapfile1 - Format the file as swap:
sudo mkswap /swapfile1 - Enable the swap file immediately:
Verify it with:
sudo swapon /swapfile1swapon --show - Make it persistent (enable on boot):
Edit /etc/fstab (backup first: sudo cp /etc/fstab /etc/fstab.bak):sudo echo "/swapfile1 none swap defaults 0 0" | sudo tee -a /etc/fstab