September 30, 2020

How to create SWAP partition in CentOS

1 min read

Unix systems, and other Unix-like operating systems, use the term “swap” to describe both the act of moving memory pages between RAM and disk,[citation needed] and the region of a disk the pages are stored on. In some of those systems, it is common to dedicate an entire partition of a hard disk to swapping. These partitions are called swap partitions. Many systems have an entire hard drive dedicated to swapping, separate from the data drive(s), containing only a swap partition. A hard drive dedicated to swapping is called a “swap drive” or a “scratch drive” or a “scratch disk“. Some of those systems only support swapping to a swap partition; others also support swapping to files.

How to create SWAP participant in CentOS

View swap information

swapon -s
free -h

Create a block file named swapfile

dd if=/dev/zero of=/swapfile bs=1024 count=4096k

Convert block file to the SWAP partition

mkswap /swapfile

Enable SWAP partition

swapon /swapfile

Configure SWAP file permissions

chmod -R 0600 /swapfile

Add the SWAP partition to automount on the system boot via /etc/fstab

echo “/swapfile swap swap defaults 0 0” >> /etc/fstab

Set memory and SWAP partition usage ratio; add usage ratio to configuration file /etc/sysctl.conf

echo “vm.swappiness = 60” >> /etc/sysctl.conf

After restarting the system, check to confirm that the SWAP partition is automatically mounted and in a normal state.

swapon -s
free -h