How to add swap space on a Ubuntu/Centos server

By | July 15, 2015

This post helps you to add swap space on a Ubuntu/Centos server


1 :  Login to server and switch as root user

$ su –


$ sudo su –

2 : Check your server for any swap files have been enabled

# swapon -s


Filename                                Type            Size    Used    Priority

The above empty list means, there are no swap files enabled on your server.

3 : Create swap file using dd command

# dd if=/dev/zero of=/swap.dat bs=1024 count=512k

The above command will create a swap file with the name “swap.dat” (you can use different file name) with size of 512 MB. If you need more space for swap, change the count value.  For example, if you need 1GB as swap, then count value should be 1024k.

4: Create Linux swap area

# mkswap /swap.dat

5 : Enable created swap file

# swapon /swap.dat

6:  Update /etc/fstab

Add the below line on /etc/fstab to enable the swap after server reboot

/swap.dat      none    swap    sw      0       0

7: Set correct permission for swap file

# chown root:root /swap.dat

# chmod 0600 /swap.dat

8: Verify created swap using free command

# free -m
total   used   free   shared   buffers   cached
Mem: 490   461   28   0   47   103
-/+ buffers/cache: 311 178
Swap: 511   21   490


# swapon -s
Filename       Type         Size         Used          Priority
/swap.dat       file            524284  21516        -1


Done !!!




Leave a Reply

Your email address will not be published. Required fields are marked *