Logo

How to Create a Swap File on CentOS 7

Apr 01, 2024

To create a swap file on CentOS 7, follow these steps:

Step 1 : Check current memory usage

free -m

Step 2 : Check disk space

df -h

Step 3 : Allocate space for the swap file (replace 2048 (2G) with desired size)

sudo dd if=/dev/zero of=/swapfile count=2048 bs=1MiB

Step 4 : Set appropriate permissions

sudo chmod 600 /swapfile

Step 5 : Set up swap space

sudo mkswap /swapfile

Step 6 : Enable the swap file

sudo swapon /swapfile

Step 7 : Verify swap is active

sudo swapon --show

Step 8 : Make the swap file permanent by editing /etc/fstab

sudo vi /etc/fstab

Add the following line:

/swapfile   swap    swap    sw    0   0

Congratulations! You have successfully created a swap file on CentOS 7.