67

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.

Eric Carvalho
  • 54,385
jojo
  • 911
  • 2
  • 8
  • 9
  • 3
    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 06:02
  • 2
    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. –  Nov 21 '12 at 01:19

1 Answers1

100

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

Create a large file e.g. with

sudo mkdir -p /var/cache/swap/   # create a directory that holds the swap file
sudo dd if=/dev/zero of=/var/cache/swap/myswap bs=1M count=4096 # for 4 GByte

Of course any other method of creating a file of defined size would do.

Announce swap to the system

sudo chmod 0600 /var/cache/swap/myswap # only root should have 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 use a swap file for hibernation since 2021, but it takes some more work. You need to find the UUID of your root partition & offset of the swapfile and then add resume=UUID=xxx resume_offset=xxx as value of GRUB_CMDLINE_LINUX_DEFAULT in /etc/grub/default, possibly also initramfs (see [https://ubuntuhandbook.org/index.php/2021/08/enable-hibernate-ubuntu-21-10/] for details.)

Additional note for Ubuntu >= 17.04: A swap on file /swapfile is created by default in a new installation (when no swap partition was present). We can manually create a swap partition later if we prefer.

In case we want to replace an existing swap (e.g. partition) with another swap (e.g. on file) we need to remove the old swap with

sudo swapoff -a  # for all

Then remove the swap entry from /etc/fstab or replace it with the new swap respectively.

Takkat
  • 142,284
  • 3
    Why wouldn't you want your swap on the SSD as well? – Joseph Garvin Sep 22 '12 at 04:25
  • 1
    @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. http://askubuntu.com/questions/178661/do-i-need-swap-with-new-ssd – Takkat Sep 22 '12 at 07:00
  • 2
    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 – Don Giulio Oct 16 '15 at 10:17
  • 2
    @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? – Don Giulio Oct 16 '15 at 11:01
  • 1
    @user72464 Simple.. do mount -a – heemayl Oct 22 '15 at 10:13
  • it seems we can use a swap file for hibernation although I have troubles with it (but my machine is a basketcase and has trouble with everything) – Zanna Oct 20 '17 at 12:50
  • @Zanna: yeah hibernation on swap on file may have been intrroduced with 17.04 but I never tested it. It is my strong believe that hibernation is evil (I once had a machine that took ten times longer to wake up than it would have been needed to reboot). – Takkat Oct 20 '17 at 12:53
  • lol it is definitely not without peril – Zanna Oct 20 '17 at 13:07
  • @Takkat you still waste a lot of (expensive) disk space when having swap on SSD. Now just why would that be the case? A byte is a byte and takes up the same amount of space on both an HDD and an SSD. – Melab Oct 22 '20 at 16:22
  • @Melab I cannot second that statement since swap file is heavy on random reads and writes so page in and out from HDD will be much slower than using SSD which will impact entire system negatively. – igor Dec 10 '23 at 18:26