[Updated July 26, 2020]: Change swapfile permission; Set swapfile in /etc/fstab.
Swap is a special area on your computer, which the operating system can use as additional RAM.
Starting with Ubuntu 17.04, the swap partition was replaced by a swap file. The main advantage of the swap file is easy resizing.
In the following example, we’ll extend the swap space available in the /swapfile from 4 GB to 8 GB.
-
- Turn off all swap processes
sudo swapoff -a
- Resize the swap
sudo dd if=/dev/zero of=/swapfile bs=1G count=8
if = input file
of = output file
bs = block size
count = multiplier of blocks - Change permission
sudo chmod 600 /swapfile
- Make the file usable as swap
sudo mkswap /swapfile
- Activate the swap file
sudo swapon /swapfile
- Edit /etc/fstab and add the new swapfile if it isn’t already there
/swapfile none swap sw 0 0
- Check the amount of swap available
grep SwapTotal /proc/meminfo
- Turn off all swap processes
Well, I answered my own question. I left entries for both the swap file and the swap partition in the fstab. When I restarted, both showed up as active, when I ran swapon -s. I’m not sure if having them both running concurrently could cause problems, so I commented out the entry for the swap partition and restarted. The box came back up with only the swap file running. That little 90mb swap partition can just sit there and gather dust. It doesn’t appear to be hurting anyone.
Hello. I recently moved onto an Ubuntu server v18.04 box, at a large webhost. The box was provisioned with a 90mb swap partition, which is really small and caused problems with a backup app. I walked through all of your commands and my server is now running a swap file, apparently bypassing the swap partition. The following lines are in the fstab:
# swap was on /dev/sda6 during installation
UUID=25678faa-bdb3-4a22-b3ae-e6ef02d200f2 none swap sw 0 0
I added the swapfile line under it, but what should I do with the swap partition entry? Should I delete it or just let it initialize and sit dormant? Will running a swapfile in this situation cause problems?
I really appreciate your information on resizing the swap space, BTW. It was well written and easy to follow.
Thank you for this. I’d been wanting to increase the swapfile size for a while, but every instruction set I found seemed to technical. Your instructions were clear, concise and did exactly what I needed. So thank you. π
You’re welcome!
I’m glad to hear that my blog post helped you.
My fstab on ubuntu 20.04 has these entries – did not change any, do I still need to change ?
I did follow all steps except any fstab change and this shows proper 8gb
grep SwapTotal /proc/meminfo
SwapTotal: 8388604 kB
# /etc/fstab: static file system information.
#
# Use ‘blkid’ to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
#
# / was on /dev/sdb3 during installation
UUID=0f2d2288-e7f1-4742-b53a-e466a50e91e7 / ext4 errors=remount-ro 0 1
# /home was on /dev/sdb4 during installation
UUID=b0651f1d-4dfd-46ff-9afd-964277a02e04 /home ext4 defaults 0 2
# swap was on /dev/sdb2 during installation
UUID=2f49d3f7-8691-40d8-b7a0-eaf71de5abf4 none swap sw 0 0
Hi Ionut,
I don’t think you need to change anything.
But you can check what type of swap is active (file or partition) with the “swapon -s” command. “-s” meaning summary.
This is what I get when I run this command on a machine with 4 GB of swap:
$ swapon -s
Filename Type Size Used Priority
/swap/file file 4194304 0 -2
[email protected]:~$ /swapfile none swap sw 0 0
bash: /swapfile: Permission denied
swap is created but why show is permission denied when type /swapfile non swap sw 0 0 ?
$ sudo vim /etc/fstab
(you can use any editor instead of vim)
and then paste this line
/swapfile none swap sw 0 0
Thanks Bogdan! I like your tutorial the best! Use it as a handbook!
Thank you!
I got issue in step 4, after running: sudo swapon /swapfile
I got: swapon: /swapfile: read swap header failed
Any suggestion?
Did you run “sudo mkswap /swapfile”? Have you got any errors from this command?
A life saver thanks.
Thank you Bogdan beautiful instructions. It worked out
(only part confusied me is Edit /etc/fstab and add the new swapfile if it isnβt already there /swapfile none swap sw 0 0 I skipped it)
Thanx
Thank a lot, i used all my 32gb in GNS3 and i needed more ram.
Cheers
Hi Bogdan!
Thank you for the insights, it worked fine. However, now
$sudo hibernate
stopped working . No error msg, I just get back to my desktop.
Would you have any idea how to resolve that? I’m using Ubuntu 20.04
Thank you!
Hi Daniel,
I would look for more information into the system logs.
If you’re using Gnome, you can open the “Logs” program and search there for anything related to “hibernate”, “sleep” or “acpi”.
You could also try the command line way:
sudo vim /var/log/dmesg
orsudo vim/var/log/syslog
Thank you.
Hi Bogdan
Thanks for this. I created the swapfile successfully with 20GB but I notice there are now two swapfiles – the 20GB one and the original one created by the OS (4GB). From the fstab file I can see two entries. The second on was entered by myself. Would this cause any issues? Thanks.
/dev/mapper/cryptswap none swap defaults 0 0
/swapfile none swap sw 0 0
When I run sudo swapon -s, it shows the /swapfile type as ‘file’ and the ‘/dev/dm-0’ type as ‘partition’.
Edwin
Hi Edwin,
From what I can see, the OS created an encrypted swap partition. This is most likely because you choose to encrypt your Ubuntu installation. In this case, you can use a partition tool such as GParted or Disks (from Gnome) to resize the swap partition.
I haven’t verified this, but I don’t think the swap file you created is being used if you also have the encrypted one.
Thank you. It simply and elegantly worked π
does this same process apply for swap partition?
Hi Meet,
Steps 1, 4, 5, 6 and 7 are also applicable to a swap partition.
You will need a partition editor program such as GParted to create the swap partition.
When adding the partition to fstab, you’ll need to add it by UUID (instead of just using “/swap”) which you can find out by running the “blkid” command.
Thank you for your clear explanation, I could resolve few issues in my OS by extending Swap memory.
Thank you, I’m glad to hear that.
yes the fstab works for persistent! thanks!
This tutorial is awesome! I finally updated my swap memory to 32Gb on Ubuntu 20.04 and now it match with my 16Gb RAM memory. Thanks for sharing this, very helpful
Thank you!
Glad I could help π
Thanks for the clear commands , It’s working my machine!
In step 2, I got the following error:
DD: Memory Exhausted by Input Buffer of Size Bytes
This can be fixed by using this command instead:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2000
(2000 = 2GB)
Hi Bogdan,
thanks for the post. The changes appear not to be persistent with a reboot. How can we make them persistent?
Thanks
Hi Enrique,
Can you please add this line at the end of the /etc/fstab file?
/swapfile none swap sw 0 0
Please let me know if this works for you and I’ll update the article.
Thank you,
Bogdan