-->
  • Recent Articles

    How to Create Swap File in Linux

     How to Create Swap File in Linux


    Create a 8GB file.
    [root@exampleserver ~]# dd if=/dev/zero of=/swap8gfile bs=1G count=8
    8+0 records in
    8+0 records out
    8589934592 bytes (8.6 GB) copied, 7.57026 s, 1.1 GB/s
    [root@exampleserver ~]#

    Change the swapfile Permission
    [root@exampleserver ~]# chmod 644 /swap8gfile

    Enable the Swap area.
    [root@exampleserver ~]# mkswap /swap8gfile
    mkswap: /swap8gfile: warning: don't erase bootbits sectors
            on whole disk. Use -f to force.
    Setting up swapspace version 1, size = 8388604 KiB
    no label, UUID=008a5d0f-5b73-4aec-b209-8c283553ad33
    [root@exampleserver ~]#

    You can add the the entry in fstab, so the swap can be persistent across reboots.
    /swap8gfile swap                    swap    defaults        0 0

    Now Enable the swap.
    [root@exampleserver ~]# swapon /swap8gfile
    [root@exampleserver ~]# free -m
                 total       used       free     shared    buffers     cached
    Mem:        515671      11721     503949          0         45       8403
    -/+ buffers/cache:       3272     512398
    Swap:        24575          0      24575
    [root@exampleserver ~]#

    Few Handy Commands:

    free -g --check the current swap status

    swapoff swapfile -- Off the swap space if already created. It is not required if creating a new one

     rm swapfile -- Remove Swap file
     dd if=/dev/zero of=/opt/swapfile bs=1024 count=4553600 -- Create a swap file of 4.7 GB
     chmod 0600 /opt/swapfile -- Change the Permission of SwapFile
     mkswap /opt/swapfile --- Define the swapfile as swap space
     swapon /opt/swapfile --- switch on the swap space so that it can be used by system
      free -g --check the current swap status
    /opt/swapfile               swap                    swap    defaults        0 0 -- Now Make an entry in fstab file

    No comments