How to Create SWAP File on Centos Server to Simulate RAM

Swap File:

Swap is known by many names. More commonly it is known as page file or paging file. So basically, a swap file is located on a hard drive. It is used as a temporary location. This temporary location is used by the computer’s RAM. A swap file is used to allocate more memory than the physical memory. That way a computer can use more memory than what is physically installed. Readers should know that those who are low on hard disk/drive space may experience system lag and or slow system performance. This is because the Linux swap file is unable to grow in size. If the file grows in size then don’t be alarmed, it is supposed to grow. The swap file size varies from a few kilo bytes to some hundred megabytes. The swap file by default is hidden on the operating system.


There are lots of things to make your server more responsive and safe from “out of memory” errors in your application. The easiest way to achieve all this is to CentOS add swap space. This will create some storage on your drive. The OS can now store data which can’t be held in the memory. This process helps the server to increase the amount of data it keeps in its working memory. This process provides a safety measure when your server runs low on memory. Reading and writing to and from swap is comparatively slower than using the actual memory. If we do not CentOS swap partition, it may show unwanted behavior. To free up memory it may start killing applications, or it may crash. This in turn will make you experience downtime and cause your unsaved data to be lost. Ensuring reliable data access means CentOS increase swap space and making it functional.

This article will cover the steps that are needed to Add or create a swap file on a CentOS VPS or Dedicated Server.


Before starting this article there are some things that need to be done first.

You need a CentOS server configured and installed. The CentOS server setup must be a non-root with sudo privileges. Once you’ve got the non-root server, use it to SSH into your CentOS server and continue installing your CentOS swap file.

Check the system for Swap Information:

It’s always good to check for already present swap space before creating new one. You should take a look at your server’s storage. It’s alright if you have multiple swap files or partitions. Generally, one is enough.

To see if the system already has any configured swap space, try using the swapon. This is a general purpose swap utility. swapon will display a summary of how much swap is used and how much is available on our device. We will need to use the –s flag with swapon as well.


If you enter this command and get nothing in return, then there exists no swap file and the summary was empty.

There is another way to check if there is swap space. That is by using free utility. This utility shows us the overall memory usage of the system. To see the swap usage and current memory, just type:


It can be seen clearly above that both methods are showing no swap space.

Check Available Storage Space:

The old fashioned way of allocating space for swap is using another separate partition that will be used just as swap space. But sometimes it is not possible to change the partition scheme because of hardware or software limitations. Luckily we can create a swap file easily that exists on a present partition.

We need to take a look at our current disk space usage. TO do this we need to type:


Note: the –h flag tell the dh to show the drive information in a more human friendly readable way.

The first line shows that the partition has 59 gigabytes of space available. That’s quite a bit of space to work with. You should know that this is being done on a fresh, medium-sized VPS instance. The actual usage of your system might be very different.

There are many different opinions about what’s the best size for swap space. It primarily depends on two things. Firstly it depends on your application requirements and then on your personal preference. Ideally, swap space equal to or double the amount of physical memory installed on your system is a good start up point.

This system that we’re using has 4GB of memory and doubling it will take more memory than I want it to. So I’ve decided to create a swap space of 4GB to match the memory on the system.

Create SWAP File on CentOS:

So now that we’re familiar with the available storage on our device, we’ll go ahead and make a swap file in our filesystem. We will make a file called swapfile in our root (/) directory. You can name this file whatever you want but for the sake of simplicity we named it swapfile. This file will be allocated the amount of space that our swap file needs.

We’re going to show you the easiest and the fastest way to create a swap file. fallocate is a command that lets you create a file with a pre-allocated size just like that. To create a file of 4GB we type:


After you have entered your password to authorize the sudo privileges, you’ll be prompted that the swap file has been created. That’s how quickly this command creates the file. To verify if the space reserved for the swapfile was correct or not simply do this:


So you see that our file was made with correct allocation of space.

Enable SWAP File on CentOS:

For all intents and purposes our file has been created. Right now our system does not know that the file we created is to be used for swap. We’re now supposed to tell our system that the file we created needs to be formatted as swap and then be enabled.

Before we go on to do that we need to adjust some permissions of our swap file. This will make sure that no one else can read this file except the root account. We can’t allow other users to read or write to this file because that would be a huge security risk. To do this we can use chmod:


This will restrict all the permissions in the read and write account only. To verify if the swap file has the correct permissions or not, we use:


Now we have secured our swapfile. We tell our system to set up the swap space by typing:


The swap file we created is now ready to be used. To use it just type:


To verify if the procedure was successful or not we can see if the system reports back the swap space:


The result shows us that we have a new swap file. To verify this we use free utility.


The swap file has now been created and the system will begin to use it when needed.

Make the Swap File Permanent:

The swap file that we created is enabled at the moment. The problem arises when we reboot. The server will not enable the file for use automatically. To make this exact thing happen we use fstab. fstab is a table that manages partitions and filesystems.

Use your text editor to edit the file with sudo privileges:


Go to the bottom of the file. You need to add to the file a line that will tell the system to enable and automatically use the swap file for use:


Now that you have added the file, close and save it. The server will check on every boot-up for this file. Now your swap file has been created and enabled for use…

Article Tools

Did this article help you?


an Author!

Write an Article