Creating Swap File

From PrgmrWiki
Revision as of 06:56, 31 January 2016 by Srn (talk | contribs) (missing chmod)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

To add a swap file to your vps run the following as root

To add a 500mb swap file change count=500 to another number for a smaller or bigger swap file

# dd if=/dev/zero of=/var/swap2 bs=1024k count=500
# chown root:root /var/swap2
# chmod 600 /var/swap2
# mkswap /var/swap2
# swapon /var/swap2

To have the swap file used by the kernel after a reboot, add the following to /etc/fstab (its the same as running swapon)

/var/swap2       none    swap    sw      0       0

To expand existing swap space after memory upgrade:

  • Turn off existing swap: swapoff -a
  • Remove old swap: rm /var/swap
  • Create new larger swap, in this example we are going from the 500mb above to 1024mb: dd if=/dev/zero of=/var/swap bs=1024k count=1024
  • Set permissions: chmod 600 /var/swap
  • Make it swap: mkswap /var/swap
  • Turn it back on swapon -a
  • run the free command to confirm its there with the size you want.