How to add Swap file on CentOS server

I need additional swap space to improve my Linux server performance,So here is a simple tutorial you can follow:

Before we proceed to swap file setup, we have to check if any swap files exist by looking at the summary of swap usage.

swapon -s

If the summary is empty, it means no swap file exists.

As we know that we do not have a swap file enabled, we can check how much space we have on the server with the following command.

df

swap

The swap file will take 512MB and we are using only 26% so we can proceed.Now we can create the swap file using the dd command :

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

It simply looks like this:

swap

Next, create a swap file area using this command:

mkswap /swapfile

swap

Setup correct file permissions:

chown root:root /swapfile
chmod 0600 /swapfile

Activate swap using this command:

swapon /swapfile

Check the summary again:

swapon -s

swap

The last thing to do is to configure Swap to run and enabled each time your server reboots, edit the file nano /etc/fstab and add the following line at the end of file:

/swapfile none swap sw 0 0

Save changes and exit the editor.

You can verify swap is activated or not by simply using the below command:

swap

Done!!

Leave a Reply

Register to Get FREE Offers

Why Pay for Software when you can GET it for FREE

JOIN US TONIGHT!