Web Hosting Talk







View Full Version : Procedure To Add a Swap File


php-master
12-13-2011, 06:03 PM
You need to use the dd command to create swap file. The mkswap command is used to set up a Linux swap area on a device or in a file.

a) Login as the root user.

b) Type following command to create 512MB swap file (1024 * 512MB = 524288 block size):
# dd if=/dev/zero of=/swapfile1 bs=1024 count=524288

c) Set up a Linux swap area:
# mkswap /swapfile1

d) Activate /swapfile1 swap space immediately:
# swapon /swapfile1

e) To activate /swapfile1 after Linux system reboot, add entry to /etc/fstab file. Open this file using a text editor such as vi:
# vi /etc/fstab

Append the following line:
/swapfile1 swap swap defaults 0 0

So next time Linux comes up after reboot, it enables the new swap file for you automatically.

g) How do I verify swap is activated or not?
Simply use the free command:
$ free -m

silasistefan
12-14-2011, 04:20 AM
I would add that, in order to append the following line (step e) you should go with the arrows at the end of the file and then press "o" (like in other).

To stop the editing press "Ctrl+C"
To save the file type ":wq"

php-master
12-14-2011, 05:04 AM
for this line:
# vi /etc/fstab
you can edit with nano command:
# nano /etc/fstab
its easy. add line and then press Ctrl+X, (for save chenges press Y)
;)

php-master
12-14-2011, 05:05 AM
if nano is not work run it:
# yum install nano
its for centos OS

Kurikami
12-14-2011, 09:04 AM
Since this is posted in VPS tutorials, most likely should mention that some providers who don't offer swap, might not allow swap at all and will suspend your services if you create swap without their knowledge. Same goes for increasing swap of providers that have pre-created swap.

So be sure to consult your host ;)