Linux add a swap file

You need to use dd command to create swapfile. Next you need to use mkswap command 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 * 1024MB = 1048576 block size):
# dd if=/dev/zero of=/swapfile1 bs=1024 count=1048576

 

c) Set up a Linux swap area:
# mkswap /swapfile

 

d) Activate /swapfile1 swap space immediately:
# swapon /swapfile

 

e) To activate /swapfile1 after Linux system reboot, add entry to /etc/fstab file. Open this file using text editor such as vi:
# vi /etc/fstab

 

Append following line:
/swapfile 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 free command:
$ free -m

Leave a Comment

Your email address will not be published. Required fields are marked *

CAPTCHA * Time limit is exhausted. Please reload the CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top