Many times swap is not setup according to the requirement during the server setup/OS install, and once the server has been inducted in production its not possible to reinstall the OS. Or simply the swap size requirement can increase over time on a server. In such scenario we can add swap on the server using a swapfile.

Below are the exact steps you can follow to add swap to the server using swap file:

# dd if=/dev/zero of=/required/path/swapfile bs=1024 count=10000000

- makeing the swap file
# mkswap /required/path/swapfile

- turning the swap on
# swapon /required/path/swapfile

- verifying that swap is added
# free -m

And you are done, you can adjust the size of the of the swap using the ‘count’ parameter in dd command.

Some points to note for this :

  • You will need to make sure that you have enough space on related partition to do this
  • Its recommended to stop main services like apache/mysql on the server before doing this to avoid any data corruption.
  • If there is one server partition based swap already then it should be switched off, it can be done following below steps :

swapoff /dev/sdaX
-where sdaX represent the partition being used for swap.

-e.g. with following partition on the server :
root@linuxtech [~]# fdisk -l /dev/sda

Disk /dev/sda: 499.9 GB, 499989348352 bytes
255 heads, 63 sectors/track, 60786 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 3200 25599577+ 83 Linux
/dev/sda3 3201 4475 10241437+ 83 Linux
/dev/sda4 4476 60786 452318107+ 5 Extended
/dev/sda5 4476 5750 10241406 83 Linux
/dev/sda6 5751 6260 4096543+ 82 Linux swap / Solaris
/dev/sda7 6261 6515 2048256 83 Linux
/dev/sda8 6516 60786 435931776 83 Linux
root@linuxtech [~]#

-for above we can switch off swap using following command :

swapoff /dev/sda6

Tags: , ,

Leave a Reply