Use swap files to expand swap space on linux
Imagine a scenario in which we want to use swap partitions to expand swap space when our Linux system uses up swap space, however, in some cases, there are no available free partitions on the disk, so we cannot expand it.
Therefore, in this case, we can use swap files to increase the swap space.
The following describes how to use swap files to expand swap space in Linux:
Let's first check the size of the existing swap space/partition and use the command'Free-m'Or'Swapon-s'
My swap partition size is 2 GB and we will expand the swap space by 1 GB.
Step 1: Use the following dd command to create a 1 GB swap file
[root@linuxtechi ~]# dd if=/dev/zero of=/swap_file bs=1G count=1
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB) copied, 414.898 s, 2.6 MB/s
[root@linuxtechi ~]#
Replace 'according to your needs'Bs'And'Count'Size.
Step 2: Set the file exchange permission to 600
[root@linuxtechi ~]# chmod 600 /swap_file
Step 3: Enable swap_file in the SWAp zone with a file)
Use the mkswap command to enable the swap Zone
[root@linuxtechi ~]# mkswap /swap_file
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=f7b3ae59-c09a-4dc2-ba4d-c02abb7db33b
[root@linuxtechi ~]#
Step 4: Add swap file entries to the fstab file
Add the following entries to the fstab file so that the files can be used after each restart.
/swap_file swap swap defaults 0 0
Step 5: Use the command 'mkswap on' to enable the swap file.
[root@linuxtechi ~]# swapon /swap_file
[root@linuxtechi ~]#
Step 6: view the swap space now
Note: To avoid exceptions, use the swapoff command as follows to disable it. Use the swapon command as shown in step 5 to re-enable the swap file.
[root@linuxtechi ~]# swapoff /swap_file
[root@linuxtechi ~]#
Please share your valuable comments or comment on this article.
Via: http://www.linuxtechi.com/extend-swap-space-using-swap-file-in-linux/
Author: Pradeep Kumar Translator: strugglingyouth Proofreader: wxy