How to create Swap file in linux


How to Create swap file in Linux


The below post will help you how to create swap file. First of all what is the use of swap file. Using the swap file we can extend the swap file partition. We can increase the swap space. Let’s see how to create swap file.


Verify what is the current swap partition size


Step 1: #swapon -s

Filename Type Size Used Priority

/dev/sda6 partition 2096440 0 -1

The above command will tell you how to view the swap file partition and size in KB


Step 2: #free -m

total used free shared buffers cached

Mem: 1886 1135 751 0 36 539

-/+ buffers/cache: 558 1328

Swap: 2047 0 2047

The above command will help you to see swap space in MB


Step 3: #expr 2096440 / 1024


The above command tells you how to convert KB to MB

Increase the swap partition space


Now we going to increase the swap partition space from 2GB to 3GB

 Step 1: #dd if=/dev/zero of=/swapfile1 bs=1024 count=1048576


Step 2: #mkswap /swapfile1


Step 3: #swapon /swapfile1

Step 4: #vim /etc/fstab

#Appent the following line in the end of the file

/createswap swap swap defaults 0 0

Step 5: #mount -a


Step 6: #free -m

total used free shared buffers cached

Mem: 1886 1867 19 0 5 1289

-/+ buffers/cache: 572 1314

Swap: 3071 0 3071


Step 6: #echo 1024*3 | bc


The above command will tells you that the size of the swap partition is increased from 2GB to 3GB