How to Make Swap File on Linux – Guide

Swapping is a way to free up working memory so that your system can serve more important processes. When your system runs out of memory, it will use swap memory and swap some memory for disk space. This will free up main memory so that you can serve other processes. Traditionally, swap memory is used as a separate partition on the hard drive. When installing Linux, create a separate partition just for the swap file. But that trend has changed in recent years. With the swap file, you no longer need a separate partition. You create a file in root and tell your system to use it as a Swap Space

Swapping can be a nightmare if you have a dedicated swap partition. In many cases, it’s impossible to resize the swap space without affecting the performance of the system. However, you can change the size of your swap file as you wish. This is part of the permanent memory on your hard drive. When the system detects that its physical memory is full, it starts using the swap space available on the system. This means that if you have a large swap file, your computer will be able to use it even when its physical memory is low.

How to Create swap file in Linux

This tool displays the swap space and RAM usage of a Linux system.

dd if=/dev/zero of=/swapfile bs=1M

This will create a new swap file on the system.

2 Linux Gib swap file example

This command will create a 1GB swap file on your system. ..

To secure your Linux system, make sure to set up file permissions using the chmod command and the chown command. ..

$ sudo chmod -v 0600 / swap file

This command will change the owner of the swap-file to root and the group to root. ..

The following are the permissions for the file: -rwxr-xr-x 1 root wheel 512 Dec 7 10:12 myfile.txt The file has the following permissions: -rw-r–r– 1 root wheel 512 Dec 7 10:12 myfile.txt ..

This command prints the contents of the swap-file.

swapon -a /dev/sda2 This sets up a swap area on the device /dev/sda2.

swapon -S swapfile

How to enable swap space file persistently on Linux

/dev/sda1 /media/swap ext4 defaults 0 0 After rebooting your Linux system, please make sure the swap file is enabled. To make the change permanent, edit the /etc/fstab file: /dev/sda1 /media/swap ext4 defaults 0 0 ..

config.log_level = “INFO” config.log_format = “%Y-%m-%d %H:%M:%S” The above will log all events at the INFO level, with date and time stamps.

The command “swap-file swap” will create a new swap file with the same size as the old one, but with different patterns. The first number is the number of blocks in the new file, and the second number is the number of blocks in the old file. ..

Save the file and close it.

Final note

How to Make Swap File on Linux In this guide, we will show you how to make a swap file on Linux. A swap file is a file that stores temporary files so that your computer can save its resources (memory and disk space) while it is not in use. When you need to shutdown your computer, your computer will first save the contents of the swap file to disk and then reload it from disk.