ubuntu 18.10增加和设置Swap交换分区

来自:
ubuntu 18.10增加和设置Swap交换分区

原文抄录如下:

ubuntu 18.10增加和设置Swap交换分区
96 毛豆豆豆豆子
2019.01.03 13:49* 字数 497 阅读 85评论 0喜欢 0
时间 作者
2019-01-03 13:32:30 张飞
公司买了2台服务器,装了ubuntu18.10的系统,老板让把swap交换分区设置成96G ,老板说一般swap分区的大小是内存的1.5倍,只是我们的服务器内存有96G够用了,所以交换分区设置成96G就行了,好吧现在开始配置吧!

本文是本人配置的时候记录下来的 希望能帮助到大家,
参考:
https://blog.csdn.net/yc461515457/article/details/53610412
https://blog.csdn.net/wangjunjun2008/article/details/50681115
https://www.cnblogs.com/yuluoxingkong/p/8968868.html
http://blog.sina.com.cn/s/blog_6e4388910100tsk7.html
https://blog.csdn.net/lhs960124/article/details/80446433
检查 Swap文件
$ swapon -s
Filename Type Size Used Priority
/swap.img file 8388604 1036 -2
上述表示查出一个swap分区文件大小是8G左右
创建一个swap文件
sudo dd if=/dev/zero of=/data/swap-yd.img bs=1G count=96
bs=1G
上面命令中的 count 即代表swap文件大小 (既 bs * countu = 96 * 1G = 96G)
/data/swap-yd.img swap文件的位置
查看之前的swap文件权限
ll /swap.img
-rw------- 1 root root 8589934592 Jan 2 12:47 /swap.img
设置swap文件权限
sudo chmod 600 /data/swap-yd.img
激活swap文件
sudo mkswap /data/swap-yd.img
Setting up swapspace version 1, size = 96 GiB (103079211008 bytes)
no label, UUID=68456241-9ec9-4b90-b325-a2d68ef08340
卸载这个 swap 文件
sudo swapoff /data/swap-yd.img
激活swap到现有系统中(重启后将会无效)
sudo swapon /data/swap-yd.img
现在在检查swap文件
swapon -s
Filename Type Size Used Priority
/swap.img file 8388604 1036 -2
/data/swap-yd.img file 100663292 0 -3
已经将/data/swap-yd.img 文件添加进swap并使用中
查看现有的swap交换空间的大小
top

top - 04:04:00 up 15:05, 9 users, load average: 0.16, 0.04, 0.24
Tasks: 314 total, 1 running, 313 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 96609.3 total, 1396.9 free, 543.8 used, 94668.6 buff/cache
MiB Swap: 106496.0 total, 106495.0 free, 1.0 used. 95221.7 avail Mem
Swap: 106496.0 total 大小为106G左右, 之前的swap有8G我们又加了96大概就是100多G(我也清楚为什么不是96+8=104G呢 -.-!)
重启服务器查看swap的大小
sudo reboot // 重启服务器
top // 查看swap
top - 04:19:35 up 2 min, 1 user, load average: 0.57, 0.66, 0.28
Tasks: 309 total, 1 running, 308 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 96609.3 total, 96009.9 free, 326.7 used, 272.6 buff/cache
MiB Swap: 8192.0 total, 8192.0 free, 0.0 used. 95672.2 avail Mem
Swap: 8192.0 total 又回到了8G左右
设置swap分区永久有效
sudo vim /etc/fstab
// 在最后一行添加如下
/data/swap-yd.img none swap sw 0 0
tips:然后重启服务器sudo reboot(重启生效)

重启之后再次检查swap
swapon -s
Filename Type Size Used Priority
/swap.img file 8388604 1036 -2
/data/swap-yd.img file 100663292 0 -3
重启之后可以看到我们配置的/data/swap-yd.img文件在swap中了
查看swap分区大小
top - 05:23:49 up 56 min, 1 user, load average: 0.02, 0.01, 0.00
Tasks: 290 total, 1 running, 289 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 96609.3 total, 95976.0 free, 351.4 used, 281.9 buff/cache
MiB Swap: 106496.0 total, 106496.0 free, 0.0 used. 95642.9 avail Mem
top 查看swap大小也是改变了的
提示
tips :永久有效必须重启才能生效
如果我们想要现在生效,并且重启后不恢复可以用立即生效配合永久生效一起使用

问题:
swapoff用错了。。。
假如按照他那样使用的话,会报错:
在这里插入图片描述

swapoff应该是将目前的swap交换区卸载的。。。。。

猜你喜欢

转载自blog.csdn.net/cdnight/article/details/87625721