现在的位置: 首页 > 综合 > 正文

Linux增加swap空间的方法

2018年05月07日 ⁄ 综合 ⁄ 共 5032字 ⁄ 字号 评论关闭
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 --虚拟内存增加

抱歉!评论已关闭.