Introduction / Why This Is Needed
A swap file in Linux is a disk area used as an extension of RAM. When physical memory runs low, the system automatically moves inactive data from RAM to swap, preventing processes from crashing due to memory shortage.
Creating a swap file is a quick and flexible alternative to allocating a swap partition. You will be able to:
- Stabilize system performance during peak loads or when working with memory-intensive applications.
- Increase available memory without upgrading hardware.
- Configure swapping parameters for specific tasks (server, desktop, embedded system).
After completing this guide, you will have a configured and active swap file that will mount automatically at boot.
Requirements / Preparation
Before starting, ensure that:
- You have root account access or sudo privileges.
- There is free space on the disk. The swap file size depends on your needs:
- For systems with 4 GB RAM or less: swap size = 1.5–2 × RAM.
- For systems with 8+ GB RAM: 1–4 GB of swap is sufficient (or even less if RAM >16 GB).
- For servers with large databases: swap equal to RAM size may be required.
- Basic utilities are installed:
fallocate(usually in theutil-linuxpackage) ordd(available everywhere). Verify with:which fallocate.
Step-by-Step Instructions
Step 1: Check Current Swap Configuration
Before creating a new file, make sure you don't already have an active swap (e.g., a partition or default file).
free -h
or
swapon --show
What you will see:
total used free shared buff/cache available
Mem: 7.7G 1.2G 5.1G 156M 1.4G 6.1G
Swap: 2.0G 0B 2.0G
If the Swap line shows zero or the partition is absent in the swapon --show output, proceed with file creation.
Step 2: Create a File of the Required Size
The recommended method is fallocate, as it creates the file instantly without filling it with zeros.
sudo fallocate -l 2G /swapfile
Here:
-l 2G— file size (2 gigabytes). Replace2Gwith the needed value:1G,4G,512M, etc./swapfile— file path. Typically placed at root/.
Alternative for older systems (if fallocate is unavailable):
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
bs=1M count=2048 will create a 2048 MB (2 GB) file. This method is slower because it writes zeros to the entire file.
Step 3: Set Correct Permissions
The swap file contains sensitive data (process memory fragments). It must be accessible only to the superuser.
sudo chmod 600 /swapfile
Check permissions:
ls -lh /swapfile
Output should be: -rw------- 1 root root ... /swapfile.
Step 4: Format the File as Swap
Prepare the file for use as swap space:
sudo mkswap /swapfile
Example output:
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Note the UUID (if planning to use it in /etc/fstab), but usually specifying the file path is sufficient.
Step 5: Activate the Swap File
Enable the file in the system without rebooting:
sudo swapon /swapfile
Make the setting permanent (so swap activates after every reboot):
Open the /etc/fstab file in an editor (e.g., sudo nano /etc/fstab) and add this line at the end:
/swapfile none swap sw 0 0
Save and close the editor.
Step 6: Configure Swappiness Parameter (Optional)
The vm.swappiness parameter controls how aggressively the system uses swap. Values range from 0 (minimum) to 100 (maximum). The default is often 60.
For desktops (to avoid slowing down applications) — recommended 10–20.
For servers (to keep more data in RAM) — 1–5.
Set temporarily (until reboot):
sudo sysctl vm.swappiness=10
To make the value persistent, add to /etc/sysctl.conf:
vm.swappiness=10
And apply: sudo sysctl -p.
Verification
- Ensure swap is active:
swapon --show
Your/swapfileshould appear in the output. - Check total memory:
free -h
The Swap line will now show size and usage. - Confirm the setting is permanent (reboot the system and run
free -hagain). - (Optional) Check the
/etc/fstabentry:cat /etc/fstab | grep swap
There should be a line with/swapfile.
Potential Issues
❌ Error fallocate: fallocate failed: No space left on device
- Cause: Insufficient free space on the disk.
- Solution: Free up space (
df -hto check) or create a smaller file.
❌ Error chmod: changing permissions of ‘/swapfile’: Operation not permitted
- Cause: The file is on a filesystem mounted with
noexecornosuidoptions (e.g., some/homepartitions). - Solution: Create the file at root
/or on a partition where permission changes are allowed.
❌ Swap does not activate after reboot
- Cause: Error in
/etc/fstab(extra spaces, incorrect path) or the file was moved/deleted. - Solution: Check
/etc/fstabsyntax withsudo mount -a(errors will be displayed). Ensure/swapfileexists and has600permissions.
❌ System "freezes" with active swap usage
- Cause:
vm.swappinessvalue is too high (system aggressively writes to swap on a slow HDD). - Solution: Reduce
vm.swappinessto 1–5. For SSDs this is less critical but still advisable to configure.
❌ mkswap warns about file size
- Cause: File created with non-integer megabyte size (e.g., 2.5 GB).
- Solution:
mkswapusually works with any size, but for compatibility, create files with sizes divisible by 4 KB. Recreate the file with a size divisible by 1 MB (e.g.,2048Minstead of2G).