I can't enable Swap space on CentOS 7

The problem with fallocate(1) is that it uses filesystem ioctls to make the allocation fast and effective, the disadvantage is that it does not physically allocate the space but swapon(2) syscall requires a real space. Reference : https://bugzilla.redhat.com/show_bug.cgi?id=1129205

I'd faced this issue earlier with my box too. So instead of using fallocate, I used dd as the link suggests

sudo dd if=/dev/zero of=/myswap count=4096 bs=1MiB

and moving ahead with chmod, mkswap & swapon commands. Bingo ! It worked.


Follow these steps, it works on DigitalOcean's droplets. I tested. Change the amount 4096 according to your need

yum install nano -y

sudo dd if=/dev/zero of=/swapfile count=4096 bs=1MiB
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

sudo nano /etc/fstab

add this line:

/swapfile   swap    swap    sw  0   0

run this command

sudo sysctl vm.swappiness=10

sudo nano /etc/sysctl.conf

add this line

vm.swappiness = 10
vm.vfs_cache_pressure = 50

To verify swap's size

swapon --summary
free -h

Tags:

Centos

Swap