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'
Free-output-with-swap
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 the size of 'bs 'and 'Count' as needed.
Step 2: Set the file exchange permission to 600
[root@linuxtechi ~]# chmod 600/swap_file
Step 3: Use a file to enable the swap zone (swap_file)
Use the mkswap command to enable the swap Zone
[root@linuxtechi ~]# mkswap /swap_file
Setting up swapspace version 1, size =1048572KiB
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 00
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
This article was originally translated by LCTT and launched with the Linux honor in China
This article permanently updates the link address: