- User ID
- 1
- Joined
- 7 Jan 2019
- Messages
- 861
- Reaction score
- 35
- Points
- 28
How to add Swap File
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.
How to adjust the swappiness value
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:
While the swappiness value of 60 is OK for most Linux systems, for production servers, you may need to set a lower value.
For example, to set the swappiness value to 10, you would run:
To make this parameter persistent across reboots append the following line to the /etc/sysctl.conf file:
The optimal swappiness value depends on your system workload and how the memory is being used. You should adjust this parameter in small increments to find an optimal value.
How to remove Swap File
If for any reason you want to deactivate and remove the swap file, follow these steps:
Notes
We know that using swap space instead of RAM (memory) can severely slow down the performance of Linux. So then, one might ask, since I have more than enough memory available, wouldn’t it better to remove swap space completely? The short answer is, No. There are performance benefits when a swap is enabled, even when you have more than enough RAM.
I hope you have learned how to create a swap file, activate and configure swap space on your Linux system
This tutorial was tested on Linux systems with Debian 8.11, Debian 9.5.0, Ubuntu 22.04 and CentOS 7, but it should work with any other Linux distribution
Feel free to post your question or comment down below
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.
- Create a file that will be used for swap:
Code:
root@cana:/# fallocate -l 1G /swapfile
Code:root@jerusalem:/# dd if=/dev/zero of=/swapfile bs=1024 count=1048576 1048576+0 records in 1048576+0 records out 1073741824 bytes (1.1 GB, 1.0 GiB) copied, 1.62013 s, 663 MB/s root@jerusalem:/#
- Only the root user should be able to write and read the swap file. To set the correct permissions type:
Code:
root@cana:/# chmod -v 600 /swapfile mode of ‘/swapfile’ changed from 0644 (rw-r--r--) to 0600 (rw-------) root@cana:/#
- Use the mkswap utility to set up the file as Linux swap area:
Code:
root@cana:/# mkswap /swapfile Setting up swapspace version 1, size = 1048572 KiB no label, UUID=485b787f-74ab-4f78-8e73-b10aaaf8c665 root@cana:/#
- Enable the swap with the following command: (the swap will be available instantly after you invoked this command):
Code:
root@cana:/# swapon /swapfile
Code:# <file system> <mount point> <type> <options> <dump> <pass> /swapfile swap swap defaults 0 0
- To verify that the swap is active, use either the swapon or the free command as shown below:
Code:
root@cana:/# swapon --show NAME TYPE SIZE USED PRIO /swapfile file 1024M 612K -1 root@cana:/#
Code:root@cana:/# free -h total used free shared buffers cached Mem: 5.8G 5.5G 388M 406M 143M 3.2G -/+ buffers/cache: 2.1G 3.7G Swap: 1.0G 612K 1.0G root@cana:/#
How to adjust the swappiness value
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:
Code:
root@cana:/# cat /proc/sys/vm/swappiness
60
root@cana:/#
While the swappiness value of 60 is OK for most Linux systems, for production servers, you may need to set a lower value.
For example, to set the swappiness value to 10, you would run:
Code:
root@cana:/# sysctl vm.swappiness=10
vm.swappiness = 10
root@cana:/#
To make this parameter persistent across reboots append the following line to the /etc/sysctl.conf file:
Code:
vm.swappiness=10
How to remove Swap File
If for any reason you want to deactivate and remove the swap file, follow these steps:
- First, deactivate the swap by typing:
Code:
# swapoff -v /swapfile
- Remove the swap file entry /swapfile swap swap defaults 0 0 from the /etc/fstab file.
- Finally, delete the actual swapfile file using the rm command:
Code:
# rm -fv /swapfile
Notes
We know that using swap space instead of RAM (memory) can severely slow down the performance of Linux. So then, one might ask, since I have more than enough memory available, wouldn’t it better to remove swap space completely? The short answer is, No. There are performance benefits when a swap is enabled, even when you have more than enough RAM.
I hope you have learned how to create a swap file, activate and configure swap space on your Linux system

Consider adjusting your server’s cache pressure as well:
vfs_cache_pressure – Controls the tendency of the kernel to reclaim the memory which is used for caching of directory and inode objects. (default = 100, recommend value 50 to 200)
swappiness – This control is used to define how aggressive the kernel will swap memory pages. Higher values will increase aggressiveness, lower values decrease the amount of swap. (default = 60, recommended values between 1 and 60) Remove your swap for 0 value, but usually not recommended in most cases.
To edit you can add or replace these lines in /etc/sysctl.conf file. For example, if you have low memory until you upgrade you can try something such as:
This will increase the cache pressure and that may seem somewhat counterproductive since caching is good for performance. However, too frequent swapping reduces your server’s overall performance significantly more. So not keeping as much cache in memory will help reduce swap/swapcache activity. Also, with vm.swappiness set to 10 or as low as 1, will reduce disk swapping.
On a healthy server with lots of available memory use the following:
This will decrease the cache pressure. Since caching is good for performance, we want to keep the cache in memory longer. Since the cache will grow larger we still want to reduce swapping so that it does not cause increased swap I/O.
To check current values using these commands use:
To enable these settings temporarily without rebooting use the following commands:
vfs_cache_pressure – Controls the tendency of the kernel to reclaim the memory which is used for caching of directory and inode objects. (default = 100, recommend value 50 to 200)
swappiness – This control is used to define how aggressive the kernel will swap memory pages. Higher values will increase aggressiveness, lower values decrease the amount of swap. (default = 60, recommended values between 1 and 60) Remove your swap for 0 value, but usually not recommended in most cases.
To edit you can add or replace these lines in /etc/sysctl.conf file. For example, if you have low memory until you upgrade you can try something such as:
Code:
vm.swappiness=10
vm.vfs_cache_pressure=200
On a healthy server with lots of available memory use the following:
Code:
vm.swappiness=10
vm.vfs_cache_pressure=50
To check current values using these commands use:
Code:
# cat /proc/sys/vm/swappiness
# cat /proc/sys/vm/vfs_cache_pressure
Code:
# sysctl vm.swappiness=10
# sysctl vm.vfs_cache_pressure=50
This tutorial was tested on Linux systems with Debian 8.11, Debian 9.5.0, Ubuntu 22.04 and CentOS 7, but it should work with any other Linux distribution

Feel free to post your question or comment down below

Last edited: