CentOS/RHEL: How To Create A SWAP FILE TO Increase Swap Size

I would like to increase swap size on my centos or rhel system. How Do I create a SWAP file to increase swap space under linux operating system? How to add a swap file on CentOS 7/6.5 or RHEL 7/6.5 linux? this guide will show you how to add a swap file and enable the swap file to current system.

You need to use dd command to create swap file firstly and then using mkswap command to setup a linux swap area in that swap file.

​CentOS/RHEL Create a Swap File 


Step1# Checking the current swap size by runnging the following command:

cat /proc/meminfo | grep -i swap

or

free

Outputs:

[root@devops ~]# cat /proc/meminfo | grep -i swap

SwapCached:         9600 kB

SwapTotal:       2129916 kB

SwapFree:        2054652 kB

[root@devops ~]# free 

             total       used       free     shared    buffers     cached

Mem:       1018256     947056      71200       1780         20     187616

-/+ buffers/cache:     759420     258836

Swap:      2129916      75264    2054652

Step2# using “dd” command to create a 100MB SWAP file, type:

dd if=/dev/zero of=/swaptest bs=1M count=100

outputs:

[root@devops /]# dd if=/dev/zero of=swaptest bs=1M count=100

100+0 records in

100+0 records out

104857600 bytes (105 MB) copied, 0.255226 s, 411 MB/s

Step3# seting up a linux swap area in above swaptest file

You can use “mkswap” command to setup a linux swap area, issue the following command:

mkswap /swapfile

outputs:

[root@devops /]# mkswap swaptest 

Setting up swapspace version 1, size = 102396 KiB

no label, UUID=490f1d73-b207-4727-8eac-5a08f57a865f

Step4# Enabling swaptest file for swaping, issue the following command:

swapon /swaptest

outputs:

[root@devops /]# swapon  /swaptest
swapon: /swaptest: insecure permissions 0644, 0600 suggested.

Form above outputs, you will find that the default permission of swaptest is 0644, and it’s insecure. so type the following command to change file permission as 0600.

chmod 600 /swaptest

Step5# checking the swap space again by runnging the following command:

cat /proc/meminfo | grep -i swap

outputs:

[root@devops /]# cat /proc/meminfo | grep -i swap
SwapCached:        10164 kB
SwapTotal:       2232312 kB
SwapFree:        2157380 kB

If you want to enable the swaptest file while system starts up, then you need to add the below configuraton into “/etc/fstab” file.using vim command to edit “/etc/fstab” file:

vim  /etc/fstab

Adding the following line:

/swaptest none swap sw 0 0

Now when system reboot, the swaptest swap area will enable automatically.

If you want to disable swap file for swapping, you can use swapoff command,type:

swapoff /swaptest

done….

You might also like:

Sidebar



back to top