linux添加交换空间


 先看系统有没有设置交换空间
# free -h  #查看内存使用情况

[root@localhost guide_tour_manage1071]# free -h
              total        used        free      shared  buff/cache   available
Mem:            15G        6.8G        8.2G         12M        568M        8.5G
Swap:           5G         4.2G        0.8G
[root@localhost guide_tour_manage1071]#

很明显已经存在Swap交换空间了,大小是5G,如果想扩容的话,可以先取消交换空间使用 ,重新创建一个更大的交换空间

 

步骤如下:

查看现有交换空间大小及路径

# cat /proc/swaps   (或者 # swapon -s)

[root@localhost guide_tour_manage1071]# swapon -s
文件名                类型        大小    已用    权限
/dev/dm-1                                 file    5242876    6627092    -1
[root@localhost guide_tour_manage1071]#

可以看到交换空间的路径是/dev/dm-1 ,大小为5G

停止交换控件的使用
# swapoff /dev/dm-1

 

使用free -h 查看发现交换空间已经不见了

[root@localhost guide_tour_manage1071]# free -h
              total        used        free      shared  buff/cache   available
Mem:            15G        6.8G        8.2G         12M        568M        8.5G
[root@localhost guide_tour_manage1071]#

 

正常情况下交换空间的大小不应该小于物理空间

新建一个swap使用的文件夹,在文件夹中创建一个名为swap,大小16G的文件(设置的越大耗时越长,耐心等待一会就好)

# mkdir /swap

# dd if=/dev/zero of=/swap/swap bs=1024M count=16

 

设置交换空间文件

# mkswap /swap/swap

[root@localhost ~]# mkswap /swap/swap
正在设置交换空间版本 1,大小 = 16777212 KiB
无标签,UUID=837bf053-e3d5-4607-a83f-7c2a555dc032

 

启用交换空间

# swapon /swap/swap

[root@localhost ~]# swapon /swap/swap
swapon: /swap/swap:不安全的权限 0644,建议使用 0600

#提示是因为我们给交换空间文件的权限过大了 设置为600就好了(如果是自己的电脑不设置也行)

 

再次使用free -h 查看内存情况,交换空间已经启用了

[root@localhost guide_tour_manage1071]# free -h
              total        used        free      shared  buff/cache   available
Mem:            15G        6.8G        8.2G         12M        568M        8.5G
Swap:           15G        6.4G        9.6G
[root@localhost guide_tour_manage1071]#

 

注意:这样的操作在系统重启后会失效,

只要在修改/etc/fstab文件,添加下边一行就可设置开机启动(原先有关swap的设置可以删除)

# vim /etc/fstab

/swap/swap swap swap defaults 0 0

#
# /etc/fstab
# Created by anaconda on Wed Jul 26 03:28:05 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
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=64c5cd1e-94cb-479c-863f-259a0179c5b9 /boot                   xfs     defaults        0 0
/swap/swap   swap   swap   defaults   0   0

查看交换空间的使用比列配置
cat /proc/sys/vm/swappiness
60


# sysctl vm.swappiness=10   #临时修改
# vim /etc/sysctl.conf   #永久修改,加入西边的的
vm.swappiness = 10
# sysctl -p  #使修改的生效

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM