How to use swap files to expand swap space on linux
This article is about how to use swap files to expand swap space on linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Imagine a scenario where our Linux system runs out of swap space, in which case we want to use swap partitions to expand swap space, but in some cases there are no free partitions available on disk, so we can't expand it.
Therefore, in this case, we can use the swap file to increase the swap space.
Here is how to expand swap space on Linux using swap files
Let's first check the size of the existing swap space / partition, using the command 'free-m' or 'swapon-s'
My swap partition size is 2 GB, and we will extend the swap space to 1GB.
Step 1: use the following dd command to create a 1GB exchange file [root@linuxtechi ~] # dd if=/dev/zero of=/swap_file bs=1G count=11+0 records in1+0 records out1073741824 bytes (1.1 GB) copied, 414.898 s, 2.6 MB/s [root@linuxtechi ~] #
Replace the size of 'bs'' and 'count'' as you need.
Step 2: set the exchange file permissions to 600 [root@linuxtechi ~] # chmod 600 / swap_file. Step 3: open the exchange area (swap_file) with files.
Open the exchange area with the mkswap command
[root@linuxtechi ~] # mkswap / swap_fileSetting up swapspace version 1, size = 1048572 KiBno label, UUID=f7b3ae59-c09a-4dc2-ba4d-c02abb7db33b [root@linuxtechi ~] # step 4: add a swap file entry to the fstab file
Add the following entry to the fstab file so that the exchange file can continue to use each other after each restart.
/ swap_file swap swap defaults 0 0
Step 5: enable the swap file with the command 'mkswap on'. [root@linuxtechi ~] # swapon / swap_ file [root @ linuxtechi ~] # step 6: now check the swap space
Note: to avoid unexpected situations, turn it off using the swapoff command as shown below, and re-enable the swap file using the swapon command shown in step 5 only if you need it.
[root@linuxtechi] # swapoff / swap_ file [root @ linuxtechi ~] # Thank you for reading! This is the end of this article on "how to use swap files to expand swap space on linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!