How to create and enable a Swap File on Linux

Configurare noua (How To)

Situatie

A swap file acts as additional virtual memory when your physical RAM is full. It can prevent your system from crashing due to memory exhaustion, especially on servers or VPS instances with limited resources. While not as fast as RAM, swap gives breathing room to processes under memory pressure. Setting one up is useful on systems without a swap partition or where more swap space is needed temporarily.

Solutie

Step 1: Check If Swap Is Already Enabled

Run: swapon –show

If no output is returned, you have no swap active. Then check memory info:

free -h

Step 2: Create a 1GB Swap File

Use fallocate to quickly create a file:

sudo fallocate -l 1G /swapfile

Then set the correct permissions:

sudo chmod 600 /swapfile

Step 3: Format and Enable the Swap File

Step 4: Make Swap Persistent After Reboot

Edit /etc/fstab to enable the swap file at boot

echo ‘/swapfile none swap sw 0 0’ | sudo tee -a /etc/fstab

 Conclusion:  A swap file gives your Linux server extra memory to work with during peak loads. It’s especially useful on low-RAM VPS systems or when compiling large projects. With just a few commands, you can stabilize your system and reduce out-of-memory crashes.

Tip solutie

Permanent

Voteaza

(9 din 18 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?