Thursday, November 5, 2015

Adding a Swap File to an RHEL6 System

Identifying Current Swap Space Usage

The current amount of swap used by an RHEL 6 system may be identified in a number of ways.


# free -k
             total       used       free     shared    buffers     cached
Mem:       3082356    2043700    1038656          0      50976    1646268
-/+ buffers/cache:     346456    2735900
Swap:      4192956          0    4192956



Swapon command with option -s, displays the current swap space in KB.

# swapon -s
Filename                        Type            Size    Used    Priority
/dev/sda2                       partition       4192956 0       -1
 

# cat /proc/swaps
Filename                        Type            Size    Used    Priority
/dev/sda2                       partition       4192956 0       -1

Adding a Swap File to an RHEL 6 System

The following dd command example creates a swap file with the name “myswapfile” under /root directory with a size of 1024MB (1GB).

# dd if=/dev/zero of=/root/myswapfile bs=1M count=1024
1024+0 records in
1024+0 records out

# ls -l /root/myswapfile
-rw-r--r--    1 root     root     1073741824 Aug 14 23:47 /root/myswapfile
 
Configure the file as swap:
Change the permission of the swap file so that only root can access it.

# chmod 600 /root/myswapfile

Make this file as a swap file using mkswap command.

# swapon /root/myswapfile
 
Verify whether the newly created swap area is available for your use.

 
# swapon -s
Filename                        Type            Size    Used    Priority
/dev/sda2                       partition       4192956 0       -1
/root/myswapfile                file            1048568 0       -2

# free -k
             total       used       free     shared    buffers     cached
Mem:       3082356    3022364      59992          0      52056    2646472
-/+ buffers/cache:     323836    2758520
Swap:      5241524          0    5241524

Finally, modify the /etc/fstab file to automatically add the new swap at system boot time by adding the following line:  

/root/myswapfile    swap    swap   defaults 0 0

No comments: