Add swap space to your existing Ubuntu 16.04 server

You set up your Ubuntu server but forgot to add some swap partition?

No problem. It is possible to add swap space to your server later by creating a swap file like you would have on Windows. All you have to do it the following:

First, create new swap file: to do so, you have to allocate all the space you want your swap file to use. The easiest way to do so it to use the dd command:

This command will create the file “swapfile” in your root directory with a size of 4 gigabytes.

Now we have to “format” the file by executing:

Now your swap file is created. You should secure your swap file by restricting access to it:

Once done so, all you have to do is to activate the new swap file:

You can check if the process was successful by checking:

Should output the following:

To make your swap file persistent so that it is active after every reboot, add the following line to your /etc/fstab file:

Leave a Reply

Your email address will not be published. Required fields are marked *