Swap partition to Swap file [duplicate]

I'm having Swap partition of 8 GB , how can i create a swap file from it ? Currently having three partitions root , home , swap . So if i create a swap file how do i create it and where do it get saved ? My RAM is about 4 GB.


Solution 1:

Generally, a swap partition is preferred to a swap file; however, you can use a swap file instead.
The swap file will be saved where you put it.
Assuming that your home partition has the most space it can be put there.

first make the swap file:

sudo dd if=/dev/zero of=/home/swapfile bs=4096 count=2M  
sudo mkswap /home/swapfile  
sudo swapon /home/swapfile

you now have an 8G swapfile.
to make it automatically active you need to edit /etc/fstab.
add the following liine:

/home/swapfile  none  swap  sw 0 0

if you no longer want the swap partition remove the line that looks like:

UUID=########## none  swap  sw  0  0