How to create swap in Linux ?
How to create swap in Linux
CREATE SWAP PARTITION LINUX
We can create a 8 Gigabyte Swap Partition
—————————————–
sudo fallocate -l 8G /swapfile ls -lh /swapfile
Enabling the Swap File
————————-
sudo chmod 600 /swapfile ls -lh /swapfile
Now that our file is more secure, we can tell our system to set up the swap space by typing:
——————————————————————————————–
sudo mkswap /swapfile
Our file is now ready to be used as a swap space. We can enable this by typing:
——————————————————————————————–
sudo swapon /swapfile
We can verify that the procedure was successful by checking whether our system reports swap space now:
——————————————————————————————–
sudo swapon -s
We have a new swap file here. We can use the free utility again to corroborate our findings:
——————————————————————————————–
free -m
Make the Swap File Permanent
—————————–
Edit the file with root privileges in your text editor:
sudo vim /etc/fstab
At the bottom of the file, you need to add a line that will tell the operating system to automatically use the file you created:
For ubuntu
swapfile none swap sw 0 0
For Centos
swapfile swap swap defaults 0 0
Save and close the file when you are finished.
cat /proc/sys/vm/swappiness
it should be 30
sudo sysctl vm.swappiness=10
vm.swappiness = 10
How to remove swap memory ?
swapoff -v /swapfile rm -rf swapfile
Script to create swap memory
#!/bin/bash echo " Enter the amount of ram to be created with G example - you need to create ram of 2GB then just type 2G and press enter" read ram sudo fallocate -l $ram /swapfile ls -lh /swapfile sudo chmod 600 /swapfile ls -lh /swapfile sudo mkswap /swapfile sudo swapon /swapfile sudo swapon -s free -m sudo echo "swapfile none swap sw 0 0" | tee >> /etc/fstab cat /etc/fstab cat /proc/sys/vm/swappiness sudo sysctl vm.swappiness=10