创建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分区。