Abdelqader Blog

Abdelqader Blog

Make swap file on linux

There is a service you can use it in linux, swap file used for supporting RAM by creating file on Harddisk drive make it work same as RAM function.

I have used it to fix some services issues, like mysql was keeping stop when RAM have no enough space and that made my websites stop working till i restart mysql service.

first you need to make file to use it as swap file, and here we will create it on root named “swap.dat” by using dd function. this function have many usages, but right now we just use it to create file with a specific size have zero content.

right now i will guide you to create a 512MB file,

dd if=/dev/zero of=/swap.dat bs=4k count=131072

bs means Block Size, then the equation here working like this 4KB * 131072 = 524288KB /1024 = 512MB

or

dd if=/dev/zero of=/swap.dat bs=512M

then to init swap file

mkswap /swap.dat
swapon /swap.dat

to check the memory details including swap file

free -k

For keep swap file enable at bootup, you have to type this command to add your swap file line in fstab file

echo '/swap.dat swap swap defaults 0 0' | sudo tee -a /etc/fstab
Abdelqader Blog

Post a comment