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