Linux Add a Swap File Tutorial

www.la‮c.iruttu‬om
Linux Add a Swap File Tutorial

A swap file is a special file on a Linux system that is used as virtual memory. It allows the operating system to use disk space as additional memory when the system's RAM is full. This can be useful if your system has a limited amount of RAM or if you are running resource-intensive applications that require more memory than is available.

To add a swap file on a Linux system, you will need to do the following:

  1. Determine the size of the swap file: The size of the swap file will depend on the amount of RAM and disk space available on your system, as well as the expected workload of the system. As a general rule, the size of the swap file should be at least twice the size of the system's RAM.

  2. Create the swap file: To create the swap file, you can use the dd command to create a file with the desired size and fill it with zeros. For example, to create a 1GB swap file, you can use the following command:

dd if=/dev/zero of=/swapfile bs=1M count=1024
  1. Set the permissions and ownership of the swap file: The swap file should be owned by the root user and have permissions of 600 (read and write only for the owner). You can use the chmod and chown commands to set the permissions and ownership of the swap file:
chmod 600 /swapfile
chown root:root /swapfile
  1. Mark the swap file as a swap space: To mark the swap file as a swap space, you can use the mkswap command:
mkswap /swapfile
  1. Enable the swap
swap -a /swapfile
  1. check out that the swap(from file) is being used
swap -l
Created Time:2017-10-29 22:08:51  Author:lautturi