Wednesday 7 December 2016

CREATING SWAP in LINUX

FIRST WAY:
Create extra partition and make that partition as SWAP.

Check the present partition disk - fdisk -l
To see the current SWAP - swapon -s
/etc/fstab - We need to make an entry here of partitions so that after reboot, we dont loose the partition created.
To remove Swap - swapoff -v /dev/xvd3
To read from fstab - swapon -a

Step 1:
1. Create partition
   fdisk /dev/sdc   -- main disk name where we will create partition
   After that follow what is on screen, it will help to create a partition for SWAP
   p - will show the partitions
   m - will show menu where all commands and switches are mentioned
   n - add a new partitions."p" will create primary partitions
   partition number - Which number of partitions
   rest few paramter as default enter, enter
   Mention how much partition we need - +2G
   p - will show the new partitions
   t - change partition system ID
   Mention partition number for which we want to change system ID - 3
   Enter HEX code - L and then 82 (Linux SWAP/solaris). L will list the codes
   Then enter W - Write table to disk and exit
   fdisk -l /dev/sdc - Will show the existing partitons on the disk
   
2. Create Swap Partition
   mkswap /dev/sdc3  - It will create the partitions
   swapon -v /dev/sdc3 - It will add the partitions
   swapon -s - will show partition has been added
3. Make entry in Fstab
   device_name mount_point(swap) partition_type(swap) defaults 0 0
4. Check swap
   swapon -a -- It will read from /etc/fstab

SECOND WAY:
Create swap with the existing partition.

#In this tutorial we will learn how to create new swap file on CentOS 7 / RHEL 7. Generally on Cloud Servers ( AWS/ DigitalOcean) or VPS, swap space is not created by default.
#Prerequisites
#Must have free space on mounted disk. You can check by using df -Th command.
#Steps to create/add new swap file on Linux

1. Create swapfile-additional file with dd command in / (root). You can select any other partition but it should be mounted (For eg. /opt, /usr ,/NewMountedPartition)

dd if=/dev/zero of=/swapfile-additional bs=1M count=4048
dd = It is a unix command used for convert and copy a file
if = read from FILE instead of stdin
/dev/zero = /dev/zero is a special file in Unix-like operating systems that provides as many null characters (ASCII NUL, 0x00) as are read from it
of = write to FILE instead of stdout
/swapfile-additional = file named swapfile-additional will be created in /
bs = Read and write bytes at a time but if you do not mention MB or GB like only number it will read as bytes. for eg. bs=1024 means 1024 bytes
count = Copy input blocks in our case it is 1024 (1M * 4048 = 4GB)

2. Run mkswap command to make swap area
mkswap /swapfile-additional

3. Change the permission of file swapfile-additional
chmod 600 /swapfile-additional

4. Permanent mounting the swap space by editing the /etc/fstab file .
Use your file editor, I generally use vi editor.
vi /etc/fstab
Paste below given content in /etc/fstab file
/swapfile-additional swap swap    0   0

5. Now mount the swap area, run below given command.
mount -a

6. Enable the swap area
swapon -a

7. Check the number swap space mounted on your system
swapon -s

8. To check how much is swap space available on system. Run below given command
free -m

No comments:

Post a Comment