Add More Swap to Linode VMs

Swap space is a reserved area that is utilized when your RAM has become full. If this is occurring frequently on your Linode, you might want to consider resizing to a plan with a larger amount of RAM, as utilizing swap can potentially slow down your machine, as transfer speeds from Solid State Drives are not quite at the speed as what RAM can handle.

Outside of resizing our Linode plan to utilize more RAM, we have two main options to increase our Linodes Swap Size: Resizing the Swap partition, and utilizing a Swap File. Generally speaking, using a Swap File is an easier process, as you can do it while the system is live and running, unlike resizing your swap partition.

Option one: Resizing the Swap Partition.

Our first step will be determining if it is safe to resize our swap partition. Assuming your Linode is set with our normal configurations, your swap disk size will be roughly 512MB, and the normal root disk will be taking up the rest of the space on your Linode plan. We are going to need to decrease the total allocated space on our root disk, and reallocate that space to our swap disk. Lets make sure that we have the adequate amount of space by running df -h on your Linode. This will output the amount of used space on your Linode, and will key us into how much space we have to allocate to our swap partition. For the rest of this post, we will pretend that we do have enough space, and we are planning on resizing our current 512MB swap disk to 1024MB. I will be preforming this from a fresh deployed CentOS 7 Linode.

Once we have determined that it is safe to proceed, we will need to power down our Linode. Once powered off, we will want to navigate to the Advanced tab in the cloud manager, then select the three option dots for our main disk, then select “Resize”. We will want to figure out the difference in disk size for our resize that will free up our desired space. With that change in, we will now be able to select our swap disks size, and resize it to the new desired size.
Once rebooted, you can now see your swap size by running swapon -s.

Option two: Swap Files

Swap files are a bit more robust, as you can add them as your system is live. They do require issuing more commands, but they give the flexibility of adding more swap space on the fly.

To start, we will need to allocate the space for swap. To add 512Mb, we will want to run the following command:

sudo fallocate -l 512M /swapfile

From here, we will want to make sure our permissions are set correctly, then we will need to create the swap file:

sudo chmod 600 /swapfile
sudo mkswap /swapfile

Then we will want to enable our Swap File with this command:

sudo swapon /swapfile

And lets make sure to add the following line to our /etc/fstab:

/swapfile swap swap defaults 0 0

Now we can run swapon -s , and it will return our current swap disk, as well as our new swap file!