Linux环境下Swap配置方法

时间:2022-11-17 23:15:42
Linux环境下Swap配置方法

Linux环境下Swap配置方法

场景:
今天下午安装一个CentOS6.5操作系统,忘记配置swap分区。看看如何安装系统之后,增加和删除swap分区。
方法如下:
1.内存占用情况
[root@joshua ~]# free -m
total used free shared buffers cached
Mem: 2006 1886 119 31 33 574
-/+ buffers/cache: 1278 727
Swap: 0 0 0
2.通过dd命令增加swap,增加一个2g的swap空间
[root@joshua ~]# 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, 36.4509 s, 57.5 MB/s

3.创建swap文件
[root@joshua ~]# 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=eba0b718-b5d0-488f-a368-0af288def8b6

[root@joshua ~]# mkswap -f /var/swap
Setting up swapspace version 1, size = 2047996 KiB
no label, UUID=5039b019-2db5-41b2-a289-3caf4fdd98bf
[root@joshua ~]#

4.加载该文件:
[root@joshua ~]# swapon /var/swap
5.看看swap是否生效
[root@mysql01 var]# free
6.查看swap当前状态:
[root@joshua ~]# cat /proc/swaps
Filename Type Size Used Priority
/var/swap file 2047996 0 -1

7.添加至/etc/fstab , vim /etc/fstab

#
# /etc/fstab
# Created by anaconda on Thu Aug 24 12:21:20 2017
#
# 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=1ae5f12f-79c1-47d4-bed7-941ab9385396 / 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@joshua ~]# swapoff /var/swap