In this example we will create 2GB swap file on external disk /media/hhd/
dd if=/dev/zero of=/media/hhd/2GB.swap bs=2048 count=1048576 status=progress
This command will create a file of size count*bs bytes, which in the above case will be 2GB
This process take a lot of minutes!
Format the swap file with the command:
mkswap /media/hhd/2GB.swap
add the file to the system, enable the swap file immediately but not automatically at boot time:
swapon /media/hhd/2GB.swap
After adding the file to the system, add this line in /etc/fstab (to edit /etc/fstab run this command nano /etc/fstab). The next time the system boots, it enables the new swap file:
/media/hhd/2GB.swap none swap sw 0 0
After adding the new swap file and enabling it, verify it is enabled by viewing the output of the command cat /proc/swaps or free
The Linux kernel provides a tweakable setting that controls how often the swap file is used, called swappiness. A swappiness setting of zero means that the disk will be avoided unless absolutely necessary (you run out of memory), while a swappiness setting of 100 means that programs will be swapped to disk almost instantly. Most Linux system comes with a default of 60, meaning that the swap file will be used fairly often if the memory usage is around half of my RAM. You can check your own system's swappiness value by running:
cat /proc/sys/vm/swappiness
I'd like to turn that down to 10 or 15. The swap file will then only be used when my RAM usage is around 80 or 90 percent.
Reboot the server and after rebooting chmod the file to take effect.
chmod 600 /media/hhd/2GB.swap
We can check the swap file by using
swapon -s
You successfully created a swap file in Linux.
Format the swap file with the command:
mkswap /media/hhd/2GB.swap
add the file to the system, enable the swap file immediately but not automatically at boot time:
swapon /media/hhd/2GB.swap
After adding the file to the system, add this line in /etc/fstab (to edit /etc/fstab run this command nano /etc/fstab). The next time the system boots, it enables the new swap file:
/media/hhd/2GB.swap none swap sw 0 0
After adding the new swap file and enabling it, verify it is enabled by viewing the output of the command cat /proc/swaps or free
The Linux kernel provides a tweakable setting that controls how often the swap file is used, called swappiness. A swappiness setting of zero means that the disk will be avoided unless absolutely necessary (you run out of memory), while a swappiness setting of 100 means that programs will be swapped to disk almost instantly. Most Linux system comes with a default of 60, meaning that the swap file will be used fairly often if the memory usage is around half of my RAM. You can check your own system's swappiness value by running:
cat /proc/sys/vm/swappiness
I'd like to turn that down to 10 or 15. The swap file will then only be used when my RAM usage is around 80 or 90 percent.
You can change the value while your system is still running with:
sysctl vm.swappiness=10
You can also clear your swap by running swapoff -a and then swapon -a as root instead of rebooting to achieve the same effect.
Reboot the server and after rebooting chmod the file to take effect.
chmod 600 /media/hhd/2GB.swap
We can check the swap file by using
swapon -s
You successfully created a swap file in Linux.
0 comments:
Post a Comment