Guides

Step-by-step tutorials and practical fixes for servers, systems, web setup, and daily technical work.
Linux Guides

How to Increase Swap Memory on Linux

Create, enable, and permanently configure swap memory on Linux servers with step-by-step commands.

Before you start

You need root or sudo access to create and enable a swap file.
Make sure the server has enough free disk space for the swap size you want to add.
Use a swap size that fits the server purpose instead of making it unnecessarily large.
If this is a production server, confirm current memory usage before making changes.

Step-by-step instructions

1. Create directory for swap file

Create a dedicated directory to store the swap file.

Command
mkdir /swap

2. Create 1GB swap file

Create a 1GB swap file using dd command.

Command
dd if=/dev/zero of=/swap/swapfile bs=1024 count=1048576
Tip

count value controls size. This example creates about 1GB swap.

3. Move to swap directory

Navigate to the directory where the swap file is created.

Command
cd /swap

4. Create swap area

Convert the file into a usable swap memory area.

Command
mkswap swapfile

5. Activate swap file

Enable the swap file so the system can start using it.

Command
swapon swapfile

6. Verify swap configuration

Check whether the swap memory is properly applied.

Command
swapon -s
Success

If swapfile is listed, the setup is complete.

Common issues

Not enough disk space

If the server does not have enough free storage, the swap file cannot be created successfully.

Swap disappears after reboot

This usually means the /etc/fstab entry was not added or was written incorrectly.

Swap is active but server is still slow

Swap can help prevent memory crashes, but it is much slower than real RAM. If memory pressure is constant, adding actual RAM may be the better solution.

About this guide

This guide explains how to increase swap memory on a Linux server by creating a swap file, enabling it immediately, and making it permanent after reboot. It is useful when your server has limited RAM or needs temporary memory protection during spikes.

How to follow this guide

  1. Check whether swap is already configured on the server.
  2. Create a swap file with the size you want to add.
  3. Set safe permissions on the swap file.
  4. Format the file as swap and enable it.
  5. Register the swap file in /etc/fstab so it stays active after reboot.
  6. Verify that the swap memory is working properly.

Why use this method?

Swap memory can help reduce memory pressure when physical RAM is low. It is not a replacement for real RAM, but it can help prevent crashes, protect services during traffic spikes, and make small servers more stable.

Frequently Asked Questions

What is swap memory?

Swap is disk space that Linux can use as extra virtual memory when physical RAM becomes full.

Is swap as fast as RAM?

No. Swap is much slower than physical RAM because it uses storage instead of memory.

Do I need to reboot after creating swap?

No. You can enable the swap file immediately with swapon, then add it to /etc/fstab for future boots.

How can I check if swap is active?

You can run free -m or swapon --show to verify that swap is enabled and visible to the system.