How to Create and Configure Swap Space on Debian/RHEL

1. Check Existing Swap

swapon -s    # active swap
free -m      # memory & swap usage

2. Check Storage

df -h

3. Create Swap File

sudo dd if=/dev/zero of=/swapfile bs=1M count=4096   # 4GB
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Verify:

swapon -s
free -m

4. Make Swap Permanent

grep -qxF '/swapfile swap swap sw 0 0' /etc/fstab || echo '/swapfile swap swap sw 0 0' | sudo tee -a /etc/fstab

5. Optional Tuning

Swappiness (default 30 → set to 10):

sudo sysctl -w vm.swappiness=10
grep -qxF 'vm.swappiness=10' /etc/sysctl.conf || echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf

Cache Pressure (default 100 → set to 50):

sudo sysctl -w vm.vfs_cache_pressure=50
grep -qxF 'vm.vfs_cache_pressure=50' /etc/sysctl.conf || echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.conf

That's all! Thank you, you've learned how to create the vSWAP.

  • 10 Users Found This Useful
Was this answer helpful?

Related Articles

How to restrict direct root access in Linux

We can do it just in two steps. Step One: At first we will create new root user as follows (for...

How to extract .tar.gz files in Linux/UNIX OS

A tarball is a group of files that are bundled together using the tar command. Use the...

How to add welcome message when SSH start?

You need to change the contents of /etc/motd. Unfortunately, by default, /etc/motd is a link to...

How to change root password when SSH logged in

Run the following command: passwd Now type your new passwordOnce done, retype new passwordDone!...

How to install Pinguzo on any Linux/UNIX OS

Login to Pinguzo panel using Softaculous account or create an account of Pinguzo To add new...