Linux – how to create swap partition

Step to create swap partition on Ubuntu Linux. It should work on any other Linux also.

  1. Create storage file (say 4GB) using fallocate for swap partition
    $ sudo fallocate -l 4G /swap1
    
    Alternatively dd (slower) can also be used. To create file using 1M buffer size using dd

    $ sudo dd if=/dev/zero of=/swap1 bs=1M count=4096
    
  2. Make swap file only accessible by root
    $ sudo chmod 0600 /swap1
    
  3. Setup linux swap area in created file
    $ sudo mkswap /swap1
    Setting up swapspace version 1, size = 4194300 KiB
    no label, UUID=9551f5f9-5565-4753-81fd-9cc43a546daf
    
  4. To enabled swap file immediately
    $ sudo swapon /swap1
    
  5. To enable swap at boot time add the following to /etc/fstab
    /swap1 swap swap defaults 0 0
    
  6. To print swap summary
    $ sudo swapon -s
    Filename				Type		Size	Used	Priority
    /swap1                                  file		4194300	0	-1
    
    Alternatively use free -m to see swap size, etc.

    $ free -m 
                 total       used       free     shared    buffers     cached
    Mem:           992        778        214         64         28        231
    -/+ buffers/cache:        517        474
    Swap:         4095          0       4095
    
  7. To disable swap
    $ sudo swapoff /swap1
    

    This also removes it from /etc/fstab to not set it at boot time.

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments