Centos系统如何临时关闭/永久关闭交换分区swap虚拟内存?

为什么要关闭swap虚拟内存?

1:swap的用途?

swap 分区就是交换分区,(windows平台叫虚拟内存)

在物理内存不够用时,

操作系统会从物理内存中把部分暂时不被使用的数据转移到交换分区,

从而为当前运行的程序留出足够的物理内存空间

2:为什么要关闭swap?

swap启用后,在使用磁盘空间和内存交换数据时,

性能表现会较差,会减慢程序执行的速度

有的软件的设计师不想使用交换,

例如:kubelet 在 1.8 版本以后强制要求 swap 必须关闭

否则会报错:

Running with swap on is not supported, please disable swap! or set --fail-swap-on flag to false

或者kubeadm init时会报错:

[ERROR Swap]: running with swap on is not supported. Please disable swap

查看swapon/swapoff所属的rpm包

[root@centos8 ~]# whereis swapoff
swapoff: /usr/sbin/swapoff /usr/share/man/man8/swapoff.8.gz /usr/share/man/man2/swapoff.2.gz
[root@centos8 ~]# whereis swapon
swapon: /usr/sbin/swapon /usr/share/man/man8/swapon.8.gz /usr/share/man/man2/swapon.2.gz
[root@centos8 ~]# rpm -qf /usr/sbin/swapon
util-linux-2.32.1-17.el8.x86_64 
[root@centos8 ~]# rpm -qf /usr/sbin/swapoff
util-linux-2.32.1-17.el8.x86_64

如果误删除了命令可以用dnf安装

[root@centos8 ~]# dnf install util-linux

查看swap当前的状态

查看swap的空间,共4G

[root@centos8 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3757        1224        1080          20        1452        2270
Swap:          4047           0        4047

用swapon查看所属设备和大小:

#-v: –verbose                 详尽模式

[root@centos8 ~]# swapon -v
NAME      TYPE      SIZE USED PRIO
/dev/dm-1 partition   4G   0B   -2

临时关闭swap

1:查看swapoff的版本

[root@centos8 ~]# swapoff --version
swapoff,来自 util-linux 2.32.1

2:关闭swap

#-a:all,禁用 /proc/swaps 中的所有交换区

[root@centos8 ~]# swapoff -a

3:检查效果 :

[root@centos8 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3757        1221        1083          20        1451        2273
Swap:             0           0           0

swap的大小为0,已关闭

4:用swapon检查:

[root@centos8 ~]# swapon -v

输出为空,表示swap已关闭

临时关闭swap后启用swap

1:查看swapon的版本

[root@centos8 ~]# swapon --version
swapon,来自 util-linux 2.32.1

2:启用 swap

#-a:–all                启用 /etc/fstab 中的所有交换区

[root@centos8 ~]# swapon -a
查看效果:
[root@centos8 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3757        1225        1079          20        1452        2269
Swap:          4047           0        4047

可以看到swap的空间显示为4G

用swapon -v查看

[root@centos8 ~]# swapon -v
NAME      TYPE      SIZE USED PRIO
/dev/dm-1 partition   4G   0B   -2

永久关闭swap

查看/etc/fstab

[root@centos8 ~]# more /etc/fstab

找到swap分区的记录:

...
/dev/mapper/cl-root     /                       xfs     defaults        0 0
UUID=f384615e-7c71-43b0-876c-45e8f08cfa6e /boot                   ext4    defaults        1 2
/dev/mapper/cl-home     /home                   xfs     defaults        0 0
/dev/mapper/cl-swap     swap                    swap    defaults        0 0

把加载swap分区的那行记录注释掉即可

#/dev/mapper/cl-swap     swap                    swap    defaults        0 0

重启机器

[root@centos8 ~]# systemctl reboot

再用free -m检查一下

[liuhongdi@centos8 ~]$ free -m
              total        used        free      shared  buff/cache   available
Mem:           3757        1127        1906          17         722        2370
Swap:             0           0           0

查看linux的版本

[liuhongdi@centos8 ~]$ cat /etc/redhat-release 
CentOS Linux release 8.1.1911 (Core)

搬瓦工推荐方案

温馨提醒 如果您有选择困难症,直接选中间的 CN2 GIA-E方案,季付 $49.99,多达 12 个机房任意切换
方案 内存 CPU 硬盘 流量/月 带宽 机房 价格 购买
CN2
(最便宜)
1GB 1核 20GB 1TB 1Gbps DC3 CN2
DC8 ZNET
$49.99/年 直达
CN2 2GB 1核 40GB 2TB 1Gbps $52.99/半年
$99.99/年
直达
CN2 GIA-E
(最推荐)
1GB 2核 20GB 1TB 2.5Gbps DC6 CN2 GIA-E
DC9 CN2 GIA
日本软银 JPOS_1
荷兰 EUNL_9
$49.99/季度
$169.99/年
直达
CN2 GIA-E 2GB 3核 40GB 2TB 2.5Gbps $89.99/季度
$299.99/年
直达
HK 2GB 2核 40GB 0.5TB 1Gbps 中国香港 CN2 GIA $89.99/月
$899.99/年
直达
HK 4GB 4核 80GB 1TB 1Gbps $155.99/月
$1559.99/年
直达
TOKYO 2GB 2核 40GB 0.5TB 1.2Gbps 日本东京 CN2 GIA $89.99/月
$899.99/年
直达
TOKYO 4GB 4核 80GB 1TB 1.2Gbps $155.99/月
$1559.99/年
直达
搬瓦工优惠码:BWH3HYATVBJW 2022年最新搬瓦工VPS购买图文教程

【AD】美国洛杉矶CN2 VPS/香港CN2 VPS/日本CN2 VPS推荐,延迟低、稳定性高、免费备份_搬瓦工vps

【AD】Justhost:全场VPS 8折优惠,200Mbps-1Gbps带宽不限流量,可选中国香港/新加坡/美国/俄罗斯CN2线路等24个机房,月付8元起