swapon has no effect, /proc/swaps remains empty
Matthew Martinez
My Ubuntu server has no more than 512 MB of memory. It is not enough for running Node.js + MongoDB.
Hence I decided to add a swap file:
# dd if=/dev/zero of=/root/myswapfile bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 4.69263 s, 229 MB/s
# mkswap /root/myswapfile
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=3c64da32-d0b4-49e5-92db-fba961b8ae28
# swapon /root/myswapfileNo error message => successful!
The new swap file should now be listed, but it is not:
# swapon -s
Filename Type Size Used Priority(empty list)
How can I make it work? What might be the reason for the missing swap?
124 Answers
First, erase your original swapfile:
sudo rm /myswapfileNext, run the following commands one by one:
sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
ls -lh /swapfileThe output should be similar to the following:
-rw------- 1 root root 1.0G Aug 3 18:20 /swapfileNow, run the following commands:
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon -sNow, edit /etc/fstab to make the changes permanent:
sudo nano /etc/fstaband add the following line to the end of the file:
/swapfile none swap sw 0 0Press CTRL + o and then press ENTER to save the file.
Press CTRL + x to exit nano.
source:
If that still doesn't work, you may want to check swappiness:
cat /proc/sys/vm/swappinessThis command should return a number. The default should be 60.
If no output, run the following commands one by one:
sudo sysctl vm.swappiness=60
echo "vm.swappiness = 60" | tee -a /etc/sysctl.confThe -a is important after tee or you will overwrite the entire file!!!
Use fdisk to create a swap. Add a new partition, select Linux swap, write the changes.mkswap /dev/sd?You need to add it to your /etc/fstab/. Change XXX to the swap UUID.
UUID=XXX none swap sw 0 0Run swapon -a to make it available from /etc/fstab.
You swap file should be in /swapfile with 600 rights.
#moving file and changing rights
sudo mv /root/myswapfile /myswapfile
sudo chmod 600 /myswapfile
sudo mkswap /myswapfileadd swap file. I think you forget this command
sudo swapon /myswapfile
#check swap is working
sudo swapon -s 5 you could do this:
sudo umount -a
gedit /etc/fstaband write in where swap is this:
/proc/swap proc default errors=noremount 1and then this:
sudo mount -aand then run gpart and set the swap drive as swap, and mount it on the root dev drive, and it might work fine this way.
3