We may face an issue with insufficient swap partition space on our Linux servers and we aren't able to extend it using swap partition due to unavailability of free partitions on the disk. In such cases we can extend or increase swap space using a swap file created manually by using a few commands:
Here are the steps to extend Swap Space using a swap file in Linux:
First, we need to check the size of existing swap space/partition using the command like 'free -m' or 'swapon -s'
- Create a swap file with a size of 1 GB using 'dd' command:
# dd if=/dev/zero of=/swap_file bs=1G count=11+0 records in
1+0 records out
1073741824 bytes (1.1 GB) copied, 414.898 s, 2.6 MB/s
Here you can replace the values of ‘bs‘ and ‘count‘ fields according to your requirement.
- We need to secure the newly created swap file with the permission: 644.
# chmod 600 /swap_file
- Make the swap area on the file using 'mkswap' command like the following:
# mkswap /swap_fileSetting up swapspace version 1, size = 1048572 KiB
no label, UUID=e8b4ac59-c90a-4fk3-ja9d-d92dbb7sd3f3fb
- Then we need to add the following entry to the 'fstab' file so that the swap file will become persistent across every reboot.
/swap_file swap swap defaults 0 0
- Enable the swap file using ‘swapon’ command:
# swapon /swap_file
- Now we can check if the swap space is increased or not:
# free -m