WHAT IS A SWAP FILE?
Whenever the system’s memory is low, a swap file is a system file that creates temporary storage space on a solid-state drive or hard disk. By switching some RAM storage from an idle program, the file makes room in memory for other programs. Let’s create a swap file On Debian 11.
UPDATE AND UPGRADE
Before start anything let’s update and upgrade our system by running the following command.
sudo apt update && sudo apt upgrade -y
CHECK for existing SWAP STATUS
Most of the case by default swap file does not exist. It’s always to good idea to make sure if there is one exist.
sudo swapon --show
Check Drive Capacity
Check the drive capacity to make sure we have enough space to create swap file.
df -h
Allocate Swap File Space on Drive
Run the following command to create 1GB swap file. Genereal rule of thumb is if you have 2GB or less of physical ram. Multiply by the 2. If physical ram is more then 2GB then the divide it by the 2.
sudo fallocate -l 1G /swapfile
Change Permissions of Swap File
After creating the swap file change the permission. Use the following command to execute it.
sudo chmod 600 /swapfile
Check Swap File Size and Permissions
After setting up the the permission let’s check the swap file size and the permission status.
ls -lh /swapfile
Make Swap File
Now assign the swap file as the swap file
sudo mkswap /swapfile
Enable Swap
To enable swap at start up run the following command.
sudo swapon /swapfile
Open fstab for Editing
To make the swap file permanent we need to edit fstab file.
sudo nano /etc/fstab
If above command not working this means nano is not installed. To install nano run this command then try again.
sudo apt-get -y install nano
Edit the fstab file
Add these line in fstab file and save.
/swapfile none swap defaults 0 0
Check Swap Parameters
Check swappiness and vfs cache pressure value and take note of it.
cat /proc/sys/vm/swappiness
cat /proc/sys/vm/vfs_cache_pressure
Change Swap Parameters
We need to change the swappiness and vfs cache pressure using below commands.
sudo sysctl vm.swappiness=10
sudo sysctl vm.vfs_cache_pressure=50
Open sysctl.conf for Editing
To make previous changes permanent let’s edit the sysctl.conf file.
sudo nano /etc/sysctl.conf
Editing the sysctl.conf
Add these lines at the end of the document (/etc/sysctl.conf) then save the file.
vm.swappiness=10
vm.vfs_cache_pressure=50
Check Swap File
Now run the command to check the swap file.
sudo swapon --show
Reboot the server
Now restart your server or instances now. To reboot the server you will need root user. Follow the command below.
Sudo su
reboot
Conclusion
Now you know how to create a swap file On Debian 11, activate, and configure the swap space. Hope this will help you. If you want Ubuntu swap file Click to read the article.
Thanks for reading.