Swapon, fstab and swapfile on Linux

Resuorces: https://linuxhandbook.com/increase-swap-ubuntu, https://www.cyberciti.biz/faq/linux-add-a-swap-file-howto/, https://www.techwalla.com/articles/how-to-increase-virtual-memory-in-linux

First thing is see how much memory we have to play with:

Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/root       51100620 7922656  40566360  17% /
devtmpfs         1005632       0   1005632   0% /dev
tmpfs            1009004       4   1009000   1% /dev/shm
tmpfs            1009004  107136    901868  11% /run
tmpfs               5120       0      5120   0% /run/lock
tmpfs            1009004       0   1009004   0% /sys/fs/cgroup
tmpfs             201800       0    201800   0% /run/user/1000

How big is the current swapfile?

sudo swapon --show
NAME     TYPE      SIZE   USED PRIO
/dev/sdb partition 512M 175.6M   -2

To double that, to around a gig:

sudo dd if=/dev/zero of=/mnt/swapfile1 bs=1024 count=1048576

Permissions:

sudo chown root:root /mnt/swapfile1
sudo chmod 0600 /mnt/swapfile1

Setup Swap area:

sudo mkswap /mnt/swapfile1

Now enable it:

sudo swapon /mnt/swapfile1

Now update the fstab file so it loads on restart:

sudo vim /etc/fstab
# FROM
# swap was on /dev/sdb1 during installation
/dev/sdb	 none            swap    sw              0       0
# TO
/mnt/swapfile1	 none            swap    sw              0       0

Check it:

sudo swapon -s
sudo swapon -s
Filename				Type		Size	Used	Priority
/dev/sdb                               	partition	524284	162680	-2
/mnt/swapfile1                         	file    	1048572	0	-3

You can also turn one off:

sudo swapoff /mnt/swapfile1   

Update

I think fallocate is an easier way to do it: https://itsfoss.com/create-swap-file-linux/

Leave a Reply

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