Site icon Colonel Server – Infrastructure Server Hosting

How to create swap space in Linux operating system

swap 1

Swap Space in Linux: What It Is and How to Create It on CentOS

Swap space in Linux functions similarly to virtual memory in Windows, acting as an extension of physical RAM when the system runs low on memory. Instead of allowing the operating system to crash or hang when RAM is fully utilized, swap space temporarily holds inactive data by using part of the hard drive. While swap is much slower than RAM (even on SSDs or NVMe drives), it plays a crucial role in maintaining system stability under high memory pressure.

How Much Swap Space Should You Allocate?

There’s no strict rule, but here are some common recommendations:

Preliminary Steps Before Proceeding

Creating Swap Space on CentOS

  1. Log in as Root Access your CentOS system as the root user or with sudo privileges.
  2. Create Swap File Use the following command to allocate 8GB of disk space to swap:
dd if=/dev/zero of=/swapspace bs=1024 count=8388608

Explanation:

  1. Set Ownership to Root
chown root:root /swapspace
  1. Set Permissions
chmod 0600 /swapspace
  1. Format as Swap
mkswap /swapspace
  1. Activate Swap
swapon /swapspace
  1. Make Swap Persistent Open the /etc/fstab file:
nano /etc/fstab

If nano is not installed, run: yum install nano

Add the following line at the end of the file:

/swapspace none swap sw 0 0

Reboot the System

Now reboot to ensure changes are applied:

reboot

Verify Swap Activation

After reboot, use the following command to check swap:

free -m

You will see the RAM and swap usage stats.

Need Expert Help?

If you’d prefer to leave server management to the pros, purchase a managed VPS. Hosting providers like Mizbanfa offer full server support including swap configuration, monitoring, and optimization.

Exit mobile version