实战扩展新增swap分区

Swap分区在系统的物理内存不够用的时候,把硬盘空间中的一部分空间释放出来,以供当前运行的程序使用。

mkswap /devices (格式化成swap格式)

swapon /swap (激活/swap,加入到swap分区中)

vim /etc/fstab (开机自启动新添加的swap分区)  ,在最后追加:

/devices swap swap defaults 0 0

如果不想使用需要删除,只需要执行#swapoff /swap

方法一、使用新建分区增加SWAP分区

[root@xuegod63 ~]# gdisk  /dev/sdb

...

Command (? for help): n  #新建分区

Partition number (2-128, default 2):     #回车

First sector (34-41943006, default = 2099200) or {+-}size{KMGTP}:  #回车

Last sector (2099200-41943006, default = 41943006) or {+-}size{KMGTP}: +1G  #给1G

Current type is 'Linux filesystem'

Hex code or GUID (L to show codes, Enter = 8300):  #回车

Changed type of partition to 'Linux filesystem'

Command (? for help): w  #保存

Do you want to proceed? (Y/N): y

格式化swap

[root@xuegod63 ~]# mkswap /dev/sdb2

正在设置交换空间版本 1,大小 = 2097148 KiB

无标签,UUID=dc41b5ef-bcf1-477c-902e-c5bb00d41c1e

验证:

[root@xuegod63 ~]# free -m

              total        used        free      shared  buff/cache   available

Mem:            977         557          75          16         345         168

Swap:          2047         234        1813

[root@xuegod63 ~]# swapon /dev/sdb2   ---开启

[root@xuegod63 ~]# free -m

              total        used        free      shared  buff/cache   available

Mem:            977         556          75          16         345         169

Swap:          3071         234        2837

[root@xuegod63 ~]# swapoff /dev/sdb2    ---关闭

[root@xuegod63 ~]# free -m

              total        used        free      shared  buff/cache   available

Mem:            977         556          75          16         345         169

Swap:          2047         234        1813

[root@xuegod63 ~]# swapon -s      ---统计

Filename                      Type              Size Used      Priority

/dev/dm-1                                partition 2097148 240532   -1

[root@xuegod63 ~]# swapon /dev/sdb2

[root@xuegod63 ~]# swapon -s  #查看

Filename                      Type              Size Used      Priority

/dev/dm-1                                partition 2097148 240532   -1

/dev/sdb2                                 partition 1048572 0     -2

方法二、通过文件增加SWAP分区

[root@xuegod63 ~]# dd if=/dev/zero of=swap_file bs=1M count=500      

#生成一个500MB的空文件,文件名为:swap_file

[root@xuegod63 ~]# ll /root/swap_file

[root@xuegod63 ~]# chmod 0600 /root/swap_file      #设置权限

[root@xuegod63 ~]# mkswap -f /root/swap_file        #格式化swap_file文件为swap系统文件格式

[root@xuegod63 ~]# swapon /root/swap_file        #启用swap_file文件的交换分区

[root@xuegod63 ~]# free -m

版权声明:
作者:WaterBear
链接:https://l-t.top/1426.html
来源:雷霆运维
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>