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. JAVA算法系列 冒泡排序

    java算法系列之排序 手写冒泡 冒泡算是最基础的一个排序算法,简单的可以理解为,每一趟都拿i与i+1进行比较,两个for循环,时间复杂度为 O(n^2),同时本例与选择排序进行了比较,选择排序又叫直 ...

  2. C++时间标准库时间time和系统时间的使用

    #include <iostream> #include <time.h> #include <stdio.h> #include <windows.h> ...

  3. PHP 性能分析第一篇: Xhprof & Xhgui 介绍

    [前言]这是国外知名博主 Davey Shafik所撰写的 PHP 应用性能分析系列的第一篇,阅读第二篇可深入了解 xhgui,第三篇则关注于性能调优实践. 什么是性能分析? 性能分析是衡量应用程序在 ...

  4. HDU 2671 Can't be easier(数学题,点关于直线对称)

    题目 //数学题//直线 y = k * x + b//直线 ax+by+c=0; 点 (x0,y0); 点到直线距离 d = (ax0+by0+c)/sqrt(a^2+b^2) /********* ...

  5. POJ 1658

    #include<iostream>//cheng da cai zi using namespace std; int main() { int i; int time; ]; cin& ...

  6. Good Bye 2015 C. New Year and Domino 二维前缀

    C. New Year and Domino   They say "years are like dominoes, tumbling one after the other". ...

  7. HDU 5596/BestCoder Round #66 (div.2) GTW likes math 签到

    GTW likes math  Memory Limit: 131072/131072 K (Java/Others) 问题描述 某一天,GTW听了数学特级教师金龙鱼的课之后,开始做数学<从自主 ...

  8. 解析Java中静态变量与实例变量的区别

    java类的成员变量有俩种:一种是被static关键字修饰的变量,叫类变量或者静态变量:另一种没有static修饰,为实例变量.      在语法定义上的区别:静态变量前要加static关键字,而实例 ...

  9. Axis2学习的第一天

    按照下面,分别建2个工程,一个client(客户端),一个server(服务端) 先实现服务端: 1.编写services.xml文件,该文件是放在aar文件里的\META-INF目录下的: < ...

  10. P2P金融

    P2P金融又叫P2P信贷,是互联网金融(ITFIN)的一种.意思是:点对点. P2P金融指不同的网络节点之间的小额借贷交易(一般指个人),需要借助电子商务专业网络平台帮助借贷双方确立借贷关系并完成相关 ...