How to make a swap-file for Ubuntu

Swap file in Ubuntu Linux is important for computer operation, as it temporarily holds inactive programs in disk “memory”, giving physical memory priority to the currently running program. Ubuntu has a swap partition enabled by default, but if you want to add more swap space to your system, you can do without the danger of destroying the hard disk partitions.

How to create a swap file:

Run this command

sudo dd if=/dev/zero of=/mnt/disk.swap bs=1M count=2048
sudo chmod 600 /mnt/disk.swap

count=2048 means that we are to make a 2Gb swapfile. We also chmod 600 for security purposes.

“Format” the swap file

sudo mkswap /mnt/disk.swap

Activate the swap file

sudo swapon /mnt/disk.swap

Run this command to make changes

gksudo gedit /etc/fstab

Insert the following to add the swap file for permanent system use

/mnt/disk.swap none swap sw 0 0

Restart your computer for best results.

Undoing swapfile creation:

Execute this command

gksudo gedit /etc/fstab

Remove the line

/mnt/disk.swap none swap sw 0 0

Turn off swap file and remove it

sudo swapoff /mnt/disk.swap && sudo rm /mnt/disk.swap

Leave a Reply

Your email address will not be published. Required fields are marked *