Quick guide on creating a swap file for your Linux masternode server
In order to operate a masternode you first need a VPS. Well, you can buy one from Vultr or Digital Ocean. They are most reliable and the most recommended VPS provider for masternodes. If you are not sure how to choose the right server then check out the following guides.
Digital Ocean masternode server setup
Vultr VPS configuration for masternodes
Both the above guide explains the server deployment process in detail. It goes from choosing the server type, size to all the other settings to create a perfect masternode server.
Now after configuring the server you need to install the binaries and compile the necessary files. But in order to compile the wallet file you need a VPS with atleast 2 GB of RAM.
Well, to save some bucks you might have opted a server with 1 GB of RAM.
1 CPU and 1 GB of RAM is the bare-minimum server which is enough to run a masternode. However it is not enough to compile the wallet and is not enough to run more than 1 masternode.
A VPS with less than 2 GB of RAM in most cases will fail to build the binaries. So if you have a server with less than 2 GB of RAM; you’ll need to create a Swap file.
What is swap file and why is it needed for masternodes?
As we said; swap file is needed to compile the wallet on your server. But is only needed if your server has not enough RAM (less than 2 GB).
Swap is designed as a virtual memory. The purpose of creating a swap file is to improve the servers efficiency and responsiveness. It extends the RAM size by allocating the portion of the hard drive space to store data. This virtual memory allows the server to run more applications than it could run in RAM alone.
So if your server has only 1 GB RAM then swap will make a huge difference. It will keep your server from crashing when RAM is under pressure. And it also helps the server run application that consumes a large amount of memory without any issues.
Note: While swapping increases the available RAM space the downside is that the disks are slow.
Anyways this is what going to help build the binaries on server. Moreover if you are looking to run 2 or more masternodes on a same VPS then you could take advantage of this virtual memory.
So let’s get started.
Here in this guide we’ll show you how to setup a swap file. We’ll walk you through all the command lines step by step to configure the swap file.
Creating swap file
The following guide works with both new and existing setups. Also it works with all Ubuntu server versions such as: v16.04, v18.04, v18.10 etc.
Note: We are assuming that you have your server up and running. Also we are assuming that you have basic knowledge on Linux and know how to access the server.
Step 1: Check if there is a swap file
Before we go ahead and configure a swap file let us first ensure that swap does not exist already.
To verify the swap space simply enter the following command and hit enter:
free -m
Now you should see something like this.
root@ubuntu-s-1vcpu-1gb-sgp1-01:~# free -m total used free shared buff/cache available Mem: 985 69 708 0 207 778 Swap: 0 0 0
If there is no swap space available then next to swap section you should see the value of 0.
Alternatively you can also run the swapon
command.
If you don’t get any output then it means there is no swap file configured on the server yet. It’s time to create one.
Step 2: Creating and sizing swap space
You can create 2 GB or 3GB or even 4GB swap space. It is up to you and there is no perfect number. But the general rule is 2x the available system memory. So if you VPS RAM is 1 GB then we recommend you to create 2 GB of swap file.
To create a 2 GB swap file enter the following command and hit enter
dd if=/dev/zero of=/swapfile count=2048 bs=1M
Note: replace 2048 with 3072 for 3GB and 4096 for 4GB swap space.
Now the above command should produce output similar to this.
2048+0 records in 2048+0 records out 2147483648 bytes (2.1 GB, 2.0 GiB) copied, 3.24693 s, 661 MB/s root@ubuntu-s-1vcpu-1gb-sgp1-01:~#
It means the swap file is created but is not yet activated.
Step 3: Activating swap file
Once the swap file is created the system will not recognize it automatically. We need to set permissions and tell the server how to format the file so it can be used as a valid swap file.
Now enter the following command and hit enter.
chmod 600 /swapfile
The above command updates the swap files permission. From now on it only allows read/write permission for root users and nobody else.
Next activate the swap file by running the following command
mkswap /swapfile
which will output the following result:
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes) no label, UUID=998cf2ae-006d-4c1a-b136-9409f78884e1
If the result is same as above then proceed to the next step which is turning the swap on.
Step 4: Enable swap on
Now the file is all ready. To enable the swap input the command below.
swapon /swapfile
Which will output nothing but the swap has been enabled. To check the swap file now input free -m
command and you should see the following result.
root@ubuntu-s-1vcpu-1gb-sgp1-01:~# free -m total used free shared buff/cache available Mem: 985 70 64 0 850 768 Swap: 2047 0 2047
It displays the total available and used swap space.
That’s it! We’ve successfully completed the swap file setup on your masternode server. Now there is one last setup.
Step 5: Enable swap on system reboot
Your masternode server will be running 24 x 7. However at some point you might be rebooting the server.
The thing is your server will not enable the swap file automatically during system reboot. So we need to ensure that swap will be turned on automatically during boot. To enable the swap on boot  just enter the following command.
sudo echo "/swapfile none swap sw 0 0" >> /etc/fstab
That’s it. We are done creating and configuring the swap file. You can now go ahead and compile the wallet or you can setup the 2nd masternode on the same server.
Reference: https://www.vultr.com/docs/setup-swap-file-on-linux