Tuesday, 14 May 2013

Adding Swap File under Linux


Adding Swap File under Linux
 If you are adding more memory on your dedicated server, you will need to add more swap space. You have two choices: add or upgrade a swap partition or create a swap file. Because changing the size or create a new swap partition is not easy, the best solution is to create a new swap file. Since the 2.6 Linux kernel, the swap files are just as fast as swap partitions.

RECOMMENDED SIZE OF LINUX SWAP.

The Recommended Swap file on Linux system should as per below instructions.
·  1 GB RAM —> 2 GB of Swap file
·  2 GB RAM —> 4 GB of Swap file
·  4 GB RAM —> 8 GB of Swap file
·  8 GB RAM —> 12 GB of Swap file
·  16 GB RAM —> 24 GB of Swap file
·  32 GB RAM —> 32 GB of Swap file

Create a swap file

To create a swap file, use the “dd” command to create an empty file. Next you need to use mkswap command to set up a Linux swap area on a device or in a file

1) Login as the root user in system.

2) Create and determine the size of the new swap file in megabytes and multiply by 1024 to determine the number of blocks. You can do this with the linux command “dd”. For example, the block size of a 1GB swap file is (1024 * 1024MB = 1048576 block size). Type following command to create 1GB swap file
# touch /swapfile
# dd if=/dev/zero of=/swapfile  bs=1024 count=1048576
Note: “swapfile” is the name of your swapfile. You need to create /swapfile file using touch command. File name could be as per your comfort.

3) Setup the swap file with the command: mkswap. Type following command to setup your swap file:
# mkswap /swapfile

4) To enable the swap file immediately but not automatically at boot time. Type:
# swapon /swapfile

5) To enable the new swap file automatically at the boot, you need to edit the file /etc/fstab and add the following line.
/swapfile swap swap defaults 0 0
The swap file will be enabled at each time the system boots.

6) You can verify the swap file is working fine with these commands:
# cat /proc/swaps
or
# free

2 comments:

  1. Thank you so much, following your guide was really straight forward :)

    ReplyDelete