Introduction / Why You Need This
Swap space is an area on disk used as an extension of RAM. When physical memory runs low, the Linux kernel moves inactive memory pages to swap, freeing up RAM for more important processes. This helps prevent programs from being abruptly terminated due to out-of-memory (OOM) conditions and improves overall system stability, especially on servers with limited RAM or desktops running multiple heavy applications simultaneously.
In this guide, you'll learn how to create and configure a swap file—the most flexible and modern way to add swap space in Linux. You'll be able to control its size, enable/disable it without rebooting, and configure system behavior when using swap.
Requirements / Preparation
Before starting, ensure:
- You have access to the system with sudo or root privileges.
- The target disk (usually
/or/home) has enough free space. Recommended swap size: from 2 GB to the size of your RAM (see FAQ). - You know the path to the partition where the file will reside. You can check with
df -h. - Basic utilities are installed:
fallocate(usually in theutil-linuxpackage) ordd. Iffallocateis unavailable, usedd.
Step 1: Check Current Swap Space
First, let's see if there's already active swap and how much it occupies. Run:
sudo swapon --show
Or use the free utility:
free -h
Look for the Swap: line in the output. If it shows 0B or the swap partition is missing from the list, there's no active swap, and you need to create one.
You can also check if a swap partition already exists in the system using:
lsblk -f
Look for the swap type in the FSTYPE column.
Step 2: Create the Swap File
We'll create the swap file in the root directory (/). You can choose another location, such as /swapfile or /var/swap. We'll specify the size in gigabytes. For example, we'll create a 4 GB file.
Method A: Using fallocate (fast, recommended)
sudo fallocate -l 4G /swapfile
This command instantly creates a file of the specified size, reserving disk space. If fallocate doesn't work on your filesystem (for example, on some older ext4 versions with -o discard enabled), use Method B.
Method B: Using dd (slower but more universal)
sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress
Here, bs=1M is a 1-megabyte block, count=4096 is the number of blocks (4096 * 1MB = 4GB). status=progress shows the progress (available in modern versions of dd).
⚠️ Important: Ensure there is enough space on the target partition. The
df -hcommand will show free space.
Step 3: Set Permissions and Convert to Swap
By default, the created file is readable and writable by everyone. This is insecure for a swap file. Set the correct permissions:
sudo chmod 600 /swapfile
Now only the root user can read and write to this file.
Next, initialize the file as a swap area:
sudo mkswap /swapfile
The command output will look something like:
Setting up swapspace version 1, size = 4 GiB (4294967296 bytes)
no label, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Remember or save the UUID (unique identifier)—you'll need it for configuring /etc/fstab.
Step 4: Activate the Swap File
Now enable the new swap space in the current session:
sudo swapon /swapfile
Check its presence again:
sudo swapon --show
You should see your /swapfile in the list with its size and type file.
Also, the free -h command will show the total swap volume in the Swap: line.
Step 5: Configure Settings and Make Permanent
5.1 Add to /etc/fstab
To have swap activate automatically at every system boot, add an entry to the /etc/fstab file. Open it for editing (for example, sudo nano /etc/fstab) and add the line:
/swapfile none swap sw 0 0
If you want to use UUID (a more reliable method, especially if the file path might change), specify:
UUID=your-uuid-from-step-3 none swap sw 0 0
Save the file and close the editor.
5.2 Configure vm.swappiness (Optional)
The vm.swappiness parameter controls how aggressively the kernel will use swap. Values range from 0 to 100.
0— the kernel will avoid swap until RAM is completely exhausted.100— the kernel will actively use swap.- The default in most distributions is
60.
Check the current value:
cat /proc/sys/vm/swappiness
Change temporarily (until reboot):
sudo sysctl vm.swappiness=10
Change permanently: add a line to /etc/sysctl.conf or create a separate config in /etc/sysctl.d/99-swappiness.conf:
vm.swappiness=10
Apply changes without rebooting:
sudo sysctl -p
For most desktop systems, the recommended value is 10-30. For servers with large amounts of RAM, you can leave it at 60 or even lower it to 10.
Verify the Result
- Reboot the system (
sudo reboot) to ensure swap activates automatically. - After booting, run:
Your swap file should appear in the output.sudo swapon --show free -h - Check that the entry in
/etc/fstabdoesn't cause errors at boot. You can run:
If the command completes without errors, the configuration is correct.sudo mount -a - Monitor swap usage in real time:
Or use thewatch -n 2 free -hhtoputility (if installed), where swap is displayed at the top.
Possible Issues
Problem: fallocate doesn't work, file is created with zero size
Solution: Use dd (Method B). This can happen on filesystems that don't support fallocate (for example, some NFS configurations or older ext4).
Problem: Activation produces error swapon: /swapfile: swapon failed: Invalid argument
Solution: Most likely, the file is created incorrectly or has wrong permissions. Ensure that:
- The file exists and has a size greater than zero (
ls -lh /swapfile). - Permissions are set to
600(sudo chmod 600 /swapfile). - The file is initialized via
sudo mkswap /swapfile.
Problem: Swap doesn't activate after reboot
Solution:
- Check the syntax of the entry in
/etc/fstab. It's better to use UUID. - Check the output of
sudo mount -afor errors. - Ensure that the
/swapfileexists at the specified path and hasn't been deleted.
Problem: System doesn't use swap even when memory runs low
Solution: Check the vm.swappiness value. If it's 0, the system will try to avoid using swap. Increase it to 10 or 20. Also check if swap is full (free -h shows Swap: 4.0G 0B 4.0G—that's normal if 0B is used).
Problem: Performance drops when swap is actively used
Solution: Swap on an HDD (traditional hard disk) significantly slows down the system. Consider:
- Adding more RAM (the most effective solution).
- Using a swap file on an SSD (much faster, but drive wear will increase).
- Configuring
vm.vfs_cache_pressureand other parameters in/etc/sysctl.confto optimize caching. - Increasing
vm.swappinessto0if swap is only used as an emergency buffer.
Problem: Not enough disk space to create a swap file
Solution: Free up space by removing unnecessary files (/var/log, package caches, old kernels). Alternatively, create the swap file on another partition/disk with free space. As a last resort, you can create a swap partition (via fdisk/gdisk and mkswap), but that will require repartitioning the disk and a reboot.