How can I create a dedicated hard drive to do "swap"on a ubuntu 12.04 LTS?

1 Answer 1


You don't have to use a partition or hdd for swap.

Just create a swap file using dd or fallocate.

Create a file of filled with zeros 1GB in this case:

 dd if=/dev/zero of=/mnt/swap.img bs=1M count=1024

Preferred method: use fallocate instead of dd

NOTE: fallocate is much faster than dd because it quickly allocates blocks and mark them as uninitialized, no I/O to the blocks.

fallocate -l 1024M /mnt/swap.img

Format the file to create a swap device

mkswap /mnt/swap.img

Adding the swap to the running system:

swapon /mnt/swap.img

Make it permanent, edit /etc/fstab and add the following entry:

/mnt/swap.img  none  swap  sw  0 0

More information: https://help.ubuntu.com/community/SwapFaq

  • So @Terrywang, therefore if im creating a 4GB swap this is what is should look like sudo fallocate -l 4096m /mnt/4096MiB.swap sudo chmod 600 /mnt/4096MiB.swap sudo dd if=/dev/zero of=/mnt/4096MiB.swap bs=4M count=4096 sudo chmod 600 /mnt/4096MiB.swap sudo mkswap /mnt/4096MiB.swap sudo swapon /mnt/4096MiB.swap gksudo gedit /etc/fstab /mnt/4096MiB.swap none swap sw 0 0 Aug 1, 2013 at 21:14
  • If you use fallocate to generate the swapfile, NO you don't need to du it using dd again, because they do the same thing, diff is fallocate is much faster. Otherwise the steps look good.
    – Terry Wang
    Aug 1, 2013 at 21:24
  • Well I was successful in creating the swap file, which i most say is running quite well. @terrywang Aug 2, 2013 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.