为centos7添加swap分区

使用文件系统构建swap

1、使用dd这个命令来新增一个2G的文件在/home下面:

[root@VM_167_102_centos ~]# dd if=/dev/zero of=/home/swap bs=1024 count=2097152
2097152+0 records in
2097152+0 records out
2147483648 bytes (2.1 GB) copied, 16.9248 s, 127 MB/s

2、使用mkswap将/home/swap这个文件格式化为swap的文件格式:

[root@VM_167_102_centos ~]# mkswap /home/swap
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=becd1f39-07b5-40d5-b675-8a1f5c23909d

3、使用swapon来将/home/swap启动

复制代码

[root@VM_167_102_centos home]# free -m
total used free shared buffers cached
Mem: 994 910 83 12 4 651
-/+ buffers/cache: 254 739
Swap: 0 0 0
[root@VM_167_102_centos home]# swapon /home/swap
swapon: /home/swap: insecure permissions 0644, 0600 suggested.
[root@VM_167_102_centos home]# free -m
total used free shared buffers cached
Mem: 994 912 82 12 4 651
-/+ buffers/cache: 256 738
Swap: 2047 0 2047

复制代码

使用swapoff关掉swapfile

1 [root@VM_167_102_centos home]# swapoff /home/swap 

为了能够让swap自动挂载,要修改/etc/fstab文件 

在文件末尾加上 

/home/swap swap swap default 0 0 
这样就算系统重启也不用手动挂载了,自动就挂载好了

原文地址:https://www.cnblogs.com/gbwpyq/p/6659414.html