Thursday, September 27, 2012

Create a Swap File in Linux

Add Additional Swap space for System performance.

Check the size of the disk and create a swap space accordingly.

[root@vmtest02 ~]# free -m
                     total       used       free     shared    buffers     cached
Mem:          2003        437       1565          0         24        307
-/+ buffers/cache:        105       1897
Swap:         4102          0           4102

[root@vmtest02 ~]# df -Th
Filesystem    Type    Size  Used Avail Use% Mounted on
/dev/sda2     ext3     12G  4.1G  6.8G  38% /
/dev/sda6     ext3     20G  3.7G   15G  20% /u01
/dev/sda5     ext3    3.9G  775M  3.0G  21% /tmp
/dev/sda1     ext3     99M   11M   83M  12% /boot
tmpfs        tmpfs   1002M     0 1002M   0% /dev/shm

Create Storage file for 1GB using the below command.

[root@vmtest02 ~]# dd if=/dev/zero of=/u01/swapfile1 bs=1024M count=1
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB) copied, 68.8808 seconds, 15.6 MB/s

To set the swap area, type

 [root@vmtest02 ~]# mkswap /u01/swapfile1
Setting up swapspace version 1, size = 1073737 kB

 By default the permissions will be

[root@vmtest02 ~]# ls -l /u01/swapfile1
-rw-r--r-- 1 root root 1073741824 Sep 27 13:26 /u01/swapfile1

change it root and set the permissions to 0600 for security reasons

Check the partition size reduced by 1GB

[root@vmtest02 ~]# df -Th
Filesystem    Type    Size  Used Avail Use% Mounted on
/dev/sda2     ext3     12G  4.1G  6.8G  38% /
/dev/sda6     ext3     20G  4.7G   14G  25% /u01
/dev/sda5     ext3    3.9G  775M  3.0G  21% /tmp
/dev/sda1     ext3     99M   11M   83M  12% /boot
tmpfs        tmpfs   1002M     0 1002M   0% /dev/shm

Activate the swap file and cross check the swap size.

[root@vmtest02 ~]# swapon /u01/swapfile1

Verify the swap space.

[root@vmtest02 ~]# free -m
                   total       used       free     shared    buffers     cached
Mem:          2003       1477        525          0         26       1331
-/+ buffers/cache:        120       1883
Swap:         5126          0          5126

Check the details of swap files using the below command.

swapon -s


You can also make the swap  on and off using the below commands.

swapoff -a &&  swapon -a
To make it permanent after reboot , make a entry in /etc/fstab like

 u01/swapfile1 swap swap defaults 0 0





No comments: