UNIX / Linux: 2 Ways to Add Swap Space Using dd, mkswap and swapon

how to increase swap space in linux

Question: I would like to add more swap space to my Linux system. Can you explain with clear examples on how to increase the swap space?

Answer: You can either use a dedicated hard drive partition to add new swap space, or create a swap file on an existing filesystem and use it as swap space.

How much swap space is currently used by the system?

Free command displays the swap space. free -k shows the output in KB.

Swapon -s, is same as the following.

Method 1: Use a Hard Drive Partition for Additional Swap Space

If you have an additional hard disk, (or space available in an existing disk), create a partition using fdisk command. Let us assume that this partition is called /dev/sdc1

Now setup this newly created partition as swap area using the mkswap command as shown below.

Enable the swap partition for usage using swapon command as shown below.

To make this swap space partition available even after the reboot, add the following line to the /etc/fstab file.

Verify whether the newly created swap area is available for your use.

Note: In the output of swapon -s command, the

Type column will say “partition” if the swap space is created from a disk partition.

Method 2: Use a File for Additional Swap Space

If you don’t have any additional disks, you can create a file somewhere on your filesystem, and use that file for swap space.

The following dd command example creates a swap file with the name “myswapfile” under /root directory with a size of 1024MB (1GB).

Change the permission of the swap file so that only root can access it.

Make this file as a swap file using mkswap command.

Enable the newly created swapfile.

To make this swap file available as a swap area even after the reboot, add the following line to the /etc/fstab file.

Verify whether the newly created swap area is available for your use.

Note: In the output of swapon -s command, the Type column will say “file” if the swap space is created from a swap file.

If you don’t want to reboot to verify whether the system takes all the swap space mentioned in the /etc/fstab, you can do the following, which will disable and enable all the swap partition mentioned in the /etc/fstab

Source: www.thegeekstuff.com

Category: Forex

Similar articles: