How to create swap partition in Linux

In this lesson we’re going to talk about swap Partition.

If you want to create a standard Linux file system to store data in the form of files and directories, then you use the mkfs command to create a file system on the given partition.

However, be aware that you can also format a partition such that can be used for Virtual Memory on your Linux system.

In this case you need to format the partition as a swap partition, and this is done using the mkswap command at the shell prompt.

Understanding swap memory in Linux

Understand that your Linux system can use Virtual Memory to allow it to run more programs that it has Physical Memory to support.

Physical Memory Distribution
Physical Memory Distribution

For example, let’s suppose we have this Linux system here and this green box represents the total amount of RAM installed in the system.

Well the operating system itself is going to use a big chunk of that available RAM just to provide operating system functions and then each application running on the system is going to use a chunk of memory as well.

Now this is just a basic representation there are many more processes running on the system besides the four that we’ve drawn here.

So, in this situation you can see that most of our Physical Memory has been consumed.

We have just a little tiny bit at the top there that can still be used for programs.

Let’s suppose I need to run another program, and this is a fairly big program it’s going to consume that much memory.

So, if I try to run this process in the memory available it won’t work it’s too big.

This program will consume a lot of memory and as you can see in the diagram we do not have that much free memory available.

Free memory for new Program
Free memory for new Program

How does swap memory work?

Now in order to make this work what Linux does and most other operating systems for that example is take a look at the various processes that are already running in memory and the kernel will decide which of those processes isn’t really doing a whole lot at the moment.

Because, If you think about it there may be hundreds of processes running on your Linux system of which a lot of them are just sitting there waiting for something to happen.

In which case they don’t actually need to consume our Physical RAM.

So, what we do is take those processes and we move them out of physical RAM and put them on to a hard disk drive.

Let’s say we are moving process-1 from Physical Memory to a Hard Disk drive.

Moving process-1 from Physical Memory to a Hard Disk drive.
Moving process-1 from Physical Memory to a Hard Disk drive.

Essentially, we map memory locations that don’t exist in physical memory but instead on one of our hard disk drives in a partition on the hard disk.

Now the processes themselves they’re being swapped don’t realize that they are not running in memory any more they just think they’re being moved to a different memory location.

The process itself has no idea that that memory location is actually in a hard disk drive.

Now the hard disk drive is much slower than Physical Memory.

So, we don’t want to put anything in the swap area on the hard disk drive that’s being actively used.

Because it’s going to run painfully slow but for a process that’s just sitting there waiting for something to happen that works just fine.

By doing this we free up space and so the additional application that we want to run is able to be inserted into physical memory and run.

And then if process-1 application needs to be used again for some reason may be and a certain event has happens this triggered it to start doing things again then we will take it back out of the swap area and put it back in to Physical Memory.

And we’ll look for some other application that currently isn’t doing anything may be that application we just loaded is done with this job it’s just sitting there waiting for something to happen.

So, we can then take it out and put it in to the swap area.

That’s why we call this swap because we’re constantly moving processes in and out of that area and by doing this the system can run more processes than it physically has RAM to support.

Creating swap partition in Linux

Now on a Linux system you can use either a partition or an entire hard disk for this Virtual Memory for a swap area.

Although my experience has been that you rarely use an entire hard disk that’s just way too much space instead what we do is define a partition on the hard drive and use it for Virtual Memory.

Now be aware that a Linux system can actually use more than one swap partition.

In fact, if you have a very heavily used system having multiple swap partitions can be really beneficial if you put those swap partitions on multiple storage devices.

In this case I’m putting an extra hard disk in the system, I’m defining a swap area on it as well as on the main hard disk.

And by doing this the system has two places where it can swap data back and forth from physical memory to swap partition and by doing this we can actually speed up the system a fairly considerable amount depending upon how heavily it’s used.

System has two places where it can swap data
System has two places where it can swap data

If you have a lightly used system using multiple swap areas isn’t all that effective but on a heavily used system like a server having two or three or more swap areas can really pick up performance.

Recommended System swap Space:

Recommended System swap Space
Recommended System swap Space

Source: redhat.com

Step: 1 Create a standard Linux Partition

Now just like a data partition a partition used for Virtual Memory for swapping has to be prepared with the mkswap command before you can actually use it.

Now of course before you do this you have to create the partition first and you also need to set the partition type to type 82.

The default if you use fdisk or gdisk is to create a type 83 partition which is a Linux data partition.

So, let’s create a partition.

~$ sudo fdisk /dev/sdb

Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x04ad2d84.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-6291455, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-6291455, default 6291455): +2G

Created a new partition 1 of type 'Linux' and of size 2 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

~$ sudo partprobe /dev/sdb

As you can see this is a standard partition with partition type 83.

~$ sudo fdisk /dev/sdb

Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/sdb: 3 GiB, 3221225472 bytes, 6291456 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
Disklabel type: dos
Disk identifier: 0x04ad2d84

Device     Boot Start     End Sectors Size Id Type
/dev/sdb1        2048 4196351 4194304   2G 83 Linux

Suggested Read: fdisk Command: How to Create Disk Partitions (MBR) in Linux

Step: 2 Change the Partition Type

You use it for swap memory you have to first use the fdisk utility or the gdisk utility to change its type to 82 for fdisk or 8200 for gdisk.

So let’s change the partition type using the fdisk command.

~$ sudo fdisk /dev/sdb

Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write 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'.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

[email protected]:~$ sudo partprobe /dev/sdb

And as you can see that changes its file system type to swap partition.

~$ sudo fdisk /dev/sdb

Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/sdb: 3 GiB, 3221225472 bytes, 6291456 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
Disklabel type: dos
Disk identifier: 0x04ad2d84

Device     Boot Start     End Sectors Size Id Type
/dev/sdb1        2048 4196351 4194304   2G 82 Linux swap / Solaris

Step: 3 Format the new swap space

The syntax for using mkswap has shown here.

mkswap [options] device [size]

We enter mkswap followed by the device file name for the partition that we want to format as a swap partition.

For example, if you had a partition /dev/sdb1 that you wanted to format as a swap partition, you would enter mkswap and then the device file name /dev/sdb1.

~$ sudo mkswap /dev/sdb1
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=a7045220-d490-4e1d-840f-17fca032f243

Step: 4 Activate the swap Space

Now after you create the swap partition with the mkswap command you then need to activate it and this is done using swapon command.

You enter swapon followed by the device file name of the swap partition you just created with mkswap, In this case we’re entering the following command.

~$ sudo swapon /dev/sdb1

Step: 5 Display swap usage Summary

If you want to view a listing of all the swap areas that have been defined and enabled on your system use the swapon command with the -s parameter.

Then you can see the swap areas that are currently being used on your system.

~$ sudo swapon -s
Filename				Type		Size	Used	Priority
/dev/sdc1                              	partition	2097148	0	-1
/dev/sdb1                              	partition	2097148	0	-2

OR

~$ sudo swapon --show
NAME      TYPE      SIZE USED PRIO
/dev/sdc1 partition   2G   0B   -1
/dev/sdb1 partition   2G   0B   -2

Alternately you can also type the following command to get the same output.

~$ cat /proc/swaps 
Filename				Type		Size	Used	Priority
/dev/sdc1                               partition	2097148	0	-1
/dev/sdb1                               partition	2097148	0	-2

In this case you can see that I have two swap partitions on two different storage devices.

You can also see the priority of those devices over here.

The one with a priority of -1 is going to be used Primarily and the one with a value of -2 will be used as the Secondary one.

Basically, it only be used if for some reason this first one is busy and can’t be used.

Step: 6 Permanently mount the swap Space

To mount the swap partition permanently, enter the following line in the /etc/fstab file.

~$ sudo nano /etc/fstab

UUID=a7045220-d490-4e1d-840f-17fca032f243	swap	swap	defaults	0	0

You can use blkid to get the UUID number of any disk.

~$ sudo blkid /dev/sdb1 
/dev/sdb1: UUID="a7045220-d490-4e1d-840f-17fca032f243" TYPE="swap" PARTUUID="04ad2d84-01"

Suggested Read: How to Mount and Unmount File Systems in Linux

Deactivate the swap Space

Now after you’ve created and enabled a swap area for some reason you need to disable it, you use the swapoff command.

As you can see here swapoff followed by the device file name of the swap partition.

~$ sudo swapoff /dev/sdb1

Enable all swap Space

Also be aware that if you have multiple swap partitions on your system and you want to activate them all at the same time instead of going through one at a time then you can just use the swapon -a command.

~$ sudo swapon -a

And that will just go through your /etc/fstab file find all the swap partitions that are contained therein and immediately enable them and put them into service.

Disable all swap Space

You can do the opposite for some reason you need to disable all of your swap partitions which frankly I don’t think I’ve ever done.

You can use the swapoff command with the -a and that’ll again go through /etc/fstab file and deactivate all of your swap partitions.

~$ sudo swapoff -a

Display Physical and Swap Memory usage in Linux

To display amount of free and used memory in the system run the following command.

~$ free -h
              total        used        free      shared  buff/cache   available
Mem:           1.9G        1.3G         74M        9.3M        568M        463M
Swap:          4.0G         19M        4.0G

Get more information on swap space

Conclusion

I hope that now you have a good understanding of How to create swap partition in Linux.

If anyone does have any questions about what we covered in this guide then feel free to ask in the comment section below and I will do my best to answer those.

If you like our content, please consider buying us a coffee.

Buy Me A Coffee

We are thankful for your never ending support.

Leave a Comment