Create and Enable Swap

Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM.
we will create a swap file of size 2GB using the dd command as follows. Note that bs=1024 means read and write up to 1024 bytes at a time and count = (1024 x 2048)MB size of the file.
dd if=/dev/zero of=/swapfile bs=1024 count=2097152
And then set the appropriate permissions on the file; make it readable only by root user as follows.
chmod 600 /swapfile
Now setup the file for swap space with the mkwap command.
mkswap /swapfile
Next, enable the swap file and add it to the system as a swap file.
swapon /swapfile
Afterwards, enable the swap file to be mounted at boot time. Edit the /etc/fstab file and add the following line in it.
/swapfile swap swap defaults 0 0
In the line above, each field means:
/swapfile – device/file name
swap – defines device mount point
swap – specifies the file-system type
defaults – describes the mount options
0 – specifies the option to be used by the dump program
0 – specifies the fsck command option

Be the first to comment

Leave a Reply

Your email address will not be published.


*