Creating Additional Swap Disk in your running Linux/Ubuntu server

I had small-default swap memory which i wanted to increase.

But it was a running machine, so i needed to do it painless, without stopping the machine.

I did the following steps which i am sharing here now:

First, check the current swap situation on your system by running:

sudo swapon –show

Decide how much swap you want to add. This will depend on your specific use case and system. For this example, let’s say you want to add 2 GB of swap. Convert this size to megabytes (MB). 1 GB is 1024 MB, so 2 GB is 2048 MB.

sudo fallocate -l 2048M /swapfile

Change the permissions of the swap file so that only the root user can read and write to it:

sudo chmod 600 /swapfile

Make this file a swap file with the mkswap command:

sudo mkswap /swapfile

Now you can enable the new swap file:

sudo swapon /swapfile

To make this swap file permanent, you need to add it to the /etc/fstab file:

echo ‘/swapfile none swap sw 0 0’ | sudo tee -a /etc/fstab

Verify that the swap is working by checking the output of swapon –show again:

sudo swapon –show

Done, now you should see something like “/swapfile file 2.0G 0B -4” in your swapdisk list.


Discover more from WP DEV - Elvin Haci

Subscribe to get the latest posts sent to your email.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from WP DEV - Elvin Haci

Subscribe now to keep reading and get access to the full archive.

Continue reading