How to create swap space on Linux by adding a swap file?

How to create swap space on Linux by adding a swap file?

 

When the physical memory of a system is full, the operating system moves inactive pages of physical memory to the swap space. This is useful for the systems that have a small amount of physical memory.

The swap space should not be considered as the replacement of physical memory. As it is located on the hard disk it has slower access time than physical memory.

The swap space or swap area can be a dedicated swap partition, a swap file, or a combination of both. If swap partition is not present in your system then you can create the swap space by creating a swap file.

How to add the swap file

We will use the dd command to create a swap file on a system. First, you need to determine the size of the swap file in block size, you can simply find the block size by multiplying the size of the swap file in megabyte to 1024 for example the swap file of size 2048 MB will have the block size 2048*1024 =2097152.

Now use the following command to create the file –

dd if=/dev/zero of=/swapfile bs=1024 count=2097152

The file is now created with the name of the swapfile

The users with root privilege will be able to read or write the swapfile, now use the following command to provide the correct permissions to this file –

chmod 0600 /swapfile

Now use the following command to set up this file as the swap space –

mkswap /swapfile

Enable the swap space by using –

swapon /swapfile

To enable the swap space permanently append the given line into /etc/fstab

/swapfile     swap   swap   defaults 0 0

Verify swap space

After adding and enabling the swap file you can verify the swap space on your system by using the following command –

cat /proc/swaps

or by using –

free -h

How to remove the swap file

For any reason, if you decided not to use this swap space and want to remove this file then you can remove it by using the given commands.

First, deactivate the swap space by using –

swapoff -v /swapfile

Then remove the swapfile entry from /etc/fstab file which was –

/swapfile     swap   swap   defaults 0 0

And then use the rm command to remove this file from the system –

rm /swapfile

Now you have successfully deleted the swapfile from your system.

Conclusion

The swap space is generally implemented with swapfile in systems running on virtual machines. Now if you have a query on this topic then you can leave it in the comments below.

Previous Dell optima with a rosewill rnx-n180ube Wi-Fi antenna won't connect.
Next [ubuntu_mate] Raspistill command not found on RPi 3B using Ubuntu mate v18 as OS
 

About author

You might also like

Linux Terminal 0 Comments

How To Change Username In Linux/Unix From The Terminal?

This post was last updated on August 1st, 2020 at 03:04 pmIn a Linux/Unix system sometimes you may need to change the username or User ID (UID) of a user

Linux Topics 0 Comments

How To Run A Process In Background In Linux/Unix?

This post was last updated on August 1st, 2020 at 02:11 pmIn a Linux/Unix system, a process is an instance of a program that is running on a computer. When

Linux Topics 0 Comments

How To List The All Enabled Repositories In Linux?

This post was last updated on August 1st, 2020 at 01:28 pmA software repository is a database of software packages stored on a remote server. Generally, these packages are retrieved

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply