Home » 2015 » June » 19 » How to create swap file in Linux : Adding extra swap space

10:58 AM
How to create swap file in Linux : Adding extra swap space

To create SWAP space/file in your system, you just need to follow below simple steps. First we shall see how much free space we are having using free -k or swapon -s command.

shankar@shankar-desktop ~ $ free -k
             total       used       free     shared    buffers     cached
Mem:       2061716     936484    1125232          0      43624     367612
-/+ buffers/cache:     525248    1536468
Swap:      492536          0    492536
shankar@shankar-desktop ~ $ swapon -s
Filename                Type        Size    Used    Priority
/dev/sda5                               partition    492536    0    -1

 

Here swapon -s just looks into the file /proc/swaps.

shankar-desktop ~ # cat /proc/swaps
Filename                Type        Size    Used    Priority
/dev/sda5                               partition    492536    0    -1

 

Note: free -k shows free spaces in KB, free -m, displays in MB and free -g shows free spaces in system in GB.

 

  • We shall create swap file /root/swapfile of size 1 GB using following simple steps:
  • create data dump of size 1 GB using dd command:

 

#>dd -if=/dev/zero -of=/root/swapfile -bs=1M count=1024

Here, -if (input file) --> reads data from specified input file

-of(output file) --> writes data to specified output file

-bs(BYTES)  -->read and write BYTES bytes at a time

-count(BLOCKS) --> copy only BLOCKS input blocks

  • Now make this file as swap file using mkswap command.

#>mkswap /root/swapfile

  • This may throw some error, better use -f option. mkswap -f /root/swapfile

 

 -f     Force - go ahead even if the command is stupid.  This allows the creation of a swap area larger than the file or partition it resides on. 

Without this option mkswap will refuse to erase the first block on a device with a partition table or on whole disk (e.g. /dec/sda).

  • Now turn on the swap file using swapon command.

#>swapon /root/swapfile

 

  • This is it! Your swap file is created. you can check it using free -k or swapon -s command again.

 

shankar@shankar-desktop ~ $ free -k
             total       used       free     shared    buffers     cached
Mem:       2061716     961744    1099972          0      45204     371216
-/+ buffers/cache:     545324    1516392
Swap:      1541104          0    1541104

  • Now, to make this swap permanent, i.e. if you want the swap file to be available as a swap area even after reboot, you need to append below line in your /etc/fstab file.

/root/swapfile               swap                    swap    defaults        0 0

  • ​Now once you reboot this swap area will be available. If you don't want to reboot and still want to check total swap space of the system, you can use swapon/swapoff command.

#>swapoff -a

#>swapon -a

 -a, --all

 

              All  devices  marked  as  ``swap'' in /etc/fstab are made available, except for those with the ``noauto'' option.  Devices that are already being used as swap are silently skipped.     

 

 

 

 

 

 
 

Category: Open System-Linux | Views: 1694 | Added by: shanky | Tags: unix, swapon, linux, mkswap, create swap file in linux, free command, swap space, swapoff | Rating: 5.0/1

Related blogs


You may also like to see:


[2014-09-13][Open System-Linux]
md5sum: calculate and check md5 message digest of a file in Linux
[2014-04-10][Open System-Linux]
SED command for filtering and transforming texts of a file using regular expression
[2015-06-19][Open System-Linux]
How to create swap file in Linux : Adding extra swap space
[2014-10-04][Open System-Linux]
SYSCTL : A command to configure kernel parameters at runtime in linux
[2016-04-14][Open System-Linux]
AT command in Linux : schedule a task at later time

Total comments: 0
ComForm">
avatar