Author Topic: How to add swap on CentOS server  (Read 4495 times)

0 Members and 1 Guest are viewing this topic.

How to add swap on CentOS server
« on: September 17, 2014, 11:31:53 AM »
Check for Swap Space

Before we proceed to set up a swap file, we need to check if any swap files have been enabled by looking at the summary of swap usage.

Code: [Select]
swapon -s
If nothing is returned, the summary is empty and no swap file exists.

Check Disk Space Usage
After we know that we do not have a swap file enabled, we can check how much space we have on the server with the df -h command.

Code: [Select]
df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda2        20G  7.0G   12G  39% /
tmpfs           246M     0  246M   0% /dev/shm
/dev/vda1       504M   38M  441M   8% /boot

Create and Enable Swap (512k = 512 MB)
Code: [Select]
sudo dd if=/dev/zero of=/swapfile bs=1024 count=512k
sudo mkswap /swapfile
sudo swapon /swapfile

Now you can see your swap using command
Code: [Select]
free -m
This file will last on the server until the machine reboots. You can ensure that the swap is permanent by adding it to the /etc/fstab file.

Code: [Select]
sudo nano /etc/fstab
Paste in the following line:
Code: [Select]
/swapfile          swap            swap    defaults        0 0

To prevent the file from being world-readable, you should set up the correct permissions on the swap file:
Code: [Select]
chown root:root /swapfile
chmod 0600 /swapfile
AntiDDoS Protection (web + mail)

Join our Development Team and get paid !

Services Monitoring & RBL Monitoring

Do you need Fast and FREE Support included for your CWP linux server?
Installation Instructions
Get Fast Support Here