How to increase swap partition size using a file.

How to increase swap partition size using a file.

by -
1 1563

How to extend swap partiton size through a file

Linux server is running out of swap space so in that case we can extend the swap space using swap partition
but due to unavailability of free partitions on the disk.we are unable to extend it.
Then we will extend swap space through a file.

Here are the Steps to extend Swap Space using Swap File in Linux.

Step1:- First we will check existing swap space through free -m or swapon -s

[root@kvit ~]# free -m
total used free shared buffers cached
Mem: 2883 472 2411 0 11 113
-/+ buffers/cache: 347 2536
Swap: 1999 0 1999

Note:- In my machine swap size is near to 2GB. Now i will extend to 6GB

Step2:- Create a swap file of size 6 GB using through dd Command.

[root@kvit ~]# dd if=/dev/zero of=/swap-file1 bs=3G count=3
0+3 records in
0+3 records out
6442438656 bytes (6.4 GB) copied, 14.2898 s, 451 MB/s

[root@kvit ~]# du -sh /swap-file1
6.0G /swap-file1

Note:- Change bs or count size as per your requirement.

Step3:- Secure the swap file with permissions 644

[root@kvit ~]# chmod 600 /swap-file1

Step:-4 You need to enable the Swap Area on the file (swap-file)

[root@kvit ~]# mkswap -f /swap-file1
Setting up swapspace version 1, size = 6291440 KiB
no label, UUID=8105c9f2-cdae-47f6-a2d7-93b8f4369e01

Step:-5 Add the swap file entry in the fstab file.

[root@kvit ~]# vim /etc/fstab

# /etc/fstab
# Created by anaconda on Thu Jan 15 19:58:21 2015
# Accessible filesystems, by reference, are maintained under ‘/dev/disk’
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
UUID=4d6ae4aa-3580-4b3a-9e02-17dee4f8be2c / ext4 defaults 1 1
UUID=6276eb9d-0368-4f4d-910e-d0cf1284a0b9 /boot ext4 defaults 1 2
#UUID=cc0055e8-637d-4a6d-ba6a-340994289413 swap swap defaults 0 0
/swap-file1 swap swap defaults 0 0                  <——insert this line>
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0

Note:- Comment existing swap partion line.
Step:-6 Enable the swap file using ‘mkswap on’ command.

[root@kvit ~]# swapon /swap-file1
[root@kvit ~]#

Note:- You can also disable swap file while troubleshooting.after that you have to follow 6th step for enabling.

[root@kvit ~]# swapoff /swap-file1
[root@kvit ~]#



Refrence Sites:

Senior Linux Administrator at KV IT-Solutions Pvt Ltd || A Linux Lover || having 4 years of experience || Handling various Linux projects || Email ID : || cell: 9818001497

“We are born free, No gates and Windows can snatch our freedom “

Download PDF


Leave a Reply

Required Captcha *