Security

How to Create SWAP Memory on Linux

A swap file (or swap space or, in Windows NT, a pagefile) is a space on a hard disk used as the virtual memory extension of a computer’s real memory (RAM). Having a swap file allows your computer’s operating system to pretend that you have more RAM than you actually do

So here it is a simple tutorial you can follow. The most common practice is to add half of your existing RAM up to 4GB. For example: if you have 1GB actual RAM allocated to your vps, then you can create about 512MB swap file.

The simple calculation is:

1024 block size x 512MB = 524288 block size. Now add that to the command:

[root@server ~]# dd if=/dev/zeroof=/swapfilebs=1024 count=524288

Next, create a swap file area using this command syntax:

[root@server~ ]# mkswap /swapfile

Define the owner of the newly created swap file and swap area then set correct permission on it:

[root@server~ ]# chown root:root /swapfile
[root@server~ ]# chmod 0600 /swapfile

Activate Swap you newly setup using this command:

[root@server~ ]# swapon /swapfile

Finally, issue command below to verify that Swap has been setup and enabled:

[root@server~ ]# swapon -s

The last thing to do is to configure Swap to run and enabled each time your server reboots. Use Nano editor (or vi) to adjust fstab setting:

[root@server~ ]# vi /etc/fstab

then add following line at the very bottom

[root@server~ ]# /swapfile none swap sw 0 0

From here on, each time you check your RAM usage using free -m command, it should display your swap too.

Leave a Reply

Your email address will not be published. Required fields are marked *