Add Swap Space on Linux

Linux

Find current swap space on the system

free -m

       total   used    free   shared   buffers  cached

Mem: 16051 15570 481 0 707 12644
-/+ buffers/cache: 2218 13833
Swap: 8189 0 8189

Or

swapon -s

Filename Type Size Used Priority
/dev/sda2 partition 8385920 88 -1

Or

cat /proc/swaps

Filename Type Size Used Priority
/dev/sda2 partition 8385920 88 -1

Method 1 – Use Hard Drive Partition

If you have additional hard disk or space available in existing disk then create partition using fdisk command. Assume /dev/sdc1 is newly created partition

Setup newly created partition as swap area

mkswap /dev/sdc1

Enable swap partition

swapon /dev/sdc1

Add following line to /etc/fstab to available even after reboot

cat /etc/fstab

/dev/sdc1 swap swap defaults 0 0
Verify whether newly created swap space is available

swapon -s

Filename Type Size Used Priority
/dev/sda2 partition 8385920 88 -1
/dev/sdc1 partition 10234430 0 -2

Method 2 – Use a file for Additional swap space

If you does not have any additional disk then you can create a file on your system and use that for swap space

Create the swap file called ‘myswap’ using following command (file size in bytes=bs*count)

dd if=/dev/zero of=/root/myswap bs=1M count=2048

Change the permission of newly created file

chmod 755 /root/myswap

Make this file as a swap file

mkswap /root/myswap

Add following line to /etc/fstab to available even after reboot

cat /etc/fstab

/root/myswap swap swap defaults 0 0
Verify whether newly created swap space is available

swapon -s

Filename Type Size Used Priority
/dev/sda2 partition 8385920 88 -1
/root/myswap file 2097152 0 -2