(this answer completely rewritten since the downvote)
Option 1 (my preference): delete the old swap file and create a new one of the correct size:
Rather than resizing the swap file, just delete it and create a new one at the appropriate size!
swapon --show # see what swap files you have active
sudo swapoff /swapfile # disable /swapfile
# Create a new 16 GiB swap file in its place (could lock up your computer
# for a few minutes if using a spinning Hard Disk Drive [HDD], so be patient)
sudo dd if=/dev/zero of=/swapfile count=16 bs=1G
sudo mkswap /swapfile # turn this new file into swap space
sudo chmod 0600 /swapfile # only let root read from/write to it, for security
sudo swapon /swapfile # enable it
swapon --show # ensure it is now active
In case you are adding this swap file for the first time, ensure it is in your /etc/fstab
file to make the swap file available again after each reboot. Just run these two commands:
# Make a backup copy of your /etc/fstab file just in case you
# make any mistakes
sudo cp /etc/fstab /etc/fstab.bak
# Add this swapfile entry to the end of the file to re-enable
# the swap file after each boot
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Source: see the "Step 4: Make the changes permanent" section here.
Option 2: resize the old swap file:
The accepted answer by @Ravexina is correct. However, initially I didn't understand all of its pieces, so I wanted to include some more descriptions and explain more of the details. See dd --help
and man dd
. Some of my learning on this comes from Bogdan Cornianu's blog post as well. I also add a few commands at the end to show how to verify your swap space once you create it.
How to resize swap file:
Here we will increase the size of the existing swap file by writing 8 GiB (Gibibytes) of zeros to the end of it.
Turn off usage of just this one swap file (located at "/swapfile"):
# Do this
sudo swapoff /swapfile
# NOT this, which unnecessarily disables all swap files or partitions
# sudo swapoff --all
# or
# sudo swapoff -a
Increase the size of the swap file by 8 GiB by appending all zero bytes to the end of it (rather than rewriting the whole file, which would be slower):
sudo dd if=/dev/zero of=/swapfile bs=1G count=8 oflag=append conv=notrunc
if
= input file
/dev/zero
= a special Linux "file" which just outputs all zero bytes every time you read from it
of
= output file
bs
= block size
- Here,
1G
stands for 1 Gibibyte, or GiB, which is the base-2 version of "Gigabyte, which is base-10. According to man dd
, G =1024*1024*1024
bytes. This is how I like to size files since computers and hardware memory are base-2.
- If you'd like to use 1 Gigabyte, or GB, which is the base-10 version of "Gibibyte", which is base-2, then you must instead use
1GB
rather than 1G
. man dd
shows that GB =1000*1000*1000
bytes.
count
= multiplier of blocks; the total memory written will be count * bs
.
oflag=append
means to append to the end of the output file, rather than rewriting the whole thing. See dd --help
and man dd
. From dd --help
:
append append mode (makes sense only for output; conv=notrunc suggested)
conv=notrunc
means when "converting" the file, "do not truncate the output file"; dd --help
, as you can see just above, shows this is recomended whenever doing oflag=append
Note: if you wanted to rewrite the whole swap file rather than just appending to it, you could create a 32 GiB swapfile like this, for example:
sudo dd if=/dev/zero of=/swapfile bs=1G count=32
Make the file usable as swap
sudo mkswap /swapfile
Turn on the swap file
sudo swapon /swapfile
(Bonus/Optional): ensure this swap file you just created is now in usage:
swapon --show
Sample output:
$ swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 64G 1.8G -2
You can also look at some memory/swap info with these two commands as well:
# 1. Examine the /proc/meminfo file for entries named "swap", such
# as the "SwapTotal" line
cat /proc/meminfo | grep -B 1000 -A 1000 -i swap
# 2. Look at total memory (RAM) and swap (virtual memory) used
# and free:
free -h
References:
- @Ravexina's answer
- Bogdan Cornianu's blog post here: https://bogdancornianu.com/change-swap-size-in-ubuntu/
- "How to Create and Use Swap File on Linux": https://itsfoss.com/create-swap-file-linux/
swapoff
, create a new /swapfile,mkswap
, andswapon -a
– heynnema Jun 21 '17 at 21:16swapoff
thendd
andmkswap
finallyswapon
. I thought you don't want to touch your file. – Ravexina♦ Jun 21 '17 at 21:29