I recently created a swap file on my centos 6 based VPS using this guide. Now I wanted to extend this swap and I was reading through this guide. However, I think I've created a swap file than a partition. How do I go about extending this swapfile? The current size is 512M and I need to extend it by another 1G.

1 Answer 1


Can you show the results of

cat /proc/swaps

so we can be sure of your situation?

BTW - did you make sure the swap space (file or partition) is in your /etc/fstab?

  1. If you created an lvm swap partition, extending it is easy and you can use the same guide - basically:

    swap off; lvextend -L +1G VolGroup/LVswap; mkswap /dev/VolGroup-LVswap; swapon -a

  2. If you indeed created a file - you can either repeat the procedure, create another 1G file and let swap handle it (swap space does not have to be contiguous)

  3. Or you could simply remove the file (swapoff /path/to/swapfile; rm !$) and recreate it in a new size

  4. Or you can use dd to extend your swap file (again - make sure to swapoff first)

    dd oflag=append conv=notrunc if=/dev/zero of=/swapfile bs=1MB count=1024


  1. It's advisable to use the free command first, to ascertain swap is not used. As long as swap usage is 0, and no process is going to require more memory then physically available, there should be no issues.

  2. Every swap device must be present in /etc/fstab for swapon -a to detect them all.

  3. If using procedure (3) above, to keep swap "downtime" to a minimum, you can do the following:

3.1 Create new swapfile
3.2 Modify /etc/fstab to point to new swap file.
3.3 swapoff <path/to/OLD/swapfile>
3.4 swapon -a
3.5 rm <path/to/OLD/swapfile>

Optionally combine 3.3 and 3.4 as: swapoff /path/to/OLD/swapfile && swapon -a will ensure least amount of time without swap.

  • Thanks Dani_l. I actually followed step 3 to create a new swapfile, deactivated the smaller one and then deleted it. Activated the new one. Replaced the smaller one in /etc/fstab with the new one. Now I have one contigous swap file of 2G. I did think of step 2 but I did not know they need not be contigous. Thanks again!
    – hpb
    Jul 12, 2014 at 19:36
  • are the potential issues on doing swapoff on a swap file in use? also, after swapon, are there any extra steps needed to get the system to recognize the larger file?
    – Gaia
    Apr 16, 2015 at 17:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .