This question already has an answer here:

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.

marked as duplicate by muru, Rinzwind, g_p, Charles Green, Pilot6 Jul 10 '17 at 14:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • You don't create really create a Swapfile from a swap partition. Do you mean you want to both remove the swap Partition to add the space it occupies space to another partition with regular filesystem and also create a swapfile? What do you hope to gain? – gmatht Jun 14 '18 at 13:55

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
  • So can i use that swap file with other distro ? Life swap partition can be used ? – Verkiya Jul 10 '17 at 7:57
  • it will only be available to the system it is installed on. you will have to repeat the process for other installations – ravery Jul 10 '17 at 8:04
  • So , is swap partition a better option than swap file in case of multiple distros ? – Verkiya Jul 10 '17 at 8:15
  • yes, then all distros can use the same swap. a note of caution. if you hibernate one distro. opening the swap with another may corrupt the hibernated one because the swap will be flushed – ravery Jul 10 '17 at 8:18
  • 1
    @Neinstein -- I thnk both were valid when i posted the answer, but I edited it to the more common command as you noted. – ravery Aug 22 '18 at 21:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.