<\/span><\/h2>\nBefore we create swap space, will check if any swap file or partition has already been set on the server. We are allowed to have more than one swap, but one swap should be more than enough in most cases.<\/p>\n
With the following command we can check if there is a swap file or partition already configured on the server:<\/p>\n
sudo swapon --show<\/pre>\nIf there is no output, it means that your server does not currently have swap set up.<\/p>\n
To make sure that your server is not using swap you can use the ‘free’<\/strong> command:<\/p>\nfree -h<\/pre>\nThe output should be similar to this:<\/p>\n
total used free shared buff\/cache available\r\nMem: 985M 228M 579M 5.5M 177M 618M\r\nSwap: 0B 0B 0B\r\n<\/pre>\nWe can see from the output that “Swap” row is zero which means that there is no active swap on the server. However, in our example, we will create a swap file that will be located on a partition that already exists.<\/p>\n
<\/span>2. Create Swap<\/span><\/h2>\nUsually, the allocated space for swap is used as a separate partition. Before we create a swap file it is best to check the disk usage:<\/p>\n
df -h<\/pre>\nThe output should be similar to this:<\/p>\n
Filesystem Size Used Avail Use% Mounted on\r\nudev 463M 0 463M 0% \/dev\r\ntmpfs 99M 3.7M 95M 4% \/run\r\n\/dev\/vda1 19G 2.2G 16G 13% \/\r\ntmpfs 493M 0 493M 0% \/dev\/shm\r\ntmpfs 5.0M 0 5.0M 0% \/run\/lock\r\ntmpfs 493M 0 493M 0% \/sys\/fs\/cgroup\r\ntmpfs 99M 0 99M 0% \/run\/user\/0\r\n<\/pre>\nWe can see from the output that on our server we have 16 GB available space (2.2 GB used) which is enough to create a swap file.<\/p>\n
Now that we know that we have storage space available, we will create a file called swap_file that will be 2GB in size and will be located in the \/mnt directory. By using the command below we will create the actual swap file:<\/p>\n
sudo fallocate -l 2G \/mnt\/swap_file<\/pre>\nWe can also use the dd command in case we do not have\u00a0fallocate installed on the server:<\/p>\n
sudo dd if=\/dev\/zero of=\/mnt\/swap_file bs=1024 count=2097152<\/pre>\nWhere bs=1024 means to allocate 1024 bytes at a time for reading and writing count = 2048 means to allocate 1024 bytes 2048 times, creating a 2GB file.<\/p>\n
When the swap file is created, we can check if the correct size of the space is being reserved:<\/p>\n
ls -lh \/mnt\/swap_file<\/pre>\nThe output should be similar to this:<\/p>\n
-rw-r--r-- 1 root root 2.0G Nov 24 10:32 \/mnt\/swap_file<\/pre>\nFrom the output result, we can see that the swap file was created with 2GB of storage.<\/p>\n
<\/span>3. Enable Swap<\/span><\/h2>\nNow that we have an available swap file, we need to use it and turn into actual swap space.<\/p>\n
The first step will be to lock the permissions of swap_file so that it can only be read by users with root privileges. We can do this by typing:<\/p>\n
sudo chmod 600 \/mnt\/swap_file<\/pre>\nNow list the file so that we can verify its permissions.<\/p>\n
ls -lh \/mnt\/swap_file<\/pre>\nOutput:<\/p>\n
-rw------- 1 root root 2.0G Nov 24 10:33 \/mnt\/swap_file<\/pre>\nFrom the output above, we can see that only the root user has the read and write flags enabled.<\/p>\n
The next step is to mark the file as swap.<\/p>\n
sudo mkswap \/mnt\/swap_file<\/pre>\nOutput:<\/p>\n
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)\r\nno label, UUID=c569dd4f-ffd9-4192-93dd-f6a470025d76<\/pre>\nAfter we mark the file, we can activate the swap file and allow our server to start using it:<\/p>\n
sudo swapon \/mnt\/swap_file<\/pre>\nWe can check if the swap file is available with the following command:<\/p>\n
sudo swapon -s<\/pre>\nOutput:<\/p>\n
Filename Type Size Used Priority\r\n\/mnt\/swap_file file 2097148 0 -2\r\n<\/pre>\nWe can again check the output of the ‘free’ command to confirm:<\/p>\n
free -h<\/pre>\nOutput:<\/p>\n
total used free shared buff\/cache available\r\nMem: 985M 228M 64M 5.5M 691M 608M\r\nSwap: 2.0G 0B 2.0G\r\n<\/pre>\nFrom the above results, we can see that the swap was successfully set up.<\/p>\n