Follow these steps to add 1GB of swap to your server. If you want to add 2GB instead of 1 GB, replace 1G
with 2G
.
sudo fallocate -l 1G /swapfile
faillocate
is not installed or if you get an error message saying fallocate failed: Operation not supported
then you can use the following command to create the swap file:sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
sudo chmod 600 /swapfile
mkswap
utility to set up the file as Linux swap area:sudo mkswap /swapfile
sudo swapon /swapfile
/etc/fstab
file and append the following line:/swapfile swap swap defaults 0 0
swapon
or the [free](https://linuxize.com/post/free-command-in-linux/)
command as shown below:sudo swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 1024M 507.4M -1
sudo free -h
total used free shared buff/cache available
Mem: 488M 158M 83M 2.3M 246M 217M
Swap: 1.0G 506M 517M
Swappiness is a Linux kernel property that defines how often the system will use the swap space. Swappiness can have a value between 0 and 100. A low value will make the kernel to try to avoid swapping whenever possible, while a higher value will make the kernel to use the swap space more aggressively.
The default swappiness value is 60. You can check the current swappiness value by typing the following command:
cat /proc/sys/vm/swappiness
#linux #linux swap file