Wednesday, December 14, 2011

Linux Add a Swap File – Howto

Procedure To Add a Swap File

You need to use the dd command to create swap file. The mkswap command is used to set up a Linux swap area on a device or in a file.
a) Login as the root user.
b) Type following command to create 512MB swap file (1024 * 512MB = 524288 block size):
# dd if=/dev/zero of=/swapfile1 bs=1024 count=524288
c) Set up a Linux swap area:
# mkswap /swapfile1
d) Activate /swapfile1 swap space immediately:
# swapon /swapfile1
e) To activate /swapfile1 after Linux system reboot, add entry to /etc/fstab file. Open this file using a text editor such as vi:
# vi /etc/fstab
Append the following line:
/swapfile1 swap swap defaults 0 0
So next time Linux comes up after reboot, it enables the new swap file for you automatically.
g) How do I verify swap is activated or not?
Simply use the free command:
$ free -m

See also:

free command

Display free memory size in MB:
$ free -m

 Output:
             total       used       free     shared    buffers     cached
Mem:           750        625        125          0         35        335
-/+ buffers/cache:        254        496
Swap:          956          0        956
Displays a line containing the totals memory in MB:

 
$ free -t -m

Output:
       total       used       free     shared    buffers     cached
Mem:           750        625        125          0         35        335
-/+ buffers/cache:        253        496
Swap:          956          0        956
Total:        1707        625       1082

vmstat command

Type vmstat command at shell prompt:
$ vmstat

Output:
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
r  b   swpd   free   buff  cache   si   so    bi    bo   in    cs us sy id wa
1  0      0 131620  35432 341496    0    0    42    82  737  1364 15  3 81  1

top command

Type top command at the shell prompt:

$ top
Sample outputs:

 

1 comment:

Blog Nguyễn Vũ said...

hi u

When i set " vi /etc/fstab "

I dont see " /swapfile1 swap swap defaults 0 0"

So you check for me?

Thank!