# 添加删除vps中的swap分区

**URL:** https://eijil.com/t/vps-swap/41
**Category:** 其它
**Created:** [December 31, 2020, 10:04am UTC](https://eijil.com/t/vps-swap/41 "2020-12-31T10:04:56Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![linuxren](https://eijil.com/user_avatar/eijil.com/linuxren/32/6_2.png) [@linuxren](https://eijil.com/u/linuxren)
#### Post date: [December 31, 2020, 10:04am UTC](https://eijil.com/t/vps-swap/41/1 "2020-12-31T10:04:56Z")

</div>

大部分vps都没有swap分区，下面的办法提供了增加一个2G swap分区的方式

**创建分区**

fallocate -l 2G /swapfile  
chmod 600 /swapfile  
mkswap /swapfile  
swapon /swapfile  
echo “/swapfile swap swap defaults 0 0” \>\> /etc/fstab

**删除分区**

swapoff -v /swapfile

---

<div class="post-metadata">

### Author: ![Tristan](https://eijil.com/user_avatar/eijil.com/tristan/32/986_2.png) [@Tristan](https://eijil.com/u/Tristan)
#### Post date: [November 12, 2024, 3:48am UTC](https://eijil.com/t/vps-swap/41/2 "2024-11-12T03:48:01Z")

</div>

首先，创建swap的基本步骤是:

### 创建swap文件:

```auto
sudo fallocate -l 4G /swapfile
# 或者用 dd 命令替代
sudo dd if=/dev/zero of=/swapfile bs=1G count=4

```

- fallocate 更快但部分文件系统可能不支持
- dd 命令更通用，bs是块大小，count是块数量

### 设置权限:

```auto
sudo chmod 600 /swapfile

```

- 出于安全考虑，只允许root读写

### 格式化为swap:

```auto
sudo mkswap /swapfile

```

- 将文件标记为swap格式

### 启用swap:

```auto
sudo swapon /swapfile

```

- 激活swap分区

### 设置开机自动挂载:

```auto
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

```

- 将配置写入fstab文件

### 这些命令中:

- 1-4是必须的
- 第5步是可选的，但建议添加以实现开机自动挂载

---

<div class="post-metadata">

### Author: ![Tristan](https://eijil.com/user_avatar/eijil.com/tristan/32/986_2.png) [@Tristan](https://eijil.com/u/Tristan)
#### Post date: [November 12, 2024, 3:49am UTC](https://eijil.com/t/vps-swap/41/3 "2024-11-12T03:49:23Z")

</div>

删除swap:

### 首先停用swap:

```auto
sudo swapoff /swapfile

```

- 这会让系统停止使用该swap
- 系统会将swap中的数据移回内存

### 从fstab中删除自动挂载项:

```auto
sudo vim /etc/fstab

```

### 找到并删除这一行:

```auto
/swapfile none swap sw 0 0

```

或者用sed命令:

```auto
sudo sed -i '/swapfile/d' /etc/fstab

```

### 删除swap文件:

```auto
sudo rm /swapfile

```

确认swap已被删除:

```auto
# 查看当前swap使用情况
free -h

# 或者
swapon --show

```

---

<div class="post-metadata">

### Author: ![Tristan](https://eijil.com/user_avatar/eijil.com/tristan/32/986_2.png) [@Tristan](https://eijil.com/u/Tristan)
#### Post date: [November 12, 2024, 3:52am UTC](https://eijil.com/t/vps-swap/41/4 "2024-11-12T03:52:32Z")

</div>

@Chatbot Translate this thread to English please!

---

<div class="post-metadata">

### Author: ![Chatbot](https://eijil.com/letter_avatar_proxy/v4/letter/c/8c91f0/32.png) [@Chatbot](https://eijil.com/u/Chatbot)
#### Post date: [November 12, 2024, 3:52am UTC](https://eijil.com/t/vps-swap/41/5 "2024-11-12T03:52:46Z")

</div>

Certainly! Here’s the translation of the thread:

* * *

**linuxren said:** Most VPS do not have a swap partition. The following method provides a way to add a 2GB swap partition.

**Creating the partition:**

```bash
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo "/swapfile swap swap defaults 0 0" >> /etc/fstab

```

**Deleting the partition:**

```bash
swapoff -v /swapfile

```

* * *

**Tristan said:** First, the basic steps to create a swap file are:

### Create a swap file:

```bash
sudo fallocate -l 4G /swapfile
# Or use the dd command as an alternative
sudo dd if=/dev/zero of=/swapfile bs=1G count=4

```

- `fallocate` is faster but may not be supported by some file systems.
- `dd` is more universal; `bs` is block size, and `count` is the number of blocks.

### Set permissions:

```bash
sudo chmod 600 /swapfile

```

- For security reasons, only root is allowed to read and write.

### Format as swap:

```bash
sudo mkswap /swapfile

```

- This marks the file as swap format.

### Enable swap:

```bash
sudo swapon /swapfile

```

- This activates the swap partition.

### Set to auto-mount at boot:

```bash
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

```

- This writes the configuration to the fstab file.

### These commands:

- Steps 1-4 are mandatory.
- Step 5 is optional but recommended to ensure auto-mount at boot.

* * *

**Tristan said:** To delete swap:

### First, disable swap:

```bash
sudo swapoff /swapfile

```

- This will stop the system from using the swap.
- The system will move data from the swap back to memory.

### Remove the auto-mount entry from fstab:

```bash
sudo vim /etc/fstab

```

### Find and delete this line:

```bash
/swapfile none swap sw 0 0

```

Or use the sed command:

```bash
sudo sed -i '/swapfile/d' /etc/fstab

```

### Delete the swap file:

```bash
sudo rm /swapfile

```

Confirm that the swap has been deleted:

```bash
# Check current swap usage
free -h

# Or
swapon --show

```

* * *

Let me know if you need anything else!
