How To Add Swap on Centos 7

In this article, we intend to teach you How To Add a Swap on Centos 7. Let’s see what exactly Swap space means.

Introduction to Swap Space

A computer has a sufficient amount of physical memory but most of the time we need more so we swap some memory on disk. Swap space is a space on a hard disk that is a substitute for physical memory. It is used as virtual memory which contains process memory images. Whenever our computer runs short of physical memory it uses its virtual memory and stores information in memory on disk. Swap space helps the computer’s operating system in pretending that it has more RAM than it actually has. It is also called a swap file. This interchange of data between virtual memory and real memory is called swapping and space on disk is “swap space”.

Virtual memory is a combination of RAM and disk space that running processes can use. Swap space is the portion of virtual memory that is on the hard disk, used when RAM is full.

Swap space can be useful to computers in various ways:

  • It can be used as a single contiguous memory which reduces I/O operations to read or write a file.
  • Applications that are not used or are used less can be kept in a swap file.
  • Having sufficient swap files helps the system keep some physical memory free all the time.
  • The space in physical memory that has been freed due to swap space can be used by OS for some other important tasks.

In this guide, we will show how to create and enable a swap file on Centos 7.

How To Add Swap Space on Centos 7

To complete this guide, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide on Initial Server Setup with Centos 7.

Now follow the steps below to continue.

Check Server Storage for Swap on Centos 7

First of all, you should check your server’s storage to see if you already have some swap space available.
You can see if the system has any configured swap by using swapon, a general-purpose swap utility.

swapon -s

Note: If nothing is returned by the command, then the summary is empty and no swap file exists.

Another way is with the free utility, which shows us the system’s overall memory usage. You can see our current memory and swap usage (in megabytes) by typing:

free -m
 total       used       free     shared    buffers     cached
Mem:          3953        315       3637          8         11        107
-/+ buffers/cache:        196       3756
Swap:            0          0       4095

As you can see, our total swap space in the system is 0. This matches what we saw with Swapon.

At this point, you should check your available storage space on your Centos 7 server. To do this, you can use the following command:

df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        59G  1.5G   55G   3% /
devtmpfs        2.0G     0  2.0G   0% /dev
tmpfs           2.0G     0  2.0G   0% /dev/shm
tmpfs           2.0G  8.3M  2.0G   1% /run
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup

As you can see on the first line, our storage partition has 59 gigabytes available, so we have quite a bit of space to work with.

Note: Keep in mind that this is on a fresh, medium-sized VPS instance, so your actual usage might be very different.

Since my system has 4 gigabytes of memory and doubling that would take a larger chunk from my storage space than I am willing to part with, I will create a swap space of 4 gigabytes to match my system’s memory.

Create a Swap File on Centos 7

At this point, we will create a file named swapfile in our root “/” directory. To do this, you can use the dd utility:

sudo dd if=/dev/zero of=/swapfile count=4096 bs=1MiB

This command will create a 4GiG file.
Then, you can verify that the correct amount of space was reserved for swap by using ls:

ls -lh /swapfile
-rw-r--r-- 1 root root 4.0G Jul 25 11:00 /swapfile

Now you have a swap file and you must enable it.

Enable Swap File on Centos 7

At this point, you need to tell your system to format this file as a swap and then enable it.
First, you should set the correct permissions for the swap file to increase your security:

sudo chmod 600 /swapfile

This will restrict both read and write permissions to the root account only.

Verify that the swap file has the correct permissions by using ls -lh again:

ls -lh /swapfile
-rw------- 1 root root 4.0G Jul 25 11:00 /swapfile

Then, you can tell your system to set up the swap space for use by running the following command:

sudo mkswap /swapfile
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=b99230bb-21af-47bc-8c37-de41129c39bf

At this point, your swap file is ready to use as a swap space. To begin using it, run the command below:

sudo swapon /swapfile

You can see if the system has any configured swap again:

swapon -s
Filename				Type		Size	Used	Priority
/swapfile               file        4194300 0     -1

Also, you can use the free utility again to corroborate your findings:

free -m
             total       used       free     shared    buffers     cached
Mem:          3953        315       3637          8         11        107
-/+ buffers/cache:        196       3756
Swap:         4095          0       4095

At this point, your swap file is enabled at the moment, but when you reboot, the server will not automatically enable the file for use. You can change that by modifying the fstab file, which is a table that manages filesystems and partitions.

Edit the file with sudo privileges in your text editor:

sudo vi /etc/fstab

At the bottom of the file, you need to add a line that will tell the operating system to automatically use the swap file that you created:

/swapfile   swap    swap    sw  0   0

When you are finished adding the line, you can save and close the file. The server will check this file on each bootup, so the swap file will be ready for use from now on.


At this point, you learn to Add Swap space on Centos 7.

Hope you enjoy it.

You may be interested in these articles:

How To Find Hard Disk Information on Linux

How To Create a New Disk on Linux

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!