Is there a way to increase my existing "swapfile" without having to destroy and re-create it? I would like to up my swap space from 1GB to 2GB. Currently it is set up as such:

$ sudo swapon -s
Filename                Type        Size    Used    Priority
/swapfile               file        1048572 736640  -1
$ ls -lh /swapfile
-rw------- 1 root root 1.0G Nov  9  2016 /swapfile

I'm using Ubuntu 14.04.

  • How much RAM do you have? Is 2G enough? I think that you'll have to swapoff, create a new /swapfile, mkswap, and swapon -a – heynnema Jun 21 '17 at 21:16
  • 1
    Add a new swap file, follow the instruction of the above question. you can have 2 swap file ;) – Ravexina Jun 21 '17 at 21:16
  • 1
    @Ravexina, A newbie question perhaps, but why would I want to add a new swap file rather than increasing the size of the existing one? Or is it not possible to increase an existing swap file? – Dave Jun 21 '17 at 21:25
  • @Dave That's possible too, as you may know we can swapoff then dd and mkswap finally swapon. I thought you don't want to touch your file. – Ravexina Jun 21 '17 at 21:29
  • 2
    @Ravexina post that as answer – Sergiy Kolodyazhnyy Jun 21 '17 at 21:42

First disable swap file:

sudo swapoff /swapfile

Now let's increase the size of swap file:

sudo dd if=/dev/zero of=/swapfile bs=1M count=1024 oflag=append conv=notrunc

The above command will append 1GiB of zero bytes at the end of your swap file.

Setup the file as a "swap file":

sudo mkswap /swapfile

enable swaping:

sudo swapon /swapfile
| improve this answer | |
  • 5
    sudo fallocate -l 2G /swapfile is probably safer than dd (although it doesn't keep the original swapfile), and it also needs a sudo chmod 600 /swapfile. – heynnema Jun 21 '17 at 23:18
  • 11
    @heynnema Doesn't fallocate make sparse files? The swapon manpage says sparse swap files are problematic (specifically mentioning fallocate). – muru Jun 22 '17 at 1:37
  • 3
    @heynnema no, the mkswap manpage also says that the files should not contain any holes. – muru Jun 22 '17 at 1:46
  • 1
    @muru guess I'm wrong :-) Every time that I read how to create a /swapfile with the onset of 17.04 they used fallocate. I guess that we'll just have to use "disk destroyer"! – heynnema Jun 22 '17 at 1:50
  • 1
    @heynnema: What you could do is to use fallocate to pre-allocate disk space and then use dd to fill the holes with zeros. – David Foerster Jun 22 '17 at 9:49

You should add a new swapfile instead of resizing the exist one because it costs you nothing to do so. To resize a swapfile, you must first disable it, which evicts the swap contents to RAM, which increases pressure on RAM and may even summon the OOM killer (not to mention that you could possibly be thrashing your disks for several minutes). Multiple swap files are not a problem, it's trivially easy to setup yet another swap file. There's quite literally no benefit to resizing a swap file over adding another.

dd if=/dev/zero of=/some/file count=1K bs=1M
mkswap /some/file
sudo chown root:root /some/file
sudo chmod 600 /some/file
sudo swapon /some/file
| improve this answer | |
  • 1
    And does count=1K give a 1G file? count is in block size, yes? And that can be 512/4096? Or is my math wrong? – heynnema Jun 22 '17 at 2:06
  • 4
    +1 This approach also makes it easy to disconnect one of the swapfiles if you later decide you need the disk space back. – joeytwiddle Jun 22 '17 at 8:51
  • 3
    This really needs to be marked as the correct answer. Wow, bravo! – Beshoy Girgis Apr 27 '18 at 23:36
  • 2
    «because it costs you nothing to do so» Except for hibernation. «The suspend image cannot span multiple swap partitions and/or swap files. It must fully fit in one swap partition or one swap file.[» wiki.archlinux.org/index.php/Power_management/… – tuxayo Jan 6 at 19:23
  • 2
    Actual source: «Q: Does swsusp (to disk) use only one swap partition or can it use multiple swap partitions (aggregate them into one logical space)? A: Only one swap partition, sorry.» kernel.org/doc/Documentation/power/swsusp.txt – tuxayo Jan 6 at 19:26

You can create another swap file as i did:

  1. sudo fallocate -l 4G /swapfile
  2. sudo chmod 600 /swapfile
  3. sudo mkswap /swapfile
  4. sudo swapon /swapfile
  5. Verify it is working with sudo swapon --show
    To make it permanent add a file to the fstabfile typing:
    echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
| improve this answer | |
  • Please note that fallocateshould bit be used to create the file, as it creates a sparce file. see man mkswap – Charles Green Feb 22 '19 at 3:13
  • @CharlesGreen it worked me as i wrote, so it should work for almos anyone. – borekon Mar 10 '19 at 17:23
  • 1
    I would sugegst reading man mkswap, especially the last pargraph before "Environment" – Charles Green Mar 10 '19 at 19:23
  • 1
    worked on NanoPi Neo with Armbian v 5.65 – Izaac Corbett Apr 14 '19 at 20:47

I have good results on my Ubuntu 17.04 following the advice of Arian Acosta from the blogpost. One can substitute the 4G here sudo fallocate -l 4G /swapfile with any amount of gigabytes you want. For example sudo fallocate -l 2G /swapfile for TS.

Generally speaking, the recommended size for a swap file is 2X the amount of RAM, but you can make it as big as you need. Remember that this is not a substitute for memory because performance is much worse since things are stored in the disk.

I’ve created a simple bash script that increments the swap file to 4GB and tested it on Ubuntu 16.04.

This can be run line by line or a bash script, but I use it to make headless installations.

echo "====== Current Swap ======"
sudo swapon -s
echo "====== Turning Off Swap ======"
sudo swapoff /swapfile
echo "====== Allocating 4GB Swap ======"
sudo fallocate -l 4G /swapfile
echo "====== Making Swap ======"
sudo mkswap /swapfile
echo "====== Setting Permissions to Root Only  ======"
sudo chmod 600 /swapfile
echo "====== Turning On Swap ======"
sudo swapon /swapfile
echo "====== Current Swap ======"
sudo swapon -s
echo "====== Done! ======"
| improve this answer | |
  • 3
    You should have added an argument to the script, which is the amount of SWAP to be added and finally add it to fstab. But still, i like this – LnxSlck Nov 24 '18 at 10:48

You might also want to check permissions. Other way to do this:

# check your swap

# turn off swap
sudo swapoff /swapfile

# To create the SWAP file, you will need to use this.
sudo fallocate -l 4G /swapfile  # same as "sudo dd if=/dev/zero of=/swapfile bs=1G count=4"

# Secure swap.
sudo chown root:root /swapfile
sudo chmod 0600 /swapfile

# Prepare the swap file by creating a Linux swap area.
sudo mkswap /swapfile

# Activate the swap file.
sudo swapon /swapfile

# Confirm that the swap partition exists.
sudo swapon -s

# check your swap again
| improve this answer | |

General answer for "how to resize swap file":

From Bogdan Cornianu, here: https://bogdancornianu.com/change-swap-size-in-ubuntu/

[QUOTE (ALMOST VERBATIM, with just a few of my comment additions) START]

In the following example, we’ll extend the swap space available in the /swapfile from 4 GiB to 8 GiB.

  1. Turn off all swap processes

    sudo swapoff -a
  2. Resize the swap

    sudo dd if=/dev/zero of=/swapfile bs=1G count=8

    if = input file
    of = output file
    bs = block size; here, 1G stands for 1 Gibibyte, or GiB, which, according to man dd is G =1024*1024*1024 bytes
    count = multiplier of blocks

  3. Make the file usable as swap

    sudo mkswap /swapfile
  4. Activate the swap file

    sudo swapon /swapfile
  5. Check the amount of swap available

    grep SwapTotal /proc/meminfo


| improve this answer | |

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.