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. nmap所有参数详解

    QQ:1258496116 端口:80http 443https 53dns 25smtp 22ssh 23telnet20.21ftp 110pop3 119nntp 143imap 179bgp ...

  2. Confluence Wiki Markup & Markdown

    Markup : 默认有支持 Markdown : 需先安装插件,插件下载地址: Confluence markdown : https://marketplace.atlassian.com/plu ...

  3. 浅谈ASP.NET报表控件

    OWC似乎使用者居多,但看见有网友在帖中抱怨OWC在使用时需要许可证书,于是将其排除,我可不想BOSS在看报表时弹出一个“没有许可证书”的窗口. 接着找到了ComponentOne的Web chart ...

  4. Ogre1.8.1 Basic Tutorial 6 - The Ogre Startup Sequence

    原文地址:http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Basic+Tutorial+6&structure=Tutorials 1. ...

  5. MySql计数器,如网站点击数,如何实现高性能高并发的计数器功能

    MySql计数器,如网站点击数,如何实现高性能高并发的计数器功能 Clicks: Date: -- :: Power By 李轩Lane TagMysql计数器高性能 现在有很多的项目,对计数器的实现 ...

  6. HDU 4493 Tutor(精度处理)

    题目 #include<stdio.h> int main() { int t; double a,s; scanf("%d",&t); while(t--) ...

  7. POJ 3320

    Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6496   Accept ...

  8. Wamp Mysql错误消息 语言设置

    Wamp Mysql错误消息 语言设置 http://my.oschina.net/wandershi/blog/264347 打开my.ini   找到 [wampmysqld] port = 33 ...

  9. VMware下Ubuntu与宿主Windows共享文件夹

    概述1.安装VMware Tool2.设置共享 步骤开始安装VMware Tool 显示如下画面(如果宿主无法访问外网,可能会出现一个更新失败,可以无视之) 通过下列命令解压.执行,分别是下面的tar ...

  10. (5)VS2010无法打开gl/glaux.h头文件的解决方法

    方法:下载缺少的文件. 参考自:http://blog.csdn.net/delphiwcdj/article/details/6326586 下载源1:CSDN下载OpenGL 安装包所需文件下载: ...