Skip to content
Blog How to use a swapfile to increase the swap space of your linux system (ubuntu, centos, debian, etc..)

How to use a swapfile to increase the swap space of your linux system (ubuntu, centos, debian, etc..)

You you have alloted a low space while installing, or you have upgraded your RAM, or you think you need some more swap space then what will you do. You can go to the partion table make make changes, but this can cause many problems.
Instead you can just make a file and assign it to the swap.
Just follow these steps for that.

1. Determine the size of the new swap file in megabytes and multiply by 1024 to determine the number of blocks. For example, the block size of a 1024 MB swap file is 1048576.
2. Lets make a zero’d file for the swap using dd. At a shell prompt as root, type the following command with count being equal to the desired block size:


dd if=/dev/zero of=/swapfile bs=1024 count=1048576

2. Make the file as a swap file. Setup the swap file with the command:


mkswp /swapfile


3.
Activate swapfile. To enable the swap file immediately but not automatically at boot time:


swapon /swapfile


4.
Verify that our swapfile has been activated


swapon -s

or by viewing the output of the command

cat /proc/swaps

or

free

5. To enable it at boot time, edit /etc/fstab to include the following entry:
If you want to have this a permanent solution, then adding this entry to fstab would probally be a better idea,for that open up /etc/fstab in your favourite editor and add the following line.


/swapfile swap swap defaults 0 0

The next time the system boots, it enables the new swap file.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.