Solved: How to add swap space in Redhat or Ubuntu Linux

In this post  we will see how we can add a file as swap space in Linux. Same steps are to be followed for Redhat and Ubuntu Linux.

Type the following command to create 100MB swap file (1024 * 100MB = 102400 block size):

dd if=/dev/zero of=/swap1 bs=1024 count=102400

Secure swap file

Setup correct file permission for security reasons, enter:

# sudo chown root:root /swap1
# sudo chmod 0600 /swap1

Set up a Linux swap area

Type the following command to set up a Linux swap area in a file:

# sudo mkswap /swap1

Activate /swap1 swap space :

# sudo swapon /swap1

Update /etc/fstab file to make it persistent across reboot.

vi /etc/fstab

Add the following line in file:

/swap1 swap swap defaults 0 0

To check if the swap file is added or not

Type the following swapon command:

#sudo swapon -s
Filename Type Size Used Priority
/dev/dm-0 partition 839676 0 -1
/swap1 file 102396 0 -2

It should show you the new file.

If you want add a logical volume for swap please refer how to add LV for swap .

How to add logical volume for swap in Redhat Linux

In our last post we have seen how to add a file for swap space.

In this post we will see how to add a LVM2 Logical Volume as swap.

Here we have a VG name VG1 in which we will create a volume LV1 of 1GB.

# lvcreate VG1 -n LV1 -L 1G

Format the new swap space using mkswap:

# mkswap /dev/VG1/LV1

Update /etc/fstab file with below entry:

# /dev/VG1/LV1 swap swap defaults 0 0

Enable the extended logical volume:

# swapon -v /dev/VG1/LV1