Linux increases swapfile file size

If you've already created a swapfilefile, you can increase its size by following these steps:

  1. Disable swap space: swapoffDisable the swap partition or swapfilefile using the command, for example:
sudo swapoff /swapfile
  1. Adjust the file size: Use ddthe command to adjust swapfilethe size of the file, for example, to swapfileincrease the file size to 2GB, you can use the following command:
sudo dd if=/dev/zero of=/swapfile bs=1G count=2

In the above command, ifthe parameter specifies the input file, ofthe parameter specifies the output file, bsthe parameter specifies the block size, and countthe parameter specifies the number of blocks. You can adjust these parameters according to your needs.

  1. format swapfilefile: use mkswapthe command to format swapfilethe file, for example:
sudo mkswap /swapfile
  1. Enable swap space: swaponenable swap partition or swapfilefile using command, for example:
sudo swapon /swapfile
  1. Verify swap space: Use freethe command to verify that swap space is enabled and check the size of the swap space, for example:
free -h

In the above command output, Swapthe line will show swapfilethe size and usage of the file.

A word of caution, before resizing swapfilea file, make sure you have enough free space on your file system to accommodate the new swapfilefile size.

in addition

To see swapfilethe size of the file in the current system, you can use lsthe and ducommands to do so. Specific steps are as follows:

  1. Execute the following command to view a list of all swapfilefiles in the current system:
ls -lh /swapfile

If there are multiple files in your system swapfile, you can replace in the above command /swapfilewith the path of the file you want to view swapfile.

  1. Execute the following command to view swapfilethe file size:
du -h /swapfile

This will display swapfilethe total size of the files. If you want to see swapfilehow the file is allocated, you can use du -h --apparent-size /swapfilethe command.

Note that if your system does not have swap space enabled, swapfilethe file will not exist. You can use swapon -sthe command to verify that swap space is enabled.

Guess you like

Origin blog.csdn.net/qq_22815083/article/details/131060331