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 :

Learn Ethical Hacking From Scratch for only $10.99
# 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 .

Leave a Reply

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