Original source
Https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-centos-6
Check for swap spaces checking 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.
Before we start creating a swap file, we need to check to see if swap is already in use. See Summary through Swap
Swapon-s
If returned, the summary is empty and no swap file exists.
If nothing is returned, summary is empty and no swap file is displayed. Check the file system
After we know that we don't have a swap file enabled, we can check how much spaces we have on the server with the DF comma nd. The swap file would take 512mb-since we are only a using up about 7% of The/dev/hda, we can proceed.
After we're sure we don't have the swap file enabled, we can check how much space our server has. Use the DF command to view. The swap file will occupy 512MB. Because we're only using 7% of/dev/hda, so it's doable.
DF
filesystem 1k-blocks Used Available use% mounted on
/dev/hda 20642428 1347968 18245884 7%/
Create and Enable the Swap FileCreate and enable the swap file
Now it's time to create the swap file itself using the ' DD command:
It's time to create a swap file with the DD command.
sudo dd if=/dev/zero of=/swapfile bs=1024 count=512k
"Of=/swapfile" designates the file ' s name. In the, the name is Swapfile.
"Of=/swapfile" Specifies the name of the file, in this case the file name is Swapfile
Subsequently we are going to prepare the swap file by creating a Linux swap area:
We then initialize the swap file by creating the Linux swap area.
sudo mkswap/swapfile
The results display:
Execution results are
Setting up Swapspace version 1, size = 536866 KB
Finish up by activating the swap file:
Finally we activate the swap file
sudo swapon/swapfile
You'll then be able into the new swap file when you view the swap summary.
Now, when you look at the swap summary, you can see the new swap file.
Swapon-s
Filename Type Size Used Priority
/swapfile file 524280 0 -1
This file is last on the server until the machine reboots. You can ensure that the ' swap is ' permanent by adding it to the Fstab file.
This file will always exist until you restart. If you want to keep this file permanently, you can add the following line to the Fstab file
Open up the file:
Open Fstab File
sudo nano/etc/fstab
Paste in the following line:
Stick this line in here.
/swapfile swap defaults 0 0
To prevent the "file from being world-readable," you should set up the correct permissions on the swap file:
To prevent anonymous users from accessing this file, we need to set the permissions on the swap file
Chown root:root/swapfile
chmod 0600/swapfile