How to create a swap file / partition on Linux

When the system needs more memory than is physically available, it needs to be swapped. The kernel swaps out less used pages and provides memory for current processes that require memory. Therefore, a page of memory is copied to a pre-configured space on the hard disk. Compared to memory speed, disk speed is much slower. Swap pages provide more space for the current application in memory (RAM) and make the application run faster.

Swap space is located on the hard drive and its access time is slower than physical memory. The swap space can be a dedicated swap partition (recommended), a swap file, or a combination of a swap partition and a swap file. It is recommended that your swap space be equal to twice the computer’s RAM or 32 MB (whichever is greater), but no more than 2048 MB (or 2 GB), because modern computers have more than 4096 MB (or 4GB) of RAM. Newer versions of Ubuntu and Centos support the use of swap files.

In this tutorial, we learn how to create a swap partition or file on a Linux system.

table of Contents

  • Create a swap partition
  • How to create a swap partition for LVM
  • Extended LVM swap partition
  • Delete swap partition
  • Adjust swap properties
  • in conclusion

Create a swap partition

1) Check disk space

Use the fdisk command to check if there is enough space on the disk to create a new partition for swap.

# fdisk -l

Disk /dev/sda: 10.7 GB, 10720641024 bytes, 20938752 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/sdb: 536 MB, 536870912 bytes, 1048576 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x66fe37bd

We will use /dev/sdb Disk is used for our swap. Free Check Exchange -m Command; we have:

# free -m
 total used free shared buff/cache available
Mem: 988 88 645 50 254 813
Swap: 0 0 0

You can see that we do not have a swap partition. We can also use the following command for verification

# swapon -s

You will see that we have no returns. It means no exchange

2) Create a partition

Create a new partition using a tool like fdisk:

# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the

Command (m for help):

You can enter m Order for help, it will list your different possibilities. We will create a new partition for the swap n command

Command (m for help): n
Partition type:
 p primary (0 primary, 0 extended, 4 free)
 e extended
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-1048575, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-1048575, default 1048575): 
Using default value 1048575
Partition 1 of type Linux and of size 511 MiB is set

Now to define the partition as a swap type, we will use the t command

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 82

Changed type of partition 'Linux' to 'Linux swap / Solaris'

of Hex code The swap partition used on Linux is 82. Now we will save the changes w command

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Set the new partition to swap. Change the switch ID to 82 (for exchange). Let’s check fdisk -l command:

# fdisk -l

Disk /dev/sda: 10.7 GB, 10720641024 bytes, 20938752 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/sdb: 536 MB, 536870912 bytes, 1048576 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x66fe37bd

 Device Boot Start End Blocks Id System
/dev/sdb1 2048 1048575 523264 82 Linux swap / Solaris

You can see the “Linux swap” mentioned in the last line.

note: On the latest versions of Ubuntu and Centos, it uses creating swap files instead of swap partitions. Let’s see how to create a swap file.

Just use the dd command or use fallocate to create the file (for example 1 GB or 2GB).

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

or

# fallocate -l 2G /mnt/swapfile
# chmod 600 /mnt/swapfile

Then follow the steps below.

3) Format as swap mode

After defining the partition, we need to format it as “swap mode”, so run the mkswap command on the newly created swap partition:

# mkswap -f /dev/sdb
mkswap: /dev/sdb: warning: wiping old swap signature.
Setting up swapspace version 1, size = 524284 KiB
no label, UUID=c4696894-0b09-4fbe-87bb-a34d6d307a4e

Either

# mkswap /mnt/swapfile

4) Enable swap space

Now that our swap partition is formatted, we need to enable swap space, so run the swapon command to enable it:

# swapon /dev/sdb

5) Verify swap space

Use the following command to verify the newly added swap space:

# free -m
 total used free shared buff/cache available
Mem: 988 88 646 50 254 814
Swap: 511 0 511

6) Add to fstab file

Then add the newly created swap partition to /etc/fstab file. It should look like this:

/dev/sdb swap swap defaults 0 0

How to create a swap partition for LVM

You can install LVM on the server and you need to create a swap partition. Due to “lvm mode” the process is not exactly the same

step 1: We must first create an LVM2 logical volume with a size of 8 GB:

#lvcreate rootvg -n swapvol -L 8G

Step 2: After creating the logical volume, we need to format the new swap space:

# mkswap /dev/rootvg/swapvol

third step: To ensure that the swap partition will mount automatically even if the server is restarted, we need to add the following entries to /etc/fstab file:

/dev/rootvg/swapvol swap swap defaults 0 0

Step 4: Now we need to enable extended logical volumes:

# swapon -v /dev/rootvg/swapvol

To test if the logical volume was successfully created, use swapon -s Either free -m Command to check swap space.

Extended LVM swap partition

You may need to expand the swap partition because the actual swap size is not sufficient for your job. With lvm, you can directly increase the size of an existing partition, as shown below.

step 1: You must first identify the swap volume group, in this case dev / rootvg / swapvol. You need to disable the current swap first

# swapoff -v /dev/rootvg/swapvol

Step 2: You must now resize the volume group to indicate how much space to add

# lvm lvresize /dev/rootvg/swapvol -L +8G

We want to increase from 8 GB to 16 GB

third step: Now we need to format the space

# mkswap /dev/rootvg/swapvol

Step 4: Now we need to activate the swap of the device marked swap in /etc/fstab

# swapon -va

Delete swap partition

For some reason, you may need to delete the swap partition in lvm mode.

step 1: To delete a swap partition, you first need to disable the swap of the associated logical volume, whether it is lvm or something else there

# swapoff -v /dev/rootvg/swapvol

Step 2: The second principle is to delete the volume, so you need to completely delete the swap partition.

lvremove /dev/rootvg/swapvol

third step: Now we need to start from /etc/fstab file

/dev/rootvg/swapvol swap swap defaults 0 0

Adjust swap properties

The Swappiness value defines how often the system exchanges data from RAM to the swap space. The current swap value is stored in the “/ proc / sys / vm / swappiness” file. The value is between 0 and 100. Lower values ​​(close to zero) will cause the kernel to try to avoid swapping. The server can have a value close to 0, and Desktop 60 should be fine.

# cat /proc/sys/vm/swappiness
60

in conclusion

In this tutorial, we looked at how to create a swap file / partition on Linux. We hope you enjoyed reading this article and providing suggestions in the comments section below.

Sidebar