linux增加swap分区和删除swapfile文件的方法
* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。
* @author Alan
* @Email no008@foxmail.com
正文
创建swapfile文件
执行命令:dd if=/dev/zero of=swapfile bs=1024 count=5000000
注:可根据实际需要更改count值的大小,这里设置5000000,大概就是4G多
[root@node2 var]# dd if=/dev/zero of=swapfile bs=1024 count=500000 500000+0 records in 500000+0 records out 512000000 bytes (512 MB) copied, 5.92783 s, 86.4 MB/s [root@node2 var]# ls -alh total 489M drwxr-xr-x. 19 root root 283 May 9 13:21 . dr-xr-xr-x. 20 root root 269 Apr 1 09:47 .. drwxr-xr-x. 2 root root 6 Apr 11 2018 adm drwxr-xr-x. 5 root root 44 Jan 7 19:37 cache drwxr-xr-x. 2 root root 6 Aug 8 2019 crash drwxr-xr-x. 3 root root 34 Mar 2 16:10 db drwxr-xr-x. 3 root root 18 Jan 7 19:37 empty drwxr-xr-x. 2 root root 6 Apr 11 2018 games drwxr-xr-x. 2 root root 6 Apr 11 2018 gopher drwxr-xr-x. 3 root root 18 Sep 14 2019 kerberos drwxr-xr-x. 34 root root 4.0K Apr 20 11:39 lib drwxr-xr-x. 2 root root 6 Apr 11 2018 local lrwxrwxrwx. 1 root root 11 Jan 7 19:37 lock -> ../run/lock drwxr-xr-x. 10 root root 4.0K May 9 13:11 log lrwxrwxrwx. 1 root root 10 Jan 7 19:37 mail -> spool/mail drwxr-xr-x. 2 root root 6 Apr 11 2018 nis drwxr-xr-x. 2 root root 6 Apr 11 2018 opt drwxr-xr-x. 2 root root 6 Apr 11 2018 preserve lrwxrwxrwx. 1 root root 6 Jan 7 19:37 run -> ../run drwxr-xr-x. 8 root root 87 Jan 7 19:37 spool -rw-r--r-- 1 root root 489M May 9 13:21 swapfile drwxrwxrwt. 4 root root 112 May 9 13:11 tmp -rw-r--r--. 1 root root 163 Jan 7 19:37 .updated drwxr-xr-x. 2 root root 6 Apr 11 2018 yp [root@node2 var]# free -h total used free shared buff/cache available Mem: 1.8G 495M 186M 9.5M 1.1G 1.1G Swap: 3.9G 0B 3.9G
将swapfile设置为swap空间
执行命令:mkswap swapfile
[root@node2 var]# mkswap swapfile Setting up swapspace version 1, size = 499996 KiB no label, UUID=c67544b9-60b6-426e-8349-5f62813d9deb [root@node2 var]# free -h total used free shared buff/cache available Mem: 1.8G 497M 180M 9.6M 1.1G 1.1G Swap: 3.9G 0B 3.9G
启用交换空间
执行命令:swapon swapfile
[root@node2 var]# swapon swapfile swapon: /var/swapfile: insecure permissions 0644, 0600 suggested.
[root@node2 var]# free -h total used free shared buff/cache available Mem: 1.8G 497M 180M 9.6M 1.1G 1.1G Swap: 4.4G 0B 4.4G #将489M的交换文件的空间启用了
删除swapfile
如果创建了swapfile之后,想要删除,必须先执行以下命令
swapoff swapfile
[root@node2 var]# swapoff swapfile [root@node2 var]# free -h total used free shared buff/cache available Mem: 1.8G 497M 180M 9.6M 1.1G 1.1G Swap: 3.9G 0B 3.9G
然后再执行删除文件操作
rm swapfile
[root@node2 var]# rm swapfile rm: remove regular file ‘swapfile’? y [root@node2 var]# ls -alh total 12K drwxr-xr-x. 19 root root 267 May 9 14:15 . dr-xr-xr-x. 20 root root 269 Apr 1 09:47 .. drwxr-xr-x. 2 root root 6 Apr 11 2018 adm drwxr-xr-x. 5 root root 44 Jan 7 19:37 cache drwxr-xr-x. 2 root root 6 Aug 8 2019 crash drwxr-xr-x. 3 root root 34 Mar 2 16:10 db drwxr-xr-x. 3 root root 18 Jan 7 19:37 empty drwxr-xr-x. 2 root root 6 Apr 11 2018 games drwxr-xr-x. 2 root root 6 Apr 11 2018 gopher drwxr-xr-x. 3 root root 18 Sep 14 2019 kerberos drwxr-xr-x. 34 root root 4.0K Apr 20 11:39 lib drwxr-xr-x. 2 root root 6 Apr 11 2018 local lrwxrwxrwx. 1 root root 11 Jan 7 19:37 lock -> ../run/lock drwxr-xr-x. 10 root root 4.0K May 9 13:11 log lrwxrwxrwx. 1 root root 10 Jan 7 19:37 mail -> spool/mail drwxr-xr-x. 2 root root 6 Apr 11 2018 nis drwxr-xr-x. 2 root root 6 Apr 11 2018 opt drwxr-xr-x. 2 root root 6 Apr 11 2018 preserve lrwxrwxrwx. 1 root root 6 Jan 7 19:37 run -> ../run drwxr-xr-x. 8 root root 87 Jan 7 19:37 spool drwxrwxrwt. 4 root root 112 May 9 13:11 tmp -rw-r--r--. 1 root root 163 Jan 7 19:37 .updated drwxr-xr-x. 2 root root 6 Apr 11 2018 yp [root@node2 var]#
注:如果没有执行swapoff操作,就执行执行删除文件的操作,系统会报错,不给删除。swapoff相当于是卸载swap分区。
学问:纸上得来终觉浅,绝知此事要躬行
为事:工欲善其事,必先利其器。
态度:道阻且长,行则将至;行而不辍,未来可期
.....................................................................
------- 桃之夭夭,灼灼其华。之子于归,宜其室家。 ---------------
------- 桃之夭夭,有蕡其实。之子于归,宜其家室。 ---------------
------- 桃之夭夭,其叶蓁蓁。之子于归,宜其家人。 ---------------
=====================================================================
* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。 * @author Alan -liu * @Email no008@foxmail.com
转载请标注出处! ✧*꧁一品堂.技术学习笔记꧂*✧. ---> https://www.cnblogs.com/ios9/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?