Linux设置SWAP分区

场景:
今天下午安装一个CentOS6.5操作系统,忘记配置swap分区。看看如何安装系统之后,增加和删除swap分区。
方法如下:

1.内存占用情况

[root@mysql01 Downloads]# free
total       used       free     shared    buffers     cached
Mem:       1922020    1691448     230572          0      49844    1252236
-/+ buffers/cache:     389368    1532652
Swap:            0          0          0

2.通过dd命令增加swap

[root@mysql01 var]# dd if=/dev/zero of=/var/swap bs=1024 count=2048000
2048000+0 records in
2048000+0 records out
2097152000 bytes (2.1 GB) copied, 5.9442 s, 353 MB/s

3.创建swap文件

[root@mysql01 var]# mkswap /var/swap
mkswap: /var/swap: warning: don’t erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 2047996 KiB
no label, UUID=daa18338-0371-4848-888d-49d662dafe3f

[root@mysql01 var]# mkswap -f /var/swap
Setting up swapspace version 1, size = 2047996 KiB
no label, UUID=754e3fdf-258c-45f2-bb8e-4725862850d3

4.加载该文件:
[root@mysql01 var]# swapon /var/swap

5.看看swap是否生效
[root@mysql01 var]# free

6.查看swap当前状态:
[root@mysql01 var]# cat /proc/swaps
Filename                                Type            Size    Used    Priority
/var/swap                               file            2047992 0       -1

7.添加至/etc/fstab

[root@mysql01 var]# vim /etc/fstab

#
# /etc/fstab
# Created by anaconda on Tue Sep 23 17:58:53 2014
#
# Accessible filesystems, by reference, are maintained under ‘/dev/disk’
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=98f4128a-2ac6-4822-8624-300c4d818afc /                       ext4    defaults        1 1
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/var/swap               swap                    swap    defaults        0 0

8.如果不再需要swap,可以清理该分区:
[root@mysql01 var]# swapoff /var/swap