Post

Linux mount swap partition to a file

Linux mount swap partition to a file

Linux mount swap partition to a file

When your system starts swapping heavily and you don’t have a dedicated swap partition — which happens a lot with VMs set up in a hurry — a swap file is the quick fix. The dd command creates a file of the right size, mkswap formats it, and swapon activates it. Adding it to /etc/fstab makes it survive reboots.

Watch Explanation on YouTube

How to create Swap File on Linux

Link URL : https://youtu.be/43Inh_045_8

  • Create a file and give it same space as you would to a swap partition
1
2
3
4
$ sudo dd if=/dev/zero of=/swapPartitionAsFile bs=1M count=2048

$ # reduce file permissions
$ sudo chmod 0600 /swapPartitionAsFile
  • Use make swap to set file as swap
1
$ sudo mkswap /swapPartitionAsFile
  • Enable the file as swap using swapon
1
$ sudo swapon /swapPartitionAsFile
  • Add the file to be auto-mounted
1
2
3
$ sudo vi /etc/fstab

/swapPartitionAsFile swap swap defaults 0 0
This post is licensed under CC BY 4.0 by the author.