How To Add Swap On Linux

by
Share this Article: Facebook0Google+0Twitter13LinkedIn0Reddit0StumbleUpon1

Swap is a type of filesystem and is a virtual memory. Whenever your RAM is full, your operating system will look for further memory in your swap space. For this reason, you reserve some part of the hard disk to create a swap partition.

swap

Identifying Current Swap Space Usage

[root@localhost ~]# cat /proc/swaps
Filename          Type          Size          Used        Priority 
/dev/sda7     partition        1951740         4                -1

Alternatively, use the swapon command:

[root@localhost ~]# swapon -s
Filename            Type           Size          Used      Priority
/dev/sda7          partition      1951740           4            -1

Finally, the free command may also be used:

[root@localhost ~]# free
                      total       used           free     shared     buffers     cached
Mem:            895112     721656    173456      0         36592      310156
-/+ buffers/cache:         374908    520204
Swap:           1952736         4    1952732

Adding a Swap File

Additional swap may be quickly added to the system by creating a file and assigning it as swap. This is achieved as follows. The following dd command example creates a swap file with the name swap with a size of 1Gb.

Create the swap file using the dd command:

[root@localhost ~]# dd if=/dev/zero of=/swap bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 17.4283 s, 61.6 MB/s

Configure the file as swap

Change the permission of the swap file so that only root can access it

[root@localhost ~]# chmod 600 /root/swap

[root@localhost ~]# mkswap /swap
 Setting up swapspace version 1, size = 1048572 KiB
 Enable the newly created swapfile :

[root@localhost ~]# swapon /swap

Finally, modify the /etc/fstab file to automatically add the new swap at system boot time by adding the following line:

# cat /etc/fstab
/swap    none    swap    sw    0 0

Once the swap space has been activated, verify that it is in use using the swapon –s command:

[root@localhost ~]# swapon -s
Filename      Type        Size       Used    Priority
/dev/sda7    partition   1951740  142884      -1
/swap            file         1048572       0         -2
[root@localhost ~]# free -k
                 total       used       free      shared       buffers      cached
Mem:        895112     828484      66628          0        2144        539552
-/+ buffers/cache:     286788      608324
Swap:       3000312    142876      2857436

If you don’t want to reboot to verify whether the system takes all the swap space mentioned in the /etc/fstab, use following to enable or disable swap.

[root@localhost ~]# swapoff -a
[root@localhost ~]# swapon -a

De-activate the additional swap space at any time using the swapoff command as follows:

[root@localhost ~]# swapoff /newswap

For questions please refer to our Q/A forum at : http://ask.unixmen.com/


Share this Article: Facebook0Google+0Twitter13LinkedIn0Reddit0StumbleUpon1