Create a swap file on Linux March 26, 2025
Antre du Tryphon

Create a swap file on Linux

March 26, 2025 · Admin Unix Ubuntu Linux Raspberry PI MAC OS X Raspbian

The simple following steps will create a swap file. Swap files are used when the operating system need more memory. It uses the swap space as memory. Its usage shall be limited, since a swap space is less efficient than memory. It will save you a memory upgrade if it is need seldomly.

  • swapon --show
    • Display the current status of the swap spaces
  • sudo dd if=/dev/zero of=/swapfile bs=1024 count=8388608
    • Create a swap file of 8 GB (8388608 / 1024 / 1024)
  • sudo mkswap /swapfile
    • Prepare the file to be used as a swap file
  • sudo chown root:root /swapfile
    • Set the owner of the swap file
  • sudo chmod 600 /swapfile
    • Set the permissions of the swap file
  • sudo swapon /swapfile
    • Enable the swap file. Reboot is not required.
  • swapon --show
    • Display the status of the new swap space

Copyright 2012–2026, Claude "Tryphon" Théroux