Linux RAM is composed of chunks of memory called pages. To free up pages of RAM, a “Linux swap” can occur and a page of memory is copied from the RAM to preconfigured space on the hard disk. Linux swaps allow a system to harness more memory than was originally physically available.
However, swapping does have disadvantages. Because hard disks have a much slower memory than RAM, virtual private server performance may slow down considerably. Additionally, swap thrashing can begin to take place if the system gets swamped from too many files being swapped in and out.
Check for Swap Space
Before we proceed to set up a swap file, we need to check if any swap files have been enabled on the VPS by looking at the summary of swap usage.
Swapon –s
An empty list will confirm that you have no swap files enabled:
Filename Type size used Priority
/dev/sda1 partition 7834620 7092 -1
Check the File System :
After we know that we do not have a swap file enabled on the server, we can check how much space we have on the server with the df command. The swap file will take 512MB— since we are only using up about 8% of the /dev/sda, we can proceed.
root@sadeek:/# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda5 236082692 16924872 207339736 8% /
udev 1854004 4 1854000 1% /dev
tmpfs 758308 824 757484 1% /run
none 5120 0 5120 0% /run/lock
none 1895768 80 1895688 1% /run/shm
Create and Enable the Swap File :-
Now it’s time to create the swap file itself using the dd command:
dd if=/dev/zero of=/swapfile bs=1024 count=512k
“of=/swapfile” designates the file’s name. In this case the name is swapfile.
Subsequently we are going to prepare the swap file by creating a linux swap area:
mkswap /swapfile
The results display:
Setting up swapspace version 1, size = 524284 KiB
no label, UUID=9932827f-0478-4819-b428-8debb9c43c71
Finish up by activating the swap file:
swapon /swapfile
You will then be able to see the new swap file when you view the swap summary.
root@sadeek:/# swapon -s
Filename Type Size Used Priority
/dev/sda1 partition 7834620 7092 -1
/swapfile file 524284 0 -2
This file will last on the virtual private server until the machine reboots. You can ensure that the swap is permanent by adding it to the fstab file.
Open up the file:
vi /etc/fstab
Paste in the following line:
/swapfile none swap sw 0 0
To prevent the file from being readable, you should set up the correct permissions on the swap file:
chown root:root /swapfile
chmod 600 /swapfile
No comments:
Post a Comment