edit
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
在开始之前,我们可以检查系统是否已经有可用的交换空间。
|
||||
|
||||
```bash
|
||||
``` bash
|
||||
sudo swapon --show
|
||||
```
|
||||
|
||||
@@ -19,7 +19,7 @@ NAME TYPE SIZE USED PRIO
|
||||
|
||||
你可以使用 `free` 来验证是否存在活动交换空间:
|
||||
|
||||
```bash
|
||||
``` bash
|
||||
free -h
|
||||
```
|
||||
|
||||
@@ -47,13 +47,13 @@ Swap: 0B 0B 0B
|
||||
|
||||
我们将在本指南中创建一个 8G 的交换文件,请根据自己服务器的需要进行调整:
|
||||
|
||||
```bash
|
||||
``` bash
|
||||
sudo fallocate -l 8G /swapfile
|
||||
```
|
||||
|
||||
验证一下是否预留了正确的空间:
|
||||
|
||||
```bash
|
||||
``` bash
|
||||
ls -lh /swapfile
|
||||
```
|
||||
|
||||
@@ -67,13 +67,13 @@ ls -lh /swapfile
|
||||
|
||||
这样,普通用户就无法访问该文件,否则会产生重大安全隐患。
|
||||
|
||||
```bash
|
||||
``` bash
|
||||
sudo chmod 600 /swapfile
|
||||
```
|
||||
|
||||
验证权限更改:
|
||||
|
||||
```bash
|
||||
``` bash
|
||||
ls -lh /swapfile
|
||||
```
|
||||
|
||||
@@ -83,7 +83,7 @@ ls -lh /swapfile
|
||||
|
||||
现在,我们可以输入以下命令将刚刚创建的文件标记为交换文件:
|
||||
|
||||
```bash
|
||||
``` bash
|
||||
sudo mkswap /swapfile
|
||||
```
|
||||
|
||||
@@ -94,7 +94,7 @@ no label, UUID=6e965805-2ab9-450f-aed6-577e74089dbf
|
||||
|
||||
标记交换文件后,我们可以启用交换文件,让我们启用它:
|
||||
|
||||
```bash
|
||||
``` bash
|
||||
sudo swapon /swapfile
|
||||
```
|
||||
|
||||
@@ -113,13 +113,13 @@ NAME TYPE SIZE USED PRIO
|
||||
|
||||
备份 `/etc/fstab` 文件,以防出错:
|
||||
|
||||
```bash
|
||||
``` bash
|
||||
sudo cp /etc/fstab /etc/fstab.bak
|
||||
```
|
||||
|
||||
将 swap 记录添加到 `/etc/fstab` 文件末尾:
|
||||
|
||||
```bash
|
||||
``` bash
|
||||
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
|
||||
```
|
||||
|
||||
@@ -139,7 +139,7 @@ echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
|
||||
|
||||
我们可以通过以下命令来查看当前的 `swappiness` 值:
|
||||
|
||||
```bash
|
||||
``` bash
|
||||
cat /proc/sys/vm/swappiness
|
||||
```
|
||||
|
||||
@@ -153,7 +153,7 @@ cat /proc/sys/vm/swappiness
|
||||
|
||||
例如,要将 `swappiness` 参数设置为 10,我们可以输入:
|
||||
|
||||
```bash
|
||||
``` bash
|
||||
sudo sysctl vm.swappiness=10
|
||||
```
|
||||
|
||||
@@ -163,7 +163,7 @@ vm.swappiness = 10
|
||||
|
||||
以上设置仅在本次会话有效,因此我们可以在 `/etc/sysctl.conf` 文件中添加这一行,在重启时自动设置该值:
|
||||
|
||||
```bash
|
||||
``` bash
|
||||
sudo nano /etc/sysctl.conf
|
||||
```
|
||||
|
||||
@@ -181,7 +181,7 @@ vm.swappiness=10
|
||||
|
||||
你可以通过再次查询 `proc` 文件系统来查看当前值:
|
||||
|
||||
```bash
|
||||
``` bash
|
||||
cat /proc/sys/vm/vfs_cache_pressure
|
||||
```
|
||||
|
||||
@@ -193,7 +193,7 @@ cat /proc/sys/vm/vfs_cache_pressure
|
||||
|
||||
我们可以将其设置为更保守的值,比如 50,只需输入:
|
||||
|
||||
```bash
|
||||
``` bash
|
||||
sudo sysctl vm.vfs_cache_pressure=50
|
||||
```
|
||||
|
||||
@@ -203,7 +203,7 @@ vm.vfs_cache_pressure = 50
|
||||
|
||||
同样,这只对当前会话有效。我们可以将其添加到配置文件中进行更改,就像刚才的设置一样:
|
||||
|
||||
```bash
|
||||
``` bash
|
||||
sudo nano /etc/sysctl.conf
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user