Tag: swapoff

How to increase the swap partition in Linux Mint and Ubuntu. How to create a Swap file in Linux

In this article, using Linux Mint and Ubuntu as an example, we will learn how to view information on the swap partition, create a swap file, and also increase the space available for swap.

The swap file is a specific place on permanent storage (for example, on a hard disk), where data that does not fit in RAM is temporarily dumped. That is, it is an opportunity to increase the amount of RAM without buying RAM modules. But such a “virtual” RAM is slower.

You can view the current size of the swap partition with the command:

swapon --show

Or with the free command:

free -h

Or with top:

top

As you can see, there is a 2 GB swap file on the system, which, let's say, is not enough.

Disable and delete this paging file with the following commands:

sudo swapoff -a
sudo rm -f /swapfile

There must be enough space on the disk to fit the swap file of the size you specified.

To create an 8G swap file (change the value as you wish) run the command:

sudo fallocate -l 8G /swapfile

Then run the commands:

sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Open System Monitor to make sure the system uses the swap file:

Or you can do it with the command:

swapon --show

That's all! You don't even need to reboot your system for the changes to take effect.

By the way, if you go again to change the size of the paging file (up or down – it doesn’t matter), then exactly follow the same instructions again, but specify a different size when creating the paging file.

How to delete swap file in Linux Mint and Ubuntu

To completely remove the paging file, run the commands:

sudo swapoff -a
sudo rm -f /swapfile

Then open the /etc/fstab file:

sudo gedit /etc/fstab

And remove the line from it:

/swapfile none swap defaults 0 0

Error “fallocate: fallocate failed: Text file busy”

If you encounter an error:

fallocate: fallocate failed: Text file busy

This means that you want to create a swap file, but a file with the same name already exists and is still in use.

To disable and remove it, run the commands:

sudo swapoff -a
sudo rm -f /swapfile

Managing Swap files on other Linuxes

To increase or create a swap file on other distributions, see the articles:

How to create a swap partition on another drive

You don't have to use the system disk for the swap, which can be low on space. You can create a partition on any drive in the system. For details, see the instruction “How to create a swap partition not on the system drive. How to move the swap partition to another disk”.

How to check swap file usage in Linux

Stress testing the system in a out of RAM scenario: How to check Swap file usage in Linux

Swap file and swap partition in Arch Linux (BlackArch): what to choose and how to add Swap

RAM, along with the central processor, is a determining factor in the performance of a computer. Moreover, the speed (frequency) of RAM is not so important, how important is its quantity – the amount of RAM.

The computer starts to work much slower if it does not have enough RAM. Moreover, if it has a swap partition, then it can copy some information from RAM to it and then read it – this reduces performance, but at least does not lead to a reset of running tasks and a computer freeze.

So, if the performance of your computer drops due to a lack of RAM, then I would recommend upgrading the hardware first, that is, buying more RAM sticks. But this is not always possible. Or, in addition to buying hardware RAM, you can add another swap partition.

What is Swap

Swap is a place in permanent storage (hard disk, SSD, USB storage) to which part of the RAM is temporarily dumped, which:

a) not currently used in calculations

b) does not fit in physical RAM.

What is the difference between swap partition and swap file

So, we have decided that the swap partition and swap file in Linux is called Swap.

Now let's look at the two available options: partition and swap file.

Swap partition

A swap partition is a dedicated disk space, a separate volume that is created when partitioning a disk during installation of an operating system.

If you want to create a swap partition after installing the operating system, for example, as follows: reduce the volume of one of the partitions, and create a Swap partition on the freed space, then most likely you will not be able to do this “hot”, that is, during operation this same operating system. You will have to boot from a USB stick or optical disc and perform the necessary operations from it.

Swap file

A swap file performs the same functions as a partition, but is more convenient to use.

The convenience of using the swap file is expressed as follows:

a) the swap file is created as a regular file, that is, there is no need to partition the disk

b) to connect the swap file, you do not need to restart the operating system

c) to change the size of the swap file, you do not need to restart the operating system

d) to disable and delete the swap file, you do not need to restart the computer

That is, the situation is quite real when you need a swap file for a short time – for example, for an hour. You can create it, connect it, perform a task that requires a significant amount of RAM, and disable and delete it after. This is especially true if you have, for example, a fast, but not large SSD drive – and you don't want to constantly keep the swap file, especially if it is not particularly needed most of the time.

Another option with a paging file is convenient on VPS hosting, when unnecessary server downtime due to maintenance is not needed, and the impossibility of physical access requires contacting the support service.

How to display information about the swap file in Linux

To check if the swap file is present, what size it is and how much it is being used, run the command:

swapon --show

Either the command:

free -h

How to create a swap file in Arch Linux

All subsequent commands must be run with root user privileges.

To create a sSwap file, run the fallocate command, along with which you need to specify the size of the swap file to be created. You can use abbreviations (M = megabytes, G = gigabytes). For example, to create a 16 GB swap file located at the /swapfile path, you need to run the command:

sudo fallocate -l 16G /swapfile

The fallocate command can cause problems with some file systems such as F2FS or XFS. A more reliable, though slower, alternative is to use the dd command (this example will create a 512 megabyte file):

dd if=/dev/zero of=/swapfile bs=1M count=512 status=progress

Set file permissions (readable swap is a huge local vulnerability):

sudo chmod 600 /swapfile

Format the file in swap:

sudo mkswap /swapfile

Activate the swap file:

sudo swapon /swapfile

Please note that already at this stage the swap file starts to be used – no system reboot is required!

To ensure that the swap file is used after each reboot, open the /etc/fstab file.

In GUI:

sudo gedit /etc/fstab

In the command line interface:

sudo vim /etc/fstab

And add this line to this file:

/swapfile none swap defaults 0 0

Note: The swap file must be specified by its location in the file system, not by its UUID or LABEL.

Removing the Swap file

To remove the swap file, you must first disable it, and then it can be deleted:

sudo swapoff -a
sudo rm -f /swapfile

Finally remove the relevant line from /etc/fstab.

How to check Swap File usage in Linux

Stress testing the system in a out of RAM Scenario: How to check Swap file usage in Linux

Loading...
X