How to Add Swap on CentOS & RHEL 8/7/6

por | 13 julio, 2021

It is a good practice to create swap on system during operating system installation. But in some cases if you don’t have enabled swap on your system during installation and you are in required to enable swap on system. This article will help you steps by step instructions to enable swap on CentOS & RHEL systems.

Swap is very useful for that system which required more RAM that physical available. If memory is full and system required more RAM to run applications properly it check for swap space and transfer files there. In general terms swap is a part of hard disk used as RAM on system.

  • Check System Swap – Before working make sure that system has already swap enabled. If there are no swap, you will get output header only.

swapon -s

Create Swap File – Lets create a file to use for swap in system of required size. Before making file make sure you have enough free space on disk. Generally its recommends that swap should be equal to double of installed physical memory.I have 4 GB physical RAM installed in my system. So I am creating swap of 8 GB in size.

fallocate -l 8G /swapfile chmod 600 /swapfile

Make It Swap – Now make is swap usable file using mkswap command.

mkswap /swapfile

Enable Swap – Now setup the swap for system using swapon command.

swapon /swapfile

Now again check that swap is enabled or not. You will see results something like below.

sudo swapon -s

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

Setup Swap Permanent – Append the following entry in /etc/fstab file to enable swap on system reboot.

vim /etc/fstab

/swapfile none swap sw 0 0

Setup Kernel Parameter – Now change the swappiness kernel parameter as per your requirement. It tells the system how often system utilize this swap area.Edit 

/etc/sysctl.conf file and append following configuration in file.

vim /etc/sysctl.conf

vm.swappiness=10

Now reload the sysctl configuration file

sysctl -p

At this point you have successfully enabled swap on your CentOS or RHEL system.