Swap Space / Swap File extending in the RHEL Servers

Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which have a slower access time than physical memory. Swap Space / Swap File extending in the RHEL Servers

By Default, the Linux machines will have a swap space created automatically during the OS installation. We can extend the swap space using 2 methods

  • Use a Hard Drive Partition for Additional Swap Space
  • Use a File for Additional Swap Space

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

Step 1: Adding the Disk from the vSphere Client – Select the VM and click Edit Settings

swap disk file rhel memory

Step 2: Click Add – Select Hard Disk

hdd hard disk swap memory
add hard disk swap memory file disk

Step 3: Click – Create a new Virtual Disk

create new virtual disk swap memory disk

Step 4: Select the Disk Size based on your requirement and Thin provision

select the space of the disk

Step 5: Click Next and Click Finish

disk add swap memory
finish disk add swap memory

Step 6: Scan the new drives using the following commands

# echo “- – -” > /sys/class/scsi_host/host0/scan

# echo “- – -” > /sys/class/scsi_host/host1/scan

# echo “- – -” > /sys/class/scsi_host/host2/scan

Note: sometimes you need to reboot the VM for the new disk to be visible in linux

Step 7: Check the fdisk whether Disk is added: #fdisk -l 

add disk file check

Step 8: Create the Partition: #fdisk /dev/sdb

disk swap fdisk

Step 9: Check the fdisk –l to check whether a partition is created

fdisk swap rhel memory extend

Step 10: Format the Partition to ext4 FileSystem : #mkfs.ext4 /dev/sdb1

swap mkfs ext memory rhel extend

Step 11: Create a physical volume: #pvcreate /dev/sdb1

pvcreate swap memory disk rhel

Step 12: Check the current swap space using the command ‘free -m’. We could see current swap is 4GB and we have added 2GB of hard disk , so the final swap space should be 6GB

free disk memory swap rhel

Step 13: Execute command to make the partition as swap ‘# mkswap /dev/sdb1’

mkswap disk memory rhel free

Step 14: Execute Command to enable swap on the created partition ‘# swapon /dev/sdc1’

swapon swap memory free disk

Step 15: edit the FSTAB and provide the partition details, so that the swap is initialized after every reboot of the VM

swap memory disk free increase

Step 16: Verify the swap location and the current swap size

 # swapon -s

 # free -m

swapon swap free memory disk

Method 2: Use the file for additional swap space

Step 1: I could see the /code has space to create file of 1 GB to extend swap from 4GB to 5GB

swap memory disk free space

Step 2: ‘Free -m’ commands show there is 4GB of swap in this machine

swap space disk memory free

Step 3: Use the command – dd if=/dev/zero of=/code/<myswapfile> bs=1M count=1000

This command will create a 1GB file in the /code partition

swap file free disk memory

Step 4: Change the ownership of the file for the root access only and execute the ‘mkswap’ to make the file(swapfile in /code) as a swap space

swap chmod free memory space

Step 5: Enable the swap for the create 1GB file using the following command

swapon swap rhel memory linux disk space

Step 6: Edit the FSTAB and add the file details, so that every time the machine is rebooted – it will initialize the file as a swap space

swap cat memory disk release linux

Step 7: Execute ‘swapon -s’ to verify the swap location details

Note: we could see /code/myswapfile is of Type – File

swapon swap memory free disk linux

Step 8: Execute the command ‘free -m’ to verify whether the swap is extended from 4GB to 5GB

extend swap memory disk free linux

We hope this 2 methods of Swap Space / Swap File extending in the RHEL Servers is working fine on your machine also.

And also if you required any technology you want to learn, let us know below we will publish them in our site http://tossolution.com/

Like our page in Facebook and follow us for New technical information.

Leave a Reply

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