Colton Wolkins

How to add more swap to a Linode/Digital Ocean Server Without Reinstalling

By the end of this post, you should be able to know how to add more “RAM” (via swap-space) to your Linux host without rebooting the system.

Why

From time to time, I begin to get emails about my system’s memory running low on my VPS hosted in Linode. You may have noticed that I included Digital Ocean in the title as well. That’s because, so long as you have root console access, whether that’s via SSH or direct, these steps will help to prevent your system to stop running out of memory. What we are doing is the equivalent of “Downloading more RAM”, and here’s how we’ll get it done.

What

In Linux, there are 3 locations where application memory may be stored. RAM, a Swap Partition, and a Swap File. RAM is a hardware limitation and typically needs a reboot to perform. A swap partition may be out of the question if your storage devices have been fully partitioned, leaving no room for expanding or adding additional swap partitions. A swap file is a file that we can create for swapping purposes that will work, so long as we have enough free space. The Windows equivalent to a swap file is pagefile.sys and it continually grows and shrinks as needed.

Adding a Swap File

  1. First we will want to create a 1G (or 2G, or 4G, etc) swap file via either fallocate
    1$ sudo fallocate -l 1G /swapfile
    If you get an error message saying that fallocate doesn’t exist, you can use dd instead to accomplish the same task, like so:
    1$ sudo dd if=/dev/zero of=/swapfile bs=1024 count=$((1*1024*1024))
    21048576+0 records in
    31048576+0 records out
    41073741824 bytes (1.1 GB, 1.0 GiB) copied, 3.87514 s, 277 MB/s
  2. Once the swap file has been created, next we need to make sure it’s setup as a swap file correctly by changing the file permissions and running the mkswap command on the file. Once that’s done, running swapon will enable the swap file as additional swap space on the fly. No reboot required.
    1$ sudo chmod 600 /swapfile
    2$ sudo mkswap /swapfile
    3Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
    4no label, UUID=a93790b6-72dc-4fae-a0d0-ac63ebf27284
    5
    6$ sudo swapon /swapfile
  3. To verify that swap is now enabled, the following two commands should reflect that the swap file has been enabled
    1$ sudo swapon --show
    2NAME      TYPE       SIZE  USED PRIO
    3/dev/sdb  partition  256M 91.1M   -2
    4/swapfile file      1024M    0B   -3
    5$ sudo free -h
    6               total        used        free      shared  buff/cache   available
    7Mem:           1.9Gi       382Mi        89Mi       0.0Ki       1.5Gi       1.4Gi
    8Swap:          1.2Gi        91Mi       1.2Gi
  4. As an optional step (though one that you may want to run anyways), you can add the swapfile to /etc/fstab so that the swapfile is enabled upon reboot.
    1/swapfile swap swap defaults 0 0

Conclusion

With all of that being said, you should now know how to create and enable a swap file. Or in other words, you now know how to “Download more RAM” in Linux. After running the above commands, I can confirm that my monitoring system sent me an email informing me that all was well. I’m no longer getting any emails about running out of swap or ram.

#linux #swap #memory #linode #vps #tutorial

Reply to this post by email ↪