Create a Linux Swap File

Swap is a space on a disk that is used when the amount of physical RAM is full. When a Linux system runs out of RAM, inactive pages are moved from the RAM to the swap space.
Swap space can take the form of either a dedicated swap partition or a swap file. In most cases, when running Linux on a virtual machine, a swap partition is not present, so the only option is to create a swap file.
Swap helps when you run out of RAM, but it is not a replacement for memory. Heavy swapping usually means your system is under memory pressure and will slow down.
How to add a swap file
Follow these steps to add 1GB of swap to your server. If you want to add 2GB instead of 1GB, replace 1G with 2G. On small systems, 1-2GB is often enough; if you use hibernation, swap should be at least the size of RAM.
Create a file that will be used for swap:
Terminalsudo fallocate -l 1G /swapfileIf
fallocateis not installed or if you get an error message sayingfallocate failed: Operation not supported, you can use the following command to create the swap file:Terminalsudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576To confirm the file size, run:
Terminalls -lh /swapfileOnly the root user should be able to write and read the swap file. To set the correct permissions type:
Terminalsudo chmod 600 /swapfileIf you are using Btrfs, swap files must be created on a No_COW, non-compressed file, otherwise
swaponcan fail. Create the swap file inside a directory withchattr +Cand compression disabled. For example:Terminalsudo mkdir -p /swap sudo chattr +C /swapThen create the swap file inside
/swap(for example,/swap/swapfile).Use the
mkswaputility to set up the file as Linux swap area:Terminalsudo mkswap /swapfileEnable the swap with the following command:
Terminalsudo swapon /swapfileTo make the change permanent, open the
/etc/fstabfile and append the following line:/etc/fstabini/swapfile none swap defaults 0 0To verify that the swap is active, use either
swaponor thefreecommand as shown below:Terminalsudo swapon --showoutputNAME TYPE SIZE USED PRIO /swapfile file 1024M 507.4M -1Terminalsudo free -houtputtotal used free shared buff/cache available Mem: 488M 158M 83M 2.3M 246M 217M Swap: 1.0G 506M 517M
How to adjust the swappiness value
Swappiness controls how readily the kernel moves memory pages out to swap. It defaults to 60 on most distributions, and you can check the current value with the sysctl
command:
sysctl vm.swappinessvm.swappiness = 60A lower value keeps pages in RAM for longer, which suits desktops and database servers with disk-backed swap, while systems that swap to zram want a much higher one. For the full range, the persistent configuration, and per-workload recommendations, see our guide on changing the swappiness value .
How to remove a swap file
If for any reason you want to deactivate and remove the swap file, follow these steps:
First, deactivate the swap by typing:
Terminalsudo swapoff -v /swapfileRemove the swap file entry
/swapfile none swap defaults 0 0from the/etc/fstabfile.Finally, delete the actual swap file using the
rmcommand:Terminalsudo rm /swapfile
FAQ
How much swap space should I create?
For systems with less than 2 GB of RAM, a swap size of twice the RAM is a common starting point. For systems with 2–8 GB of RAM, matching the RAM size is usually enough. For systems with more than 8 GB of RAM, 4–8 GB of swap is a reasonable ceiling unless you use hibernation, in which case swap must be at least the size of your RAM.
How do I resize an existing swap file?
Turn off the current swap, recreate the file at the new size, reformat it, and re-enable it:
sudo swapoff /swapfile
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfileMy system already has a zram device. Can I still add a swap file?
Yes. Some distributions, such as Fedora and Pop!_OS, enable a zram-based swap device by default. You can add a swap file alongside it using the same steps above. If you run swapon --show and see a /dev/zram entry instead of a file, that is expected. The new swap file will appear as an additional entry once enabled.
Conclusion
For more on memory management, the free and vmstat
commands give you a live view of swap usage. If your system is swapping heavily under normal load, adding more RAM is a more effective fix than increasing swap.
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page