Use swap files to expand swap space on linux

Source: Internet
Author: User

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
 
 
  1. [root@linuxtechi ~]# dd if=/dev/zero of=/swap_file bs=1G count=1
  2. 1+0 records in
  3. 1+0 records out
  4. 1073741824 bytes (1.1 GB) copied, 414.898 s, 2.6 MB/s
  5. [root@linuxtechi ~]#

Replace 'according to your needs'Bs'And'Count'Size.

Step 2: Set the file exchange permission to 600
 
 
  1. [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

 
 
  1. [root@linuxtechi ~]# mkswap /swap_file
  2. Setting up swapspace version 1, size = 1048572 KiB
  3. no label, UUID=f7b3ae59-c09a-4dc2-ba4d-c02abb7db33b
  4. [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.

 
 
  1. /swap_file swap swap defaults 0 0

Step 5: Use the command 'mkswap on' to enable the swap file.
 
 
  1. [root@linuxtechi ~]# swapon /swap_file
  2. [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.

 
 
  1. [root@linuxtechi ~]# swapoff /swap_file
  2. [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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.