HOW TO CREATE OR DELETE SWAP FILE IN LINUX .

 

In this post i’m going to show you guys how to create or delete swap partition in Linux

  • First create one file . My file is deepit.txt
[root@deepit ~]# touch deepit.txt
  • As you know this file doesn’t contain any size . So we will provide this file a size by disk dumping command .
  • dd – disk dumping
  • if=/dev/zero   – means no content , but we want to give size .
  • of=filename   – here provide file name , whose size you wanna change .
  • bs=1M              – at a time take base 1MB
  • count=1000    – so here base is 1M , we want it to take 1000 times , which makes it                                               10000Mb ( don’t  add “M” in count section)
[root@deepit ~]# dd if=/dev/zero of=deepit.txt bs=1M count=1000

1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 7.74336 seconds, 135 MB/s
  • Now format this file to inform system which type of file is this. Type  mkswap filename
[root@deepit ~]# mkswap deepit.txt
Setting up swapspace version 1, size = 1048571 kB
  • We have to activate swap file before using it .Type swapon  filename
[root@deepit ~]# swapon deepit.txt
  • Now you can check your deepit.txt swap file is been created by cat   /proc/swaps command.
[root@deepit ~]# cat /proc/swaps
Filename                                Type            Size    Used    Priority
/dev/sda2                               partition       5116692 0       -1
/root/deepit.txt                        file            1023992 0       -2
  • If you want to make it permanent .Type below command .
[root@deepit ~]# vim /etc/fstab
  • Make entry as shown below    Press I to go to insert mode.
  • first path of file   /root/deepit.txt
  • mounted to        swap
  • file type               swap
  • rest will be same .
LABEL=/                 /                       ext3    defaults        1 1
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
LABEL=SWAP-sda2         swap                    swap    defaults        0 0
/root/deepit.txt        swap                    swap    defaults         0 0
  • After making entry press ESC key and type :wq (to save and quit)
~
~
:wq

NOW IF YOU WANT TO DELETE SWAP FILE

  • Go to /etc/fstab where you did permanenet entry .
[root@deepit ~]# vim /etc/fstab
  • Remove this entry or just put # in front of this entry ( it will comment out)
#/root/deepit.txt        swap                    swap    defaults         0 0
  • Now press ESC and type :wq (to save and quit)
~
~
:wq
  • Deactivate swap file .  Type swapoff filename 
[root@deepit ~]# swapoff deepit.txt
  • Now delete the file by rm -rf command .
[root@deepit ~]# rm -rf deepit.txt
  • You can check by cat  /proc/swaps command it’s been deleted.
[root@deepit ~]# cat /proc/swaps
Filename                                Type            Size    Used    Priority
/dev/sda2                               partition       5116692 0       -1

CHECK OUT MY VIDEO

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s