Single Blog

How To Add Swap on CentOS 7

January 18, 2017, Written by 0 comment

Enable SWAP on your CentOS 7 Server

One of the best ways to make your server more responsive and guard against out-of-memory errors or applications crashing is to add SWAP space. SWAP is an area of storage drive where the Operating System can temporarily store data that it can not hold in memory.

This provides you with the ability to increase the amount of information that your server can keep in its working memory. reading from and writing to swap is lower than using memory, however it can provide a great safety net for when your server is low on memory. This is ideal for our smaller Virtual Servers.

When a server runs out of memory (RAM), it may start killing applications and processes to free up memory. This poses potential issues of data being corrupted, and downtime occuring.

This guide will show you how to create and enable a SWAP file on CentOS 7.

Check the System for Swap Information

Before starting, we should take a look at our server’s storage to see if swap space is available. While we can have multiple swap files or swap partitions, one should be sufficient.

We can see if the system has any configured swap by using swapon, a general-purpose swap utility. With the -s flag, swapon will display a summary of swap usage and availability on our storage device:

free -m

total used free shared buff/cache available
Mem: 993 443 77 77 473 212
Swap: 0 0 0

The total swap space in the system is 0. This matches what we saw with swapon.

Check Available Storage Space

Before we start, we should be aware of our current drive usage. We can get this information by typing:

df -h

Filesystem Size Used Avail Use% Mounted on
/dev/vda1 30G 15G 15G 50% /
devtmpfs 489M 0 489M 0% /dev
tmpfs 497M 0 497M 0% /dev/shm
tmpfs 497M 50M 447M 11% /run
tmpfs 497M 0 497M 0% /sys/fs/cgroup

As we have 15GB storage space available, there is space to create a SWAP storage file.

Create a SWAP File

The fastest and easiest way to create a swap file is by using fallocate. This command creates a file of a preallocated size instantly. We can create a 2 gigabyte file by typing:

 sudo fallocate -l 2G /swapfile

Once you’ve entered your password to authorize SUDO privileges, the SWAP file will be created instantly. entering your password to authorize sudo privileges, the swap file will be created almost instantly. To verify that the correct amount of space was reserved for swap by using ls:

ls -lh /swapfile

-rw-r--r-- 1 root root 2.0G Oct 30 11:00 /swapfile

Our swap file was created with the correct amount of space set aside.

Enable a SWAP File

Our file is created, but our system does not know that this is supposed to be used for swap. We need to tell our system to format this file as swap and then enable it.

Prior to this, we should adjust the permission on our SWAP file so that it isn’t readable by anyone besides the root account. Allowing other users to read or write to this file would be a huge security risk. We can lock down the permissions with chmod:

sudo chmod 600 /swapfile

This will restrict both read and write permissions to the root account only. Use ls -lh to check it has the correct permissions –

sudo chmod 600 /swapfile

-rw------- 1 root root 2.0G Oct 30 11:00 /swapfile

Now that our swap file is more secure, we can tell our system to set up the swap space for use by typing –

sudo mkswap /swapfile

Our swap file is now ready to be used as a swap space. Begin using it by typing:

sudo swapon /swapfile

To verify that the procedure was successful, we can check whether our system reports swap space now:

swapon -s

Filename Type Size Used Priority
/swapfile file 2097150 0 -1

This output confirms that we have a new swap file. We can use the free utility again to corroborate our findings:

free -m

total used free shared buff/cache available
Mem: 993 433 97 76 462 223
Swap: 2047 182 1865

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.

James Ensor