Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've deleted my existing swap partition due to some partitioning problem. I don't have a swap space now so I've created a swap file with:

dd if=/dev/zero of=/root/myswapfile bs=1M count=1024

Here's after swapon -s:

/root/myswapfile    file    1048572    1320    -1

Now I want to edit my /etc/fstab to enable the swap file after reboot.

share|improve this question
Swap on SSD is dangerous cause SSD drives have low duration on intense write operations. This means you can really consume your precious and expensive SSD drive like that. – user109611 Nov 21 '12 at 1:19
Notice that bs=1G count=1 would give the same result, have a look at the dd manpage for the other units. – LiveWireBT Dec 20 '14 at 6:02

These ares the steps to create a swap on a file:

sudo mkdir -p /var/cache/swap/   # create a driectory that holds the swap file
sudo dd if=/dev/zero of=/var/cache/swap/myswap bs=1M count=4096 # for 4 GByte
sudo chmod 0600 /var/cache/swap/myswap # only root can access
sudo mkswap /var/cache/swap/myswap # format as swap
sudo swapon /var/cache/swap/myswap # announce to system

Insert the following line in /etc/fstab for swap from the next boot:

/var/cache/swap/myswap    none    swap    sw    0   0

Note: In case you have your system files on a SSD you may want to consider to hold your swap file on a hard disk location.

Also note: You can not use a swap file for hibernation (see Ubuntu SwapFaq)

share|improve this answer
Why wouldn't you want your swap on the SSD as well? – Joseph Garvin Sep 22 '12 at 4:25
@JosephGarvin: as wear-out may probably not be a problem you still waste a lot of (expensive) disk space when having swap on SSD. Depending on your system RAM you may not need swap too often. – Takkat Sep 22 '12 at 7:00
Thanks, how would you check that the /etc/fstab would mount the swap file correctly without rebooting? I can't see the swap file if I do mount -a && mount – user72464 Oct 16 '15 at 10:17
@user72464 To show all swap used we can issue swapon -s. Available swap is also displayed on free -h. – Takkat Oct 16 '15 at 10:39
Yep, thanks, but how can I make sure that the swap will be actually loaded at boot, and my fstab is not corrupt? – user72464 Oct 16 '15 at 11:01

protected by Eric Carvalho Jul 13 '15 at 22:14

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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