≡ Menu

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

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
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Tanmay August 18, 2010, 6:42 am

    Nice article.
    I tried to run the swapon command on RHL, but it says command not found! I am not sure which package would be good to install this functionality. Or any one can provide me the equivalent command for RHL.

    Thanks,
    Tanmay

  • Rajesh Kumar August 18, 2010, 6:50 am

    How can i use my Nokia 6303c in Red Hat Linux to access the GPRS?How to make dial up connection in linux for Mobile modem?

  • Wing Loon August 18, 2010, 9:33 pm

    Tanmay, make sure util-linux is installed and you will get swapon in Red Hat.

  • Tanmay August 19, 2010, 6:43 am

    @Wing Loon: Thanks for your comment. It seems util-linux is not there on my box. I’ll install that. Thanks again.

    Tanmay

  • Binu December 26, 2011, 12:46 am

    It is very clearly explaiend and I can say it is SMART

    Thanks

    Bin

  • Vipul Mishra April 3, 2012, 8:31 am

    This article really very helpful

  • albertobosia April 25, 2012, 2:58 am

    thanks, this helped me a lot!

  • Johnny Yoon May 29, 2012, 12:33 am

    Thank you so much !! It was very helpful.

  • Eric June 12, 2012, 8:58 am

    Good job whoever did this, I was stuck with no space to add partitions to allocate swap

  • ayyaz July 11, 2012, 12:28 am

    thanks for article it helped.

  • mike lucas September 28, 2012, 9:55 am

    i was experience hi server load because on mysqld and server crashed once bcoz of going out of my 8gb ram and 3 gig of swap now i have created swap file of 20 gb , will c how it handles now.,

    thanks a lot for this article.

  • Ashutosh November 24, 2012, 12:10 pm

    Very nicely explained the Linux Swap space adding topic !
    Keep doing good jobs!
    Thanks a ton.

  • Padmahasa January 6, 2013, 8:22 am

    Hi, thanks for both of the methods. And i was interested in 2nd method. And i’m new to ubuntu. During the time of installation i had no idea why swap space is required. So i installed without swap space and i tried to create swap file in /root directory as u instructed in 2nd method. But the problem is while using mkswap command i got this error

    root@padmahasa-desktop:/root# mkswap /root/myswapfile
    Setting up swapspace version 1, size = 1048572 KiB
    no label, UUID=569c3355-e718-46c9-82f8-f2d2d6ef59a8

    and i ignored and continued with next steps. It has created swapfile but when i tried to hibernate it showed an error stating

    cannot get swap device try swapon -a
    cannot get swap writer

    Please help me. I just want my system to put into hibernate successfully. Please help me.

  • Liju January 28, 2013, 2:47 am

    [root@ip-10-248-13-2 uploads]# free -m
    total used free shared buffers cached
    Mem: 594 425 169 0 51 260
    -/+ buffers/cache: 113 481
    Swap: 0 0 0

    [root@ip-10-248-13-2 uploads]# mkswap -f /dev/xvdf
    Setting up swapspace version 1, size = 1048572 KiB
    no label, UUID=5c6d68ca-52ea-4e9c-ab64-136025a7bfa0
    [root@ip-10-248-13-2 uploads]# swapon /dev/xvdf
    [root@ip-10-248-13-2 uploads]# free -m
    total used free shared buffers cached
    Mem: 594 485 109 0 51 261
    -/+ buffers/cache: 172 421
    Swap: 1023 0 1023
    [root@ip-10-248-13-2 uploads]#

  • Suresh Nagar February 27, 2013, 6:37 am

    Really helpful articles……….

  • Padmahas February 27, 2013, 11:11 pm

    Hi, Thanks for both of the above methods. But since at the time of istallation i don’t know the use of swap partition, i installed ubuntu 12.10 without swap partition. I want to hibernate without creating swap partition but only using swap file. I used second method to create swapfile and got this error while using mkswap

    root@padmahasa-desktop:~# mkswap /root/myswapfile
    Setting up swapspace version 1, size = 1048572 KiB
    no label, UUID=cc477ba5-e2d9-495c-a63b-300c4e5e0048

    and each time UUID differs. But i ignored and continued with next steps and when i tried to hibernate my system i got these errors
    PM: swap header not found
    cannot get swap device try swapon -a
    cannot get swap writer

    I just want to hibernate my system only using swapfile. Is it possible to do it…. Please help me. I’m new to ubuntu.

  • Anonymous March 20, 2013, 8:02 pm

    Ramesh:

    Great information! However, I started out with 2 gigs of memory and added 8 gigs. With that much memory, do I really need swap space?

    Gene

  • Vahid September 10, 2013, 4:45 am

    Great information, Thank you .

  • Hammad September 14, 2013, 4:15 pm

    Thank you!

    Very well put.

  • rameshbabu September 26, 2013, 3:33 pm

    Hi Thanks,
    for the blog, It is really helpful. Everthing work great, untill I reboot or “sudo swapoff -a” “sudo swapon -a”. How do I make it permanent

    -bash-4.1$ free -k
    total used free shared buffers cached
    Mem: 1012548 335500 677048 0 18864 143952
    -/+ buffers/cache: 172684 839864
    Swap: 5177328 0 5177328

    —–
    -bash-4.1$ sudo swapoff -a
    -bash-4.1$ sudo swapon -a
    -bash-4.1$ free -k
    total used free shared buffers cached
    Mem: 1012548 333268 679280 0 18888 143952
    -/+ buffers/cache: 170428 842120
    Swap: 2031608 0 2031608

    it falls back on 2gb..

    Thanks
    rb

  • Bindu November 10, 2013, 12:04 am

    Every thing works fine but after swapoff command swapon shows no directory found, how can I make the changes to reflect even after the swapon

    [root@localhost thor_client]# swapon -s
    Filename Type Size Used Priority
    /dev/mapper/vg-lv_swap partition 35028988 180724 0
    /root/myswapfile file 30744572 0 -1
    [root@localhost thor_client]# free -k
    total used free shared buffers cached
    Mem: 32940424 30082712 2857712 0 58732 28536156
    -/+ buffers/cache: 1487824 31452600
    Swap: 65773560 180724 65592836
    [root@localhost thor_client]# swapoff -a
    [root@localhost thor_client]# swapon -a
    swapon: root/myswapfile: stat failed: No such file or directory

    Thanks
    Bindu

  • Khang December 25, 2013, 9:27 pm

    Great article.
    Many Thanks!

  • Maxx January 4, 2014, 9:55 pm

    Really neat tips and advice.. Very useful. Keep up the good work

  • David January 29, 2014, 7:41 am

    Quick, concise and easy to follow guide.
    Most useful. Thanks.

  • Rajeev April 6, 2014, 12:17 am

    It is very easy to understand ….
    thanks for this post…

  • posinath April 15, 2014, 6:35 am

    Excellent

  • nozgi May 9, 2014, 1:36 pm

    excellent article. Thanks for posting this great tip. You saved the day!!

  • alper c May 30, 2014, 3:22 am

    Thanks so much, It helped me to allocate more memory for windows azure which comes with zero swap space and 768ram unreasonably

  • dil June 21, 2014, 11:27 am

    Thank you so much sir!

  • yasaman July 24, 2014, 12:10 am

    Thank you very much!
    It was really clean and worked for me! 🙂

    Thanks

  • Sourav November 11, 2014, 3:54 am

    thanks a lot

  • Vimal February 3, 2015, 4:24 am

    Thanks boss…

  • Pallavi June 25, 2015, 10:23 pm

    Very useful article

  • bharat October 13, 2015, 1:26 pm

    Thanks that worked

  • Anonymous January 6, 2016, 5:12 pm

    Awesome! It is great you easily simplifies it.

  • mubeen Mohammed April 6, 2016, 9:38 am

    Thanks for very good steps. 100% working fine. Good Job. Thanks for posting.

  • Sara May 25, 2016, 4:44 am

    Good one. Thanks

  • aryashree June 18, 2016, 12:26 am

    [root@localhost /]# free -m
    total used free shared buffers cached
    Mem: 3724 3468 256 0 7 36
    -/+ buffers/cache: 3424 299
    Swap: 89106 1872 87234

    i want to delete the swap partition but getting issue while deleting .

    [root@localhost /]# swapoff -a
    swapoff: /root/myswapfile: swapoff failed: Cannot allocate memory

    Actually i have created more swap space and i want to reduce of the swap memory.

  • Ketan Patel March 9, 2017, 7:42 pm

    Excellent article, pretty concise information. It has help me in several instances. I have bookmarked this article and go through it quite often as needed. Keep it up!

  • TrungDung April 4, 2017, 1:59 am

    Hi AMESH ,
    Which method is better performance or be the same? If method 1 (or 2) is better, could you explain why it is ?

    Thank you !