Add a swap file quickly and easily Print

  • linux, swap, swapfile, memory, disk, fallocate, dd, swapon, swapoff, fstab
  • 0

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 /).

  1. Create the 1 GB swap file
    Use fallocate (fastest and preferred on modern filesystems like ext4) or dd as a fallback:
    sudo fallocate -l 1G /swapfile1
    If fallocate fails (e.g., "Operation not supported"), use:
    sudo dd if=/dev/zero of=/swapfile1 bs=1M count=1024
  2. Set secure permissions (critical for security - swap may contain sensitive data):
    sudo chmod 600 /swapfile1
  3. Format the file as swap:
    sudo mkswap /swapfile1
  4. Enable the swap file immediately:
    sudo swapon /swapfile1
    Verify it with:
    swapon --show
  5. 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

Was this answer helpful?

« Back

Powered by WHMCompleteSolution