How to create or increase the swap file space for Debian or Red Hat

Overview

Here are bare minimal instructions on how to set up a swap file for Debian (e.g. Ubuntu Linux) and Red Hat based operating systems (e.g. AlmaLinux or CentOS).

The difference between Debian and Red Hat is that with Debian fallocate seems to always work and on Red Hat dd seems to work better.

In the beginning and in the end swapon command is used to test for the availability of the swap file. In the middle the vi text editor is used to append the requisite swap entries to fstab. If you’re working with AWS EC2, also see the reference pointing to their site.

It’s possible to use this entire procedure for Red Hat but you can’t do fallocate and you have to use dd instead.

Debian / Ubuntu Instructions

Below if you have 2GB RAM and you want to create the maximum size swapfile as per the table at the bottom of this article. On 4GB you could add a maximum of 8GB.

Go in as sudo -i

swapon --show
fallocate -l 4G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

vi /etc/fstab

Append or comment existing swap entry and then append this:

 /swapfile swap swap defaults 0 0

Check your work:

swapon --show
free -h

Red Hat derivatives e.g. AlmaLinux

fallocate doesn’t work well with Red Hat deritivates so use dd as per below.

Example is for 4 GB, but for 1 GB count = 1048576 so use a calculator and multiply by your desired size.

  • 1 GB = 1048576
  • 2 GB = 2097152
  • 3 GB = 3145728
  • 4 GB = 4194304

To assess the situation I like to use free -m or swapon --show

dd if=/dev/zero of=/swapfile bs=1024 count=1048576
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
vi /etc/fstab

Add this to fstab:

/swapfile swap swap defaults 0 0

Comment out the old swap file line.

Note about Red Hat and dedicated swap partitions

If you use vi to edit the /etc/fstab file, and you encounter the line below:

/dev/mapper/cl-swap swap swap defaults 0 0

That basically means the installation engineer created a separate swap partition for your installation. Although this is technically a more performant way of using swap, it completely breaks the ability to easily upgrade swap space. The reason for this is you would have to go and find the backend technology for the disk (e.g. the VM or physical host type) and then fiddle with complicated commands to enlare it. Also, if you just comment out that line and use the hard disk type swap option as per our advice, you will end up with an orphaned partition of wasted disk space.

The solution is too complicated to document here, as there are too many different types of disk backends, but you could use this document as a primer.

Note about existing swap file

There is a command swapoff

but the problem is you shouldn’t just use it unless you have a lot of time. See references why this is complicated, but in a nutshell once you run this command it’s going to transfer what’s in the current swap file to memory, and that’s a complicated process. It might just crash your machine or slow it down to FUBAR.

So it’s better just to create a new swap file, and then remove the other swap file later. Follow the above steps but use /swapfile2 as the name.

How Much Swap Is Needed*

*Adapted from the Ubuntu reference, removed hibernation column as Servers don’t hibernate

RAM       Recommended    Maximum
1GB          1GB          2GB
2GB          1GB          4GB
3GB          2GB          6GB
4GB          2GB          8GB
5GB          2GB          10GB
6GB          2GB          12GB
8GB          3GB          16GB
12GB         3GB          24GB
16GB         4GB          32GB
24GB         5GB          48GB
32GB         6GB          64GB
64GB         8GB          128GB
128GB        11GB         256GB
256GB        16GB         512GB
512GB        23GB         1TB
1TB          32GB         2TB
2TB          46GB         4TB
4TB          64GB         8TB
8TB          91GB         16TB

Caveats

Update: 10 December 2025

After many decades of setting up swap files, and many issues with heavy servers running out of RAM and even swap space, I feel compelled to add a caveats section with a huge disclaimer. Unfortunately, as with many things in systems administration, there is no magic bullet.

Some of the techniques that I use:

  • Don’t set up swap if you have a ton of RAM. This works well and save VMs abusing a disk.
  • However, even on a system with a ton of RAM, you’ll STILL see the swap file being used and depleted. Hence, this disclaimer.

To give myself a much deeper understanding as to why this happens, I did this AI prompt to ChatGPT 5.1 10 December 2025:

Please help me deduce if this system has optimal swap file?
root@host:~# free -m
               total        used        free      shared  buff/cache   available
Mem:           40107        4557         675         266       35659       35549
Swap:            979         979           0
What other commands do I check or is this good enough?

Let’s explore the complexity of the answer (DISCLAIMER: AI):

Your system is basically screaming, “I have almost 40 GB of RAM and I’m still using swap like it’s 2006.” Let’s decode what’s going on and how to check if things are behaving optimally.

You have a tiny swap (≈1 GB).

  • It is completely full.
  • You still have 35 GB of RAM available, so the kernel isn’t swapping because it needs to — it did it earlier and never reclaimed it. This is very common with default swappiness values.

Swap being full is not inherently bad if those swapped-out pages are truly cold, but it’s worth checking whether the system is under any memory pressure.

To get a clearer picture, you can check:

  1. vmstat -s

    This gives a snapshot of paging activity. If “pages swapped in/out” steadily grows while you have tons of free RAM, swap strategy might not be ideal.

  2. cat /proc/meminfo | egrep "SwapTotal|SwapFree|SwapCached"

    Shows whether swap contains cached junk or real pressure.

  3. vmstat 1 10

    Watching si (swap in) and so (swap out) in real time tells you if the system is actively swapping. If both stay at 0, you’re fine.

  4. cat /proc/sys/vm/swappiness

    Default on many distros is 60, which makes the kernel fairly eager to push infrequently used pages to swap long before RAM is full. On servers with big RAM, people often lower this to 10–20.

  5. top or htop

  6. Look at Swap: line to see if processes are actively using swap or if it’s static.

  7. dmesg | grep -i oom

    Just to rule out past Out-Of-Memory events.

Is this good enough?

What you have so far is a starting clue, but not enough to judge “optimal.” Nothing is currently wrong: no pressure, no free-RAM shortages, no active swapping. However, you can still tune for better behavior.

Rough rule of thumb for a 40 GB RAM server:

  • Lower swappiness to around 10

sudo sysctl vm.swappiness=10

(Persist it in `/etc/sysctl.conf`)

  • Consider increasing swap slightly (2–4 GB) if you want safer buffer space for rare spikes — but you don’t need huge swap with that much RAM.
  • If vmstat shows no active swapping, your system is effectively fine even if swap appears full.

If you want, you can show me the output of vmstat 1 5 and /proc/sys/vm/swappiness, and I can help interpret it with you.


This is there I draw the line. One can’t scale many systems if you have to do this kind of eval! Good luck! Let me know in the comments what you find.


References

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top