How to Create a Swap File on CentOS 7

Try it in our public cloud & Get $50 Credit
CLAIM NOW

Swap files allow an operating system to free physical memory on your server by temporarily storing used memory to a hard drive. This can often increase performance, especially when your server is running a memory-intensive operation. For these reasons, it may be a good idea to create a swap file on your server. This guide demonstrates how to add more swap space to your server or create a new swap file if none exist.

Getting started

Confirm that you have the following before you follow this guide:
• 1 Node (Cloud Server or Dedicated Server) running CentOS 7.

No special tools are needed to manage swap files on your CentOS system. All of the tools we are going to use are included by default on nearly every Linux system.

Tutorial

Check your current swap configuration

First, check if your system already has a swap space configured. You can check using the “swapon” command:

swapon -s

If the command returns nothing, then you do not currently have any configured swap space. Otherwise it will return similar to the sample below. Each swap space will have a name and a size associated with it.

Output Sample:

swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 8388604 9820 -1

Creating and enabling a swap file

If your system doesn’t currently have a swap file, or if you would like to add more swap to your system, the following steps will allow you to create one.

First you must ensure that your system has available space to create a new swap file. To do so, use the “df” command. The “-h” argument is used so that it will output human-readable sizes rather than only bytes.

df -h

Output Sample:

df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 909G 21G 842G 3% /
tmpfs 3.8G 0 3.8G 0% /dev/shm
/dev/sda1 488M 62M 401M 14% /boot

Next choose the partition that you want to store your new swap file on. Make sure that the partition has enough space, as the swap file will often be several gigabytes in size. One you have chosen, create the new swap file using the “fallocate” command. In our sample command, “16G” means that it will create a swap file that is 16 gigabytes in size. This file will be placed into the root directory (/) with the name “swap16”.

fallocate -l 16G /swap16

If you system have more than one disk, you may put the swapfile on a disk that have low Input/Output usage. SWAP on a primary disc may slowdown a lot your system in case the Input/Output usage is already high.

It is very important that you set the permissions on the new swap file so that it can only be accessed by the root user. This can be done as follows:

chmod 600 /swap16

Now verify that the file was successfully created, with the correct permissions and size:

ls -lh /swap16

Output Sample:

ls -lh /swap16
-rw------- 1 root root 16G Jul 29 12:32 /swap16

If the file has the correct permissions and size, you can now create the new swap space using the file you just created:

mkswap /swap16

The last step is to mount the swap space to your system so that it can be used:

swapon /swap16

To verify that your new swap space has been correctly mounted to your system, use the “swapon” command, as we did previously:

swapon -s

Output Sample:

swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 8388604 9820 -1
/swap16 file 16777212 0 -2

If “swapon” now shows your new swap file, then it has been successfully mounted and is now in use by your system.

Make your swap file permanent

Normally you will want to make your swap file permanent, so that will persist even after your system has been rebooted. To make your new swap file permanent, you will need to add it to the fstab (file systems table) file so that it will be automatically mounted when your system boots.

Open the fstab file using your favorite file editor:

nano /etc/fstab

Now add the following line at the end of the fstab. Be careful to not modify the other lines in the file, as they are used to mount your other system partitions.

/swap16 swap swap sw 0 0

Once you’re done, save the file and exit. Your system will now automatically mount the new swap file when it boots.

Conclusion

You’ve now successfully created a swap file, added it to your system, and configured it to be mounted on boot. No more work needs to be done in the future to use your swap file; your system will handle it automatically. If you ever need to modify the amount of swap space on your system, you can create new swap files as we did here and add/remove them from fstab as needed.

If you found this article helpful, feel free to share it with your friends and let us know in the comments below!