系统相关
首页 > 系统相关> > 【linux】实战扩展swap分区

【linux】实战扩展swap分区

作者:互联网

swap 分区

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

基础命令:

mkswap /devices : 格式化成swap格式
swapon /devices : 激活swap ,加入到swap分区中
开机自动启动新添加的swap分区: /etc/fstab /devices swap swap defaults 0 0

实验步骤:

  1. 建立分区
    [root@xinsz08 ~]# 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  #给1GCurrent 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

2) 查看是否分区成功

[root@centos7-xinsz08 ~]# ll /dev/sdb*brw-rw----. 1 root disk 8, 16 2月  26 15:36 /dev/sdb
brw-rw----. 1 root disk 8, 17 2月  26 15:36 /dev/sdb1
brw-rw----. 1 root disk 8, 18 2月  26 15:37 /dev/sdb2

  1. 格式化
[root@centos7-xinsz08 ~]# mkswap /dev/sdb2正在设置交换空间版本 1,大小 = 1048572 KiB
无标签,UUID=bf1788b2-c1f5-4f89-bb62-cd91617ee8dc

对比( 激活swap空间之前的对比)

[root@centos7-xinsz08 ~]# free -m  total        used        free      shared  buff/cache   available
Mem:           3770         454        2938          14         378        3082
Swap:          2047           0        2047[root@centos7-xinsz08 ~]# swapon /dev/sdb2 [root@centos7-xinsz08 ~]# free -m  total        used        free      shared  buff/cache   available
Mem:           3770         452        2940          14         377        3084
Swap:          3071           0        3071

使用 文件增加swap空间?
阿里云默认的没有swap空间,如何增加swap?

1) 使用dd命令创建个500M大小的文件[root@centos7-xinsz08 ~]# dd  if=/dev/zero of=swap_file bs=1M count=500记录了500+0 的读入
记录了500+0 的写出
524288000字节(524 MB)已复制,12.2954 秒,42.6 MB/秒[root@centos7-xinsz08 ~]# ll -h swap_file -rw-r--r--. 1 root root 500M 2月  26 15:46 swap_file[root@centos7-xinsz08 ~]# chmod 0600 swap_file 2) 把500M的文件格式化成swap 
[root@centos7-xinsz08 ~]# mkswap -f swap_file 正在设置交换空间版本 1,大小 = 511996 KiB
无标签,UUID=def65bf9-7143-4734-9801-d3d8bb583061[root@centos7-xinsz08 ~]# free -m  total        used        free      shared  buff/cache   available
Mem:           3770         455        2423          14         891        3075
Swap:          3071           0        3071
3) 激活swap空间[root@centos7-xinsz08 ~]# swapon /root/swap_file4) 查看是否被激活[root@centos7-xinsz08 ~]# free -m  total        used        free      shared  buff/cache   available
Mem:           3770         456        2423          14         891        3074
Swap:          3571           0        3571[root@centos7-xinsz08 ~]#

标签:free,分区,dev,centos7,swap,linux,xinsz08,root
来源: https://blog.51cto.com/xinsz08/2706429