Linux系统中如何调整swap大小

Linux系统中如何调整swap大小,第1张

这里我要跟大家介绍的是Linux系统中调整swap大小的 方法 。欢迎大家阅读。

 

Linux系统中调整swap大小的方法

1、使用free命令带上m参数,查看swap文件大小,官方建议在RAM是2到4.5G时,swap是RAM的2倍如果RAM大于等于4G则swap等于RAM即可

2、也可用cat 查看etc目录下的swaps文件,如下图

3、创建一个swap文件

复制内容到剪贴板

########

dd if=/dev/zero of=/tmp/swap bs=1MB count=1024

########

以下仅供参考:不用看

Creating a swap file

First of, make sure the file system the disk you wish to swap on is properly mounted. For the purposes of this tutorial we will assume the disk is mounted as /mnt and we want to use the file /mnt/myswap.swp for swapping.

使用下面的命令创建一个1G的swap文件

复制内容到剪贴板

Use the following command to create a 1024MB file that we will use for swapping

########

dd if=/dev/zero of=/mnt/myswap.swp bs=1024MB count=1

########

4、制作一个swap文件,如果觉得繁琐,可以将mkswap文件挂载到tmp目录下,例如:

复制内容到剪贴板

########

mkswap /tmp/swap

########

以下仅供参考:不用看

Preparing the swap file

Before we enable the swap file we must first set it up. The following command accomplishes just that:

########

mkswap /mnt/myswap.swp

########

5、使用swapon 启动/tmp/swap

复制内容到剪贴板

########

swapon /tmp/swap

########

使用一个swap分区,如果已经有一个swap分区,该步骤可以省略

以下仅供参考:不用看

Using a swap partition

Setting up a swap partition is a bit more difficult, as the partition must be first created then formatted using the linux-swap file system. Once that is done, assuming the swap partition is at/dev/scsi/host0/bus0/target0/lun0/part5 (common for v24 USB drives with mutiple partitions), the only command that needs be issued is:

6、再次使用free查看增加后的swap大小

7、如果只想增加swap大小,请忽略以下的 *** 作,重要!!如果不使用刚才增加的1G的swap文件,使用下面的swapoff命令,可以关闭,这样swap文件就缩小了1G

使用swapoff关闭swap文件

复制内容到剪贴板

########

swapoff /tmp/swap

########

注意事项:如果只想增加swap大小,请忽略第7步的所有 *** 作。

ize Used Priority

/dev/dm-1 partition 83886040 -2

2、给主机添加一块1g磁盘,并完成分区

[root@dbserver~]# lsblk -p

NAME MAJ:MINRM SIZE RO TYPE MOUNTPOINT

/dev/sda 8:0 0 100G 0disk

├─/dev/sda1 8:1 0 2G 0part /boot

└─/dev/sda2 8:2 0 98G 0part

├─/dev/mapper/rhel-root253:0 0 55G 0lvm /

├─/dev/mapper/rhel-swap253:1 0 8G 0lvm [SWAP]

└─/dev/mapper/rhel-home253:2 0 35G 0lvm /home

/dev/sdb 8:16 0 1G 0disk

└─/dev/sdb1 8:17 01023M 0 part

/dev/sr0 11:0 11024M 0 rom

说明:/dev/sdb1为新添加磁盘分区

3、将新添加的分区设置为交换区

[root@dbserver~]# mkswap /dev/sdb1

Setting upswapspace version 1, size = 1047548 KiB

no label,UUID=cce40e01-b659-43aa-8801-a2a0cccf77eb

#启用交换分区/dev/sdb1

[root@dbserver~]# swapon /dev/sdb1

[root@dbserver~]# swapon -s

Filename Type Size Used Priority

/dev/dm-1 partition 83886040 -2

/dev/sdb1 partition 10475480 -3

[root@dbserver~]# free -m

total used free shared buff/cache available

Mem: 7821 581 6770 15 469 6946

Swap: 9214 0 9214

#设置开机自动挂载

[root@dbserver~]# echo "/dev/sdb1 swap swap defaults 0 0" >>/etc/fstab

说明:此处第一个0表示不要备份,第二0表示系统开机过程中不需要检验


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/7230517.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-03
下一篇2023-04-03

发表评论

登录后才能评论

评论列表(0条)

    保存