Archive for July, 2014

Adding swap space with a new swap file

Check the current swap space with

[root@testserver ~]# free -k or free -m or cat /proc/swaps or swapon -s

RUn the below command to Create a swap file with a size of 1GB

[root@testserver ~]# dd if=/dev/zero of=/root/myswapfile bs=1M count=1024

Now check the swap file created at /root/

[root@testserver ~]# ls -l /root/myswapfile

Change the permission of the swap file, in order to privent other user access except root

[root@testserver ~]# chmod 600 /root/myswapfile

Run the below command to make use of this swap file as swap for the server

[root@testserver ~]# mkswap /root/myswapfile

Now enable the swap file
[root@testserver ~]# swapon /root/myswapfile

Add the swapfile to fstab in order to make use of swap space which is newly added afer reboot also

[root@testserver ~]# vi /etc/fstab

Add below line to the fstab file

/root/myswapfile               swap                    swap    defaults        0 0

Now check the swap at the filesystem
free -m or free -k or swapon -s
Default swap and additional swap files and locations are added to fstab file. If we dont want use new swap then 

[root@testserver ~]# swapoff -a

[root@testserver ~]# swapon -a

‘-a’ indicates all the swaps will be retained what are added to fstab