Setup of Swap File on Dedicated Host

There can be times when there is a need for increasing the responsiveness of the server for the prevention of out of memory issues. Here the out of memory issues can happen when the application is running the Linux Dedicated Server and it starts by consuming a huge amount of memory.

Here the Swap file is designed as the virtual memory, this will be using the harddrive in storage of the data which cannot be held in the RAM. Here it will be shown to you on how to create the swap file this should be working under Ubuntu, CentOS and the Debian.

Step 1 - Verifying that Swap is not Existing

For the prevention of any issue in Linux Dedicated Server, you will be required to run the following command for verifying that a swap space is currently not active:

Free -m

Once the command has run you will be able to see that something similar is there according to the output.

total used free shared buffers cached
Mem: 1620 11637 124 12 22 2120
-/+ buffers/cache: 441 3541
Swap: 0 0 0

If you are able to see the value as 0 in the Swap section then you will be able to proceed to next step.

Also you will be able to run the following command in Linux Dedicated Server for checking if there is any configured swap file:

swapon -s

If there is no output from the swapon then you can proceed to the next step


Step 2 - Creation of Swap File

Here you will be required to choose a location for the file. Here it will be stored at the root of the server web space. This will be creating a 2GB swap file by running of the following command:

dd if=/dev/zero of=/swapfile count=2048 bs=1M

Here the dd command will be producing the output which will be in similar format to:
2345+0 records in
2345+0 records out

2167482645 bytes (2.1 GB) which is copied 10.5342 s , 214MB/s

Now you need to verify that the file which is located at the root of Linux Dedicated Server by running:
ls / | grep swapfile

Now you can proceed if you are able to see the swap file.


Step 3 - Activation of the Swap File

Here the Swap file will not be recognized automatically. You will be required to tell the server and how to format the file and then enable it so that it can be used as a valid swap file. For security measure one needs to update the swapfile permissions for allowing the R/W for the root and for no other users.
Run: chmod /swapfile

Here the permission will be chnged and it can be verified for running the following command:
ls -lh /swapfile

You will be able to see the file that is displayed:
-rw------- 1 root root 2.0G Oct 2 18:47 /swapfile

Now you need to tell the server for setting up the swap file by running:
mkswap /swapfile

Once you have run it you will be able to see the following output:
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=ff3fc469-9c4b-4913-b653-ec53d6460d0e

If you get everything as it is shown above then you will be able to go to the next step.

Step 4 - Turning the Swap on

After the file is ready that can be used in swap, you will be required to enable it by running the following co


Once your file is ready to be used as swap, you need to enable it by running:
swapon /swapfile

You can verify that the swap file is active by running the free command again.
free -m

total used free shared buffers cached
Mem: 1840 1754 86 16 23 1519
-/+ buffers/cache: 210 1630
Swap: 2047 0 2047

If there is a Swap file which is showing other that 0 then it would mean that swap file has been successfully set up.