Add swap partition on linux server

Basically SWAP partition is create on your HDD area and it will be used when space of RAM is no longer sufficient for data. The information written on SWAP will be slower than RAM. It is useful part of our system to prevent system from cashing.

Check the System for Swap Information

sudo swapon -s

Filename      Type        Size      Used      Priority

if you get only header information of table it means you don't have currently SWAP space enabled.

Another more familier way to check SWAP space is 

free -m

                      total              used              free               shared           buff/cache            available
Mem:            7713              5006              624               111                2082                    2214
Swap:           975                20                  955

 

Create A SWAP File

For instance, in our example, we're looking to create a 2 Gigabyte file. We can do this by specifying a block size of 1 Gigabyte and a count of 2.

sudo dd if=/dev/zero of=/swapfile bs=1G count=4

We can see that 2 Gigabytes have been allocated by typing:

ls -lh /swapfile
-rw-r--r-- 1 root root 2.0G Feb 2 17:15 /swapfile

There is one another way to create SWAP partition using fallocate programme.

We can create 2GB of SWAP partition by below command

sudo fallocate -l 2G /swapfile

after creating SWAP file we need to enable it.

Enable SWAP file

We can simply lock down the file permission by typing below command.

sudo chmod 600 /swapfile

Now we can inform our system to set up SWAP space by typing

sudo mkswap /swapfile

now just on the SWAP partition by typing

sudo swapon -s

Now Set SWAP file permanent by fstab entry
edit the file with root priviledges
sudo nano /etc/fstab

Insert below line in file

/swapfile    none    swap    sw    0    0

 

 



 

What's Your Reaction?

like
0
dislike
0
love
0
funny
0
angry
0
sad
0
wow
0