You can support us by downloading this article as PDF from the Link below. Download the guide as PDF

How can I create Swap on Ubuntu / Debian / CentOS & Fedora Linux?. Swap space in Linux is used when the amount of physical memory (RAM) is depleted. The inactive pages in memory are moved to swap space when the system needs more memory resources and RAM is full. Swap space is critical for machines with a small amount of RAM.

It is usually recommended to keep swap space in a dedicated swap partition on a hard disk. But for system installations on smaller disk size, this can be created on a file. The recommended amount of swap space increased linearly with the amount of RAM in the system.

Recommended System Swap Space

Here is the recommendation from Red Hat on the amount of swap to be used relative to the size of physical memory.

My system has 8 GB of RAM.

$ grep MemTotal /proc/meminfo | awk '{print $2 / 1024}'


I’ll create a swap file of 8 GB, which satisfies “At least 4 GB” formula.

Create Swap file in Linux

1 – Create an empty file:

sudo fallocate -l 8096M /swapfile

The same can be done with dd command.

$ sudo dd if=/dev/zero of=/swapfile bs=1G count=8 oflag=direct
8+0 records in
8+0 records out
8589934592 bytes (8.6 GB, 8.0 GiB) copied, 35.7336 s, 240 MB/s

2 – Set up the swap file using the command below.

$ sudo mkswap /swapfile
mkswap: /swapfile: insecure permissions 0644, 0600 suggested.
Setting up swapspace version 1, size = 8 GiB (8589930496 bytes)
no label, UUID=549d4cd2-f1cf-4839-8ddf-1a20df9af48e

3 – Change the security of the swap file.

The swap file should not be world readable. Only root user should have read and write permissions.

sudo chmod 0600 /swapfile

4 – Turn on swap file

Specify the fil on which the paging and swapping should take place.

sudo swapon /swapfile

5 – Enable the swap file at boot time

Let’s add the lines to enable swap file at boot to /etc/fstab file.

$ sudo nano /etc/fstab

# Mount swap file at boot
/swapfile swap swap defaults 0 0

The next time the system boots, it activates the new swap file.

6 – Test swap

Let’s verify if swap is active and can be used for paging. Use any of the three methods shown below.

$ cat /proc/swaps
Filename				Type		Size	Used	Priority
/swapfile                               file		8388604	0	-2

$ free -h
              total        used        free      shared  buff/cache   available
Mem:          7.3Gi       2.5Gi       2.7Gi       232Mi       2.2Gi       4.3Gi
Swap:         8.0Gi          0B       8.0Gi

$ sudo swapon --show
/swapfile file   8G   0B   -2

Removing Swap file

If you would like to remove a swap file, use below procedure.

1 – Disable the swap file.

sudo swapoff -v /swapfile

2 – Remove its entry from the /etc/fstab file.

3 – Remove the actual Swap file:

sudo rm /swapfile

You have learned how to create a Linux Swap and how to remove a Linux Swap file.

Other useful guides:

How to Optimize Linux System with Stacer

How to Monitor Linux Server with Netdata and Grafana

Best Linux Books for Beginners & Experts

Top 10 Free Backup software for Linux

Best Terminal File Managers for Linux

You can support us by downloading this article as PDF from the Link below. Download the guide as PDF


Please enter your comment!
Please enter your name here