This is a great little trick if for some reason you need a little extra Swap. You may face a situation where your RAM and Swap are being eaten up by a hungry app. Well, no problem, all though not as fast or efficient as extra RAM or a bigger Swap partition, it is still handy in some situations.
How to add more Linux Swap with Swap File
Assuming you want to put it in “/”, Create an empty 500MB file
$ sudo dd if=/dev/zero of=/swap.img bs=1024k count=512
Format it as a swap file
$ sudo mkswap /swap.img
Add it to your running Linux system
$ sudo swapon /swap.img
Optionally you can add /swap.img to fstab for automatic swap activation.
$ sudo nano /etc/fstab
Add this line at the end of the file
/swap.img none swap sw 0 0
Then Ctrl+X, Y to save and Enter to close Nano
Run “free -m” command to verify that you’ve added the newly created swap to your Linux based operating system.