UNIX / Linux: 2 Ways to Add Swap Space Using dd, mkswap and swapon

by RAMESH NATARAJAN on AUGUST 18, 2010
 

Question: I would like to add more swap space to my Linux system. Can you explain with clear examples on how to increase the swap space?

Answer: You can either use a dedicated hard drive partition to add new swap space, or create a swap file on an existing filesystem and use it as swap space.

How much swap space is currently used by the system?

Free command displays the swap space. free -k shows the output in KB.

# free -k
total used free shared buffers cached
Mem: 3082356 2043700 1038656 0 50976 1646268
-/+ buffers/cache: 346456 2735900
Swap: 4192956 0 4192956

Swapon command with option -s, displays the current swap space in KB.

# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 4192956 0 -1

Swapon -s, is same as the following.

# cat /proc/swaps
Filename Type Size Used Priority
/dev/sda2 partition 4192956 0 -1

Method 1: Use a Hard Drive Partition for Additional Swap Space

If you have an additional hard disk, (or space available in an existing disk), create a partition using fdisk command. Let us assume that this partition is called /dev/sdc1

Now setup this newly created partition as swap area using the mkswap command as shown below.

# mkswap /dev/sdc1

Enable the swap partition for usage using swapon command as shown below.

# swapon /dev/sdc1

To make this swap space partition available even after the reboot, add the following line to the /etc/fstab file.

# cat /etc/fstab
/dev/sdc1 swap swap defaults 0 0

Verify whether the newly created swap area is available for your use.

# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 4192956 0 -1
/dev/sdc1 partition 1048568 0 -2 # free -k
total used free shared buffers cached
Mem: 3082356 3022364 59992 0 52056 2646472
-/+ buffers/cache: 323836 2758520
Swap: 5241524 0 5241524

Note: In the output of swapon -s command, the Type column will say “partition” if the swap space is created from a disk partition.

Method 2: Use a File for Additional Swap Space

If you don’t have any additional disks, you can create a file somewhere on your filesystem, and use that file for swap space.

The following dd command example creates a swap file with the name “myswapfile” under /root directory with a size of 1024MB (1GB).

# dd if=/dev/zero of=/root/myswapfile bs=1M count=1024
1024+0 records in
1024+0 records out # ls -l /root/myswapfile
-rw-r--r-- 1 root root 1073741824 Aug 14 23:47 /root/myswapfile

Change the permission of the swap file so that only root can access it.

# chmod 600 /root/myswapfile

Make this file as a swap file using mkswap command.

# mkswap /root/myswapfile
Setting up swapspace version 1, size = 1073737 kB

Enable the newly created swapfile.

# swapon /root/myswapfile

To make this swap file available as a swap area even after the reboot, add the following line to the /etc/fstab file.

# cat /etc/fstab
/root/myswapfile swap swap defaults 0 0

Verify whether the newly created swap area is available for your use.

# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 4192956 0 -1
/root/myswapfile file 1048568 0 -2 # free -k
total used free shared buffers cached
Mem: 3082356 3022364 59992 0 52056 2646472
-/+ buffers/cache: 323836 2758520
Swap: 5241524 0 5241524

Note: In the output of swapon -s command, the Type column will say “file” if the swap space is created from a swap file.

If you don’t want to reboot to verify whether the system takes all the swap space mentioned in the /etc/fstab, you can do the following, which will disable and enable all the swap partition mentioned in the /etc/fstab

# swapoff -a

# swapon -a

UNIX / Linux: 2 Ways to Add Swap Space Using dd, mkswap and swapon的更多相关文章

  1. How To Add Swap Space on Ubuntu 16.04

    Introduction One of the easiest way of increasing the responsiveness of your server and guarding aga ...

  2. Linux 文件系统错误的修复方法 ddrescue替代dd的恢复软件 备用超级块

    Linux 文件系统错误的修复方法  ddrescue替代dd的恢复软件  备用超级块 最近处理的一件 linux 服务器断电导致文件系统启动后文件系统不可读写,数据不可用的案例,现总结下 Linux ...

  3. Linux交换空间(swap space)

    每次安装Linux的时候,都会要求配置交换分区,那么这个分区是干嘛的呢?不设置这个分区有什么后果?如果一定要设置,设置多大比较合适?本篇将试图回答这些问题并尽量覆盖所有swap相关的知识. 下面的所有 ...

  4. Linux交换空间(swap space)的那些优缺点

    下面的所有例子都在ubuntu-server-x86_64 16.04下执行通过 什么是swap? swap space是磁盘上的一块区域,可以是一个分区,也可以是一个文件,或者是他们的组合.简单点说 ...

  5. 50个最常用的UNIX/Linux命令

    转自http://get.jobdeer.com/493.get 1. tar command examples Create a new tar archive. $ tar cvf archive ...

  6. How To Add Swap on Ubuntu 14.04

    https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04 How To Add Swap on ...

  7. [转载]How To Add Swap on Ubuntu 12.04

    How To Add Swap on Ubuntu 12.04 Aug 17, 2012  Linux Basics Ubuntu   About Linux Swapping Linux RAM i ...

  8. Zabbix报告无交换内存主机 Lack of free swap space on xxxxx

    [root@xx ~]# free -m total used free shared buffers cached Mem: 3832 3488 343 0 267 2389 -/+ buffers ...

  9. 10+ commonly using find command switches with example Unix/Linux

    http://crybit.com/find-command-usage-with-example-unixlinux/ find command is one of the best search ...

随机推荐

  1. 最全的dedeCMS标签调用技巧和大全

    1. 页面php方法获取字段  $refObj->Fields['id']; 2. 在页面上使用PHP连接数据库查询 {dede:php} $db = new DedeSql(false); $ ...

  2. 扫描二维码跳转app

    1.判断是否安装了app <html> <head> <meta name="viewport" content="width=device ...

  3. WSDL相关文档

    http://msdn.microsoft.com/en-us/library/ms996486.aspx http://msdn.microsoft.com/en-us/library/aa4685 ...

  4. ExtJS与jQuery的一点细节上的对比

    首先说明这不是一篇完整解读ExtJS和jQuery所有方面差异的文章,只是针对我个人刚看了两天的jQuery产生的一些疑问的整理.之前用过一段时间ExtJS,了解ExtJS的一些机制.现在做移动开发, ...

  5. D3D11 Debug Layer的bug

    在开发D3D应用程序时,我们会使用Debug Layer来调试应用程序,以确保我们的程序在最终发布时没有warnings和errors.不过最近在开发应用程序时遇到了这样的问题,就是我把多个网格模型的 ...

  6. JobClient学习------作业提交与初始化

    public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); ...

  7. 2014多校第三场1005 || HDU 4891 The Great Pan(模拟)

    题目链接 题意 : 给你n行字符串,问你有多少种理解方式.有两大类的理解 (1){A|B|C|D|...}代表着理解方式可以是A,可以是B或C或者D. (2)$blah blah$,在$$这两个符号中 ...

  8. Linux网络编程9——对TCP与UDP的简易封装2.0

    具体生成动态库的操作及使用该动态库的操作请参见上篇博文.以下仅仅列出改进版本的代码. 代码 my_socket.h #ifndef __MY_SOCKET_H__ #define __MY_SOCKE ...

  9. jmeter if 控制器

    判断变量值是不是为空(有没有被赋值): "${jd_aid}"!="\${jd_aid}"

  10. skip list

    概述 Skip list是平衡树的一种替代的数据结构,但是和红黑树不相同的是,跳表对于树的平衡的实现是基于一种随机化的算法的,这样也就是说跳表的插入和删除的工作是比较简单的.并且是Redis.Leve ...