How to create a Swap FILE in CentOS 7 – with Video

Last updated on December 20th, 2019 at 05:46 am.

How to Create a Swap file in Centos 7

Creating a Swap FILE in CentOS 7

If you are on a VPS and you do not have the capability to create swap disks, the next alternative is for you to use a Swap File. A swap File will act as Memory when your VPS Memory (RAM) is completely depleted.

VPS Tutorial – Setup CentOS 7, Run Multiple Websites :

In this short post let’s see how to create a Swap File in CentOS7.

Watch the video embedded above to learn how to create a Swap file in Centos .

Use the following command to check the Swap info for your Server. This will help you understand if your Linux server already has a swap file / disks created or not.

sudo swapon -s 

You may also use the following to check your server Memory available.

sudo free -m 

To check  the available disk you may use :

sudo df -h 

Creating The Swap File

We’ll create the swap file in the /root directory

Utilize the following command to create the file in the root directory. Note that, theswap  is the name of the swap file, you can rename yours if you so wish.

sudo dd if=/dev/zero of=/root/theswap bs=1M count=2048 

in the above command, 2048 is the size of our swap, theswap is the name of our swap file.

Confirm that the file has been created by listing the file and it’s properties:

sudo ls -l /root/theswap

Next, for security reasons,  change the permissions of theswap file :

sudo chmod 600 /root/theswap

Confirm the changes have taken effect by listing the file and it’s properties:

sudo ls -l /root/theswap

Convert the created file to a swap file using the following command.

sudo mkswap /root/theswap

Next, enable the new Swap File (theswap)

sudo swapon /root/theswap

Next, let’s add the new file into /etc/fstab so that it can be part of the mounted partitions/memory .

sudo nano /etc/fstab

Once the config file /etc/fstab opens up in the nano editor, add the following at the end of it.

/root/theswap                              swap                    swap    defaults        0 0

Then press CTRL X to exit , press Y to accept changes , then Press enter.

Verify that the new swap is available for use:

sudo swapon -s 

You may also use the following to check it in your  available memory.

sudo free -m 

You may even create another swap file if you so wish, just rename the file to something different.

There you go, you have a new swap area on your Centos 7 server.

Comment Here

Join this free course:

How to host multiple WordPress
websites on a VPS

Close me!