Understanding Swapfile and setting up Swap for a System Print

  • swap file, create swap file, remove swap file, swap commands, mkswap, dd, dd command, fstab, mount swap, swap memory, swap area, swap space, swapoff
  • 35

Swap is space on a disk that is reserved to be used as virtual memory. When a Linux server runs out of memory, the kernel can move inactive processes into swap to make room for active processes in the working memory.

1. Create 'swapfile' file with dd command in / (root's home dir that is '/'). 'dd' Linux commands primary purpose is to convert and copy files.
 
dd if=/dev/zero of=/swapfile bs=1M count=4048
 
dd = It is a unix command used for convert and copy a file
 
if = read from FILE instead of stdin
/dev/zero = /dev/zero is a special file in Unix-like operating systems that provides as many null characters (ASCII NUL, 0x00) as are read from it
of = write to FILE instead of stdout
/swapfile = file named swapfile will be created in /
bs = Read and write bytes at a time but if you do not mention MB or GB like only number it will read as bytes. for eg. bs=1024 means 1024 bytes
count = Copy input blocks in our case it is 1024 (1M * 4048 = 4GB)
 
2. Run mkswap command to make swap area: mkswap /swapfile
 
3. Change the permission of file swapfile-additional: chmod 600 /swapfile
 
4. Permanent mounting the swap space by editing the /etc/fstab file:
Use your file editor, I generally use vi editor: vi /etc/fstab
    Add the content in /etc/fstab file: /swapfile swap swap 0 0
 
5. Now mount the swap area: mount -a

6. Enable the swap area: swapon -a

7. Check the number swap space mounted on your system: swapon -s
 
8. To check how much is swap space available on system.Run below given command: free -m
 
You can try creating swapfile with different sizes on your local machine or test VM.
 
 
To remove a swap file:
 
1. At a shell prompt as root, execute the following command to disable the swap file (where /swapfile is the swap file): swapoff -v /swapfile

2. Remove its entry from the /etc/fstab file.

3. Remove the actual file: rm /swapfile

Was this answer helpful?

« Back

Powered by WHMCompleteSolution