Create swap space on linux

Introduction

Swap space is a portion of disk storage that Linux can use as virtual memory when the available physical RAM is insufficient.

Swap can be configured either as a dedicated disk partition or as a swap file. A swap file is often easier to create and manage because it does not require repartitioning the disk.

This document explains how to create a 1 GB swap file at /var/swap.img, enable it, verify it, and configure it to remain active after a server reboot.

Note: Swap is not a replacement for physical RAM. It is primarily used as a fallback when RAM becomes highly utilized.


Prerequisites

Before creating swap space, ensure the following:

Server Requirements
  • Linux server with root or sudo access.
  • Sufficient free disk space.
  • A filesystem that supports swap files.
  • Basic knowledge of Linux commands.
  • Server should not already have an unnecessary or conflicting swap configuration.
Verify Current Memory and Swap

Check the available RAM and existing swap:

free -m

Check existing swap:

swapon --show

Check available disk space:

df -h

Example:

Filesystem      Size  Used Avail Use%
/dev/sda1 50G 35G 15G 71%

Make sure sufficient disk space is available before creating the swap file.


Implementation

Step 1: Check Existing Swap

Before creating a new swap file, check whether swap is already configured:

swapon --show

You can also use:

free -m

Example:

               total        used        free
Mem: 7980 6200 1780
Swap: 0 0 0

If the system already has sufficient swap, creating another swap file may not be necessary.


Step 2: Create the Swap File

Create a 1 GB swap file using:

sudo dd if=/dev/zero of=/var/swap.img bs=1024k count=1000

This creates:

/var/swap.img

with approximately 1 GB of space.

A faster alternative on many modern Linux systems is:

sudo fallocate -l 1G /var/swap.img

Step 3: Set Secure Permissions

Set the swap file permissions so that only root can read or write it:

sudo chmod 0600 /var/swap.img

Set the owner:

sudo chown root:root /var/swap.img

Verify:

ls -lh /var/swap.img

Expected permissions:

-rw------- 1 root root 1.0G /var/swap.img

Step 4: Format the File as Swap

Initialize the file as swap space:

sudo mkswap /var/swap.img

You should receive output similar to:

Setting up swapspace version 1

Step 5: Enable the Swap File

Enable the swap file:

sudo swapon /var/swap.img

Verify:

swapon --show

Example:

NAME           TYPE  SIZE USED PRIO
/var/swap.img file 1G 0B -2

Step 6: Verify Swap Memory

Run:

free -m

You should now see approximately 1 GB of swap:

               total        used        free
Mem: 7980 6200 1780
Swap: 1000 0 1000

Configure Swap to Persist After Reboot

By default, a manually enabled swap file may not automatically be enabled after a reboot.

To make it persistent, edit /etc/fstab:

sudo nano /etc/fstab

Add the following line:

/var/swap.img none swap sw 0 0

Save the file and exit.

Alternative using vi

sudo vi /etc/fstab

Add:

/var/swap.img none swap sw 0 0

Step 5: Test the /etc/fstab Configuration

Before rebooting, test the configuration:

sudo swapoff /var/swap.img
sudo swapon -a

Then verify:

swapon --show

And:

free -m

If the swap appears again, the /etc/fstab entry is working correctly.

Important: Always verify /etc/fstab carefully before rebooting. An incorrect fstab entry can cause boot-related problems.


Verify Final Configuration

Run:

swapon --show

Then:

free -h

Also verify the configuration:

grep -i swap /etc/fstab

Expected:

/var/swap.img none swap sw 0 0

You can also check:

ls -lh /var/swap.img

Troubleshooting

Swap file is already active

Check:

swapon --show

If /var/swap.img is already listed, do not run mkswap on the active file.


swapon gives a permission error

Check permissions:

ls -l /var/swap.img

Set:

sudo chmod 600 /var/swap.img
sudo chown root:root /var/swap.img

Then:

sudo swapon /var/swap.img

mkswap reports that the file has insecure permissions

Run:

sudo chmod 600 /var/swap.img

Then:

sudo mkswap /var/swap.img

Check whether the filesystem supports swap files

If swapon fails, check the filesystem:

df -T /var/swap.img

Some filesystem/configuration combinations require special handling for swap files.


Conclusion

Creating a swap file is a simple way to provide additional virtual memory without creating a dedicated disk partition.

The basic process is:

Check RAM and disk
       ↓
Create swap file
       ↓
Set permissions
       ↓
Run mkswap
       ↓
Enable with swapon
       ↓
Verify with free/swapon
       ↓
Add entry to /etc/fstab
       ↓
Test persistence

A swap file can help prevent applications from being immediately terminated when physical RAM becomes exhausted. However, high swap usage is usually an indication that the server may need more physical RAM or workload optimization, rather than a reason to continually increase swap.


FAQs

Q1. What is swap space?

Swap is disk space used by Linux as additional virtual memory when physical RAM is under pressure.


Q2. Is swap the same as RAM?

No. RAM is physical memory and is significantly faster than disk-based swap.


Q3. How much swap should I create?

There is no single value suitable for every server. The appropriate size depends on the server’s RAM, workload, and whether applications require swap.

For example, this procedure creates:

1 GB swap

You should determine the required size based on the server’s workload rather than following a fixed ratio.


Q4. How can I check whether swap is enabled?

Use:

swapon --show

or:

free -h

How do to add a swap space to an EC2 instance? – Pheonix Solutions

How to create the swap space using the swap file on Ubuntu 20.04? – Pheonix Solutions

Talk to our experts:

Looking for the right technology solution for your business?. Our experts can help with web hosting, domain registration, DevOps and cloud, software development, web applications, mobile applications, and enterprise solutions. Get in touch with our team here.

admin

Writes about Web & Architecture at Pheonix Solutions.

Leave a Reply

Scroll to Top