linux增加swap大小

本文档详细介绍了在Linux系统中通过创建swap文件来增加虚拟内存的方法,包括使用dd命令创建文件,mkswap进行格式化,swapon启动交换空间,以及在/etc/fstab中配置使其开机自动启用。此外,还提到了另一种方式是通过创建新的swap分区,涉及fdisk进行分区,更改分区ID,mkswap格式化,以及swapon启动。
摘要由CSDN通过智能技术生成

1、用dd命令增加swapfile:dd if=/dev/zero of=/whatever/swap bs=block_size count=number_of_block

[root@sense image]# dd if=/dev/zero of=/opt/image/swap bs=1M count=2048

2048000+0 records in

2048000+0 records out

2097152000 bytes (2.1 GB) copied, 272.867 seconds, 7.7 MB/s

2、创建(设置)交换空间,使用命令mkswap

[root@sense image]# mkswap /opt/image/swap

Setting up swapspace version 1, size = 2097147 kB

3、检查现有的交换空间大小,使用命令free

[root@sense image]# free -m

total used free shared buffers cached

Mem: 1011 989 21 0 1 875

-/+ buffers/cache: 112 898

Swap: 1027 0 1027

或者检查meminfo文件

grep SwapTotal /proc/meminfo

4、启动新增加的2G的交换空间,使用命令swapon

[root@sense image]# swapon /opt/image/swap

5、确认新增加的2G交换空间已经生效,使用命令free

[[root@sense image]# free -m

total used free shared buffers cached

Mem: 1011 995 15 0 4 877

-/+ buffers/cache: 113 897

Swap: 3027 0 3027

或者检查meminfo文件

grep SwapTotal /proc/meminfo

6、修改/etc/fstab文件,使得新加的2G交换空间在系统重新启动后自动生效

在文件最后加入:

/opt/image/swap swap swap defaults 0 0

--------------------------------------------------------------

Linux增加swap空间的方法windows下有虚拟内存,Linux下有swap。

如果在安装linux时没有分配足够的swap,可以在Linux下进行增加。

具体有两种方法:1、建立一个swap分区

              2、建立一个swap文件

一、建立一个swap分区

   可以利用磁盘的还未分配的空间建立一个swap分区;如果磁盘已全部分配,那就只能增加一块硬盘,并从其中分割出swap分区。

   1.用fdisk建立一个分区

    2.将分区的id更改为82

    3.用mkswap将分区格式化

   4.用swapon命令启动swap(用swapoff关闭)

具体操作如下:

[root@redhat ~]# fdisk /dev/sdc --用fdisk对硬盘进行分区

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOS disklabel. Changes will remain in memory only,

until you decide to write them. After that, of course, the previous

content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): m --显示fdisk命令参数

Command action

   a toggle a bootable flag

   b edit bsd disklabel

   c toggle the dos compatibility flag

   d delete a partition

   l list known partition types

   m print this menu

   n add a new partition

   o create a new empty DOS partition table

   p print the partition table

   q quit without saving changes

   s create a new empty Sun disklabel

   t change a partition's system id

   u change display/entry units

   v verify the partition table

   w write table to disk and exit

   x extra functionality (experts only)

Command (m for help): n --增加一个新分区

Command action 

   e extended

   p primary partition (1-4)

p --选择分区类型

Partition number (1-4): 1 --指定分区号

First cylinder (1-130, default 1): 1 --指定歧视磁柱号

Last cylinder or +size or +sizeM or +sizeK (1-130, default 130): 130 --指定结束磁柱号

Command (m for help): p --显示分区列表

Disk /dev/sdc: 1073 MB, 1073741824 bytes

255 heads, 63 sectors/track, 130 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot Start End Blocks Id System

/dev/sdc1 1 130 1044193+ 83 Linux

Command (m for help): t --更改分区id

Selected partition 1

Hex code (type L to list codes): 82 --82是swap的分区id号

Changed system type of partition 1 to 82 (Linux swap)

Command (m for help): p --显示分区列表

Disk /dev/sdc: 1073 MB, 1073741824 bytes

255 heads, 63 sectors/track, 130 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot Start End Blocks Id System

/dev/sdc1 1 130 1044193+ 82 Linux swap

Command (m for help): w --写入分区表并退出

The partition table has been altered!

Calling ioctl() to re-read partition table.

Syncing disks.

[root@redhat ~]# fdisk -l --显示所有分区信息

Disk /dev/sda: 8589 MB, 8589934592 bytes

255 heads, 63 sectors/track, 1044 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot Start End Blocks Id System

/dev/sda1 * 1 905 7269381 83 Linux

/dev/sda2 906 1044 1116517+ 82 Linux swap

Disk /dev/sdb: 8589 MB, 8589934592 bytes

255 heads, 63 sectors/track, 1044 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot Start End Blocks Id System

/dev/sdb1 1 1044 8385898+ 83 Linux

Disk /dev/sdc: 1073 MB, 1073741824 bytes

255 heads, 63 sectors/track, 130 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot Start End Blocks Id System

/dev/sdc1 1 130 1044193+ 82 Linux swap

[root@redhat ~]# mkswap /dev/sdc1 --对sdc1格式化为swap格式

Setting up swapspace version 1, size = 1069248 kB

[root@redhat ~]# free --查看当前虚拟内存情况

             total used free shared buffers cached

Mem: 1034564 465176 569388 0 13928 365672

-/+ buffers/cache: 85576 948988

Swap: 1116508 0 1116508 --虚拟内存总量为1116508

[root@redhat ~]# swapon /dev/sdc1 --启动虚拟内存

[root@redhat ~]# free --查看当前虚拟内存情况

             total used free shared buffers cached

Mem: 1034564 465696 568868 0 13956 365644

-/+ buffers/cache: 86096 948468

Swap: 2160692 0 2160692 --虚拟内存总量增加

二、建立一个swap文件

 

   如果不想建立一个swap分区,可以建立一个swap文件,而且步骤更加的简单:

 

1、用dd指令建了一个swapfile;

 

2、用mkswap将swapfile格式化为swap文件格式;

 

3、用swapon启动该文件(用swapoff关闭)

 

[root@redhat ~]# dd if=/dev/zero of=/tmp/swap bs=1M count=1024--用dd命令增加swapfile

1024+0 records in

1024+0 records out

[root@redhat ~]# ll /tmp/swap 

-rw-r--r-- 1 root root 1073741824 Oct 31 18:52 /tmp/swap

[root@redhat ~]# mkswap /tmp/swap --用mkswap格式化swapfile

Setting up swapspace version 1, size = 1073737 kB

[root@redhat ~]# free --查看当前虚拟内存情况

             total used free shared buffers cached

Mem: 1034564 1008344 26220 0 6276 906844

-/+ buffers/cache: 95224 939340

Swap: 1116508 0 1116508 --当前是1116508

[root@redhat ~]# swapon /tmp/swap --启动swapfile

[root@redhat ~]# free --再次查看虚拟内存情况

             total used free shared buffers cached

Mem: 1034564 1008792 25772 0 6292 906828

-/+ buffers/cache: 95672 938892

Swap: 2165076 0 2165076 --虚拟内存增加

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值