Tutorial on closing/creating/adding Swap swap partition in Linux

888u

Last update at :2024-01-01,Edit by888u

The Swap swap partition in Linux is the virtual memory in Windows. It virtualizes the physical hard disk space of the Linux server into memory to solve the problem of insufficient memory. The methods for creating and adding Swap partitions are the same for different Linux distributions (CentOS/Ubuntu/Debian). Below, this site will share how to determine the size of the Swap partition and how to create a Swap partition.

1. Linux Swap partition size selection

The size of the Linux Swap partition is related to the physical memory size of your server itself. The larger the memory, the larger the Swap partition should be set. The relationship between the two is as follows.

Physical memory swap partition (SWAP)

2. Linux Swap partition Create/add Swap partition

1. View the current Linux Swap partition

The command is as follows:

free -m

2. Close the Swap partition

If we need to add a Swap partition, we must first close the Swap partition. The command is as follows:

swapoff -a

If the Swap partition cannot be closed, it may be because the current physical memory usage + the current usage of the Swap partition has exceeded the total size of the physical memory. We can restart the server first and close the Swap partition when the machine first starts.

3. Create Swap partition file

The creation command is as follows, where /var/swapfile is the file location and bs*count is the file size. For example, the following command will create a 4G file:

dd if=/dev/zero of=/var/swapfile bs=1M count=4096

4. Create Swap file system

Format the file just now into a Swap partition file. The command is as follows:

mkswap /var/swapfile

5. Enable Swap partition

Open the Swap partition based on the new Swap partition file:

swapon /var/swapfile

6. View the current Linux partition

Check whether the Swap partition is successfully created/added:

free -m

As shown in the figure below, a 4GB Swap partition space has been successfully created:

7. Setting up startup

Add the following code to the /etc/fstab file to allow the Linux swap partition to be automatically mounted at boot:

/var/swapfile swap swap defaults 0 0

Recommended site searches: registration-free space, Hong Kong vps host rental, free virtual host application, website IP query, proxy server IP, Japanese proxy server IP, domain name registration website, which Hong Kong space is better, virtual host rental, private server rental ,

Tutorial on closing/creating/adding Swap swap partition in Linux

All copyrights belong to 888u unless special state
取消
微信二维码
微信二维码
支付宝二维码