手动增加swap空间 1、可能遇到的需求:某一个程序需要大一点的swap分区,需要我们手动增加swap分区 首先在文件系统中创建一个模拟的磁盘出来: dd if=/dev/zero of=/tmp/newdisk bs=1M count=100 dd命令:是用来操作磁盘的,可以读、写;if指定从哪里去读;of指定将0写到哪里去;bs指定每一个块的大小为1M;newdisk大小=count乘以100(即newdisk大小为100M) [root@linux-01 ~]# dd if=/dev/zero of=/tmp/newdisk bs=1M count=100 100+0 records in 100+0 records out 104857600 bytes (105 MB) copied, 4.65032 s, 22.5 MB/s [root@linux-01 ~]# du -sh /tmp/newdisk 100M /tmp/newdisk

2、创建完虚拟磁盘后需要格式化: [root@linux-01 ~]# mkswap -f /tmp/newdisk Setting up swapspace version 1, size = 102396 KiB no label, UUID=a211ca68-dba6-4e06-8e64-999c31ba4555

3、把新加的100Mswap加载到现在的swap上,使用swapon命令: [root@linux-01 ~]# free -m
total used free shared buff/cache available Mem: 976 131 584 6 259 660 Swap: 2047 0 2047 //未加载前swap分区大小2047 [root@linux-01 ~]# swapon /tmp/newdisk
swapon: /tmp/newdisk: insecure permissions 0644, 0600 suggested. //提示权限不安全 [root@linux-01 ~]# free -m
total used free shared buff/cache available Mem: 976 131 585 6 259 660 Swap: 2147 0 2147 //加载完100M之后可以查看到swap大小为2147M [root@linux-01 ~]# chmod 0600 /tmp/newdisk //更改newdisk权限为0600

4、卸载新增加的swap:使用swapoff命令: [root@linux-01 ~]# swapoff /tmp/newdisk [root@linux-01 ~]# free -m total used free shared buff/cache available Mem: 976 129 587 6 259 663 Swap: 2047 0 2047 //卸载完成之后swap大小恢复为2047M 不想要可以删除掉newdisk: [root@linux-01 ~]# rm -f /tmp/newdisk