参考网站:http://blog.csdn.net/hshl1214/article/details/6048278

原帖子写的很好,我把步骤给贴出来,测试系统:rhel6.4 x64


1.新建磁盘分区作为swap分区

[root@localhost /]# free -m  //查看当前swap信息
             total       used       free     shared    buffers     cached
Mem:          1878        348       1530          0         19        156
-/+ buffers/cache:        172       1706
Swap:         4031          0       4031
[root@localhost /]# swapoff -a      //停用swap空间
[root@localhost /]# free -m         //查看是否停用
             total       used       free     shared    buffers     cached
Mem:          1878        345       1532          0         19        156
-/+ buffers/cache:        169       1709
Swap:            0          0          0
[root@localhost /]# fdisk /dev/sdb  //进入sdb硬盘中(sdb硬盘中有多余未使用的磁盘空间)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n      //新建分区
Command action
   e   extended
   p   primary partition (1-4)
p     //创建主分区
Partition number (1-4): 2   //sdb硬盘的第二个主分区
First cylinder (8193-10240, default 8193):   //从硬盘哪个柱面开始,enter键默认(可以写对应数字)
Using default value 8193
Last cylinder, +cylinders or +size{K,M,G} (8193-10240, default 10240): //从硬盘哪个柱面开始,enter键默认

Using default value 10240

Command (m for help): p      //查看改变后分区情况,P:打印分区信息

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
64 heads, 32 sectors/track, 10240 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb92c060c

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        8192     8388592   83  Linux
/dev/sdb2            8193       10240     2097152   83  Linux

Command (m for help): t      //开始修改/dev/sdb2的属性,改为swap id:82
Partition number (1-4): 2    //选择要修改的分区
Hex code (type L to list codes): 82  //swap分区Id
Changed system type of partition 2 to 82 (Linux swap / Solaris)

Command (m for help): p  //查看改变后分区情况

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
64 heads, 32 sectors/track, 10240 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb92c060c

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        8192     8388592   83  Linux
/dev/sdb2            8193       10240     2097152   82  Linux swap / Solaris   //这里被改成swap属性

Command (m for help): w        //保存修改
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@localhost /]# mkswap /dev/sdb2
/dev/sdb2: No such file or directory   //修改后需要重启

[root@localhost /]# reboot    //重启

[root@localhost ~]# fdisk -l

[root@localhost ~]# mkswap /dev/sdb2   //格式化swap分区

Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=2d26d5ad-4ee2-4c1d-b90c-7cf4060fd9a9
[root@localhost ~]# swapon /dev/sdb2    //启用swap
[root@localhost ~]# free  -m    // 查看swap容量是否变大,判断操作是否成功
             total       used       free     shared    buffers     cached
Mem:          1878        348       1529          0         18        156
-/+ buffers/cache:        173       1704
Swap:         6079          0       6079[root@localhost ~]# vi /etc/fstab
/dev/sdb2 swap swap defaults 0 0   //使系统开机时自启用,在文件最后添加一行


###########################################################

2.用文件作为Swap分区
(紧随上面的信息做如下操作)
[root@localhost ~]# free -m   //查看初始信息
             total       used       free     shared    buffers     cached
Mem:          1878        348       1529          0         18        156
-/+ buffers/cache:        173       1704
Swap:         6079          0       6079

#创建要作为swap分区的文件:增加1GB大小的交换分区,则命令写法如下,其中的count等于想要的块的数量(bs*count=文件大小)。
[root@localhost ~]# dd if=/dev/zero of=/root/swapfile bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 12.1908 s, 88.1 MB/s
[root@localhost ~]# mkswap /root/swapfile   //格式化为交换分区文件
mkswap: /root/swapfile: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=3330e86b-ec5a-4423-bc08-07bcc3785495
[root@localhost ~]# swapon /root/swapfile     //启用交换分区文件
[root@localhost ~]# free -m   //查看修改后的信息,判定操作是否成功
             total       used       free     shared    buffers     cached
Mem:          1878       1403        474          0         19       1180
-/+ buffers/cache:        204       1674
Swap:         7103          0       7103
[root@localhost ~]# vi /etc/fstab
/root/swapfile swap swap defaults 0 0   //使系统开机时自启用,在文件最后添加一行


3.删除swap空间

[root@localhost ~]# swapoff /root/swapfile
[root@localhost ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          1878       1403        475          0         19       1180
-/+ buffers/cache:        203       1674
Swap:         6079          0       6079
[root@localhost ~]#