Skip to main content

How to Create and Manage Swap Partition in Linux

 

Swap partition is a space on your hard disk that serves as overflow for your RAM. It can help you run more applications, prioritize data, and hibernate your system. In this article, we will explain how to create, size, and manage a swap partition in Linux with practical examples.

How to Create the Swap Partition

To create a swap partition, you need to have some free space on your hard disk. You can use a tool like fdisk or gparted to create a new partition or resize an existing one. For this example, we will use fdisk to create a new swap partition of 2 GB on /dev/sda.

First, run sudo fdisk /dev/sda to enter the interactive mode of fdisk. You will see a prompt like this:

Command (m for help):

Type m to see the list of available commands. You will see something like this:

Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Type p to print the current partition table. You will see something like this:

Disk /dev/sda: 20.0 GB, 20000000000 bytes
255 heads, 63 sectors/track, 2432 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        1913    15360000   83  Linux
/dev/sda2            1914        2432     4161536   83  Linux

Note the size and the end of the last partition. In this case, it is /dev/sda2 with 4 GB and ending at cylinder 2432. We will use this information to create a new partition after it.

Type n to add a new partition. You will see a prompt like this:

Command action
   e   extended
   p   primary partition (1-4)

Type p to create a primary partition. You will see a prompt like this:

Partition number (1-4):

Type 3 to create the third partition. You will see a prompt like this:

First cylinder (1915-2432, default 1915):

Type 1915 to start the partition from the next available cylinder. You will see a prompt like this:

Last cylinder, +cylinders or +size{K,M,G} (1915-2432, default 2432):

Type +2G to specify the size of the partition as 2 GB. You will see a message like this:

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (1915-2432, default 1915): 1915
Last cylinder, +cylinders or +size{K,M,G} (1915-2432, default 2432): +2G

Command (m for help):

Type p again to print the new partition table. You will see something like this:

Disk /dev/sda: 20.0 GB, 20000000000 bytes
255 heads, 63 sectors/track, 2432 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        1913    15360000   83  Linux
/dev/sda2            1914        2432     4161536   83  Linux
/dev/sda3            1915        2178     2113536   83  Linux

Note the size and the device name of the new partition. In this case, it is /dev/sda3 with 2 GB.

How to Assign Its Type in Fdisk

To use the new partition as swap space, we need to change its type from Linux (83) to Linux swap (82). To do this, type t to change a partition’s system id. You will see a prompt like this:

Partition number (1-4):

Type 3 to select the third partition. You will see a prompt like this:

Hex code (type L to list codes):

Type 82 to change the type to Linux swap. You will see a message like this:

Changed system type of partition 3 to 82 (Linux swap / Solaris)

Type p again to print the new partition table. You will see something like this:

Disk /dev/sda: 20.0 GB, 20000000000 bytes
255 heads, 63 sectors/track, 2432 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        1913    15360000   83  Linux
/dev/sda2            1914        2432     4161536   83  Linux
/dev/sda3            1915        2178     2113536   82  Linux swap / Solaris

How to Format and Activate the Swap Partition

To use the swap partition, we need to format it with the mkswap command and activate it with the swapon command. To do this, run the following commands:

sudo mkswap /dev/sda3
sudo swapon /dev/sda3

The mkswap command will create a swap area on the partition and print a message like this:

Setting up swapspace version 1, size = 2113532 KiB
no label, UUID=12345678-1234-1234-1234-123456789012

The swapon command will enable the swap space and print no message.

To verify that the swap partition is active, run the free -m command to see the memory usage. You will see something like this:

             total       used       free     shared    buffers     cached
Mem:          1000        800        200          0         50        300
-/+ buffers/cache:        450        550
Swap:         2000          0       2000

Note the swap size and usage. In this case, it is 2000 MB and 0 MB.

How to Deactivate the Swap Partition

To deactivate the swap partition, you can use the swapoff command. To do this, run the following command:

sudo swapoff /dev/sda3

The swapoff command will disable the swap space and print no message.

To verify that the swap partition is inactive, run the free -m command again. You will see something like this:

             total       used       free     shared    buffers     cached
Mem:          1000        800        200          0         50        300
-/+ buffers/cache:        450        550
Swap:            0          0          0

Note the swap size and usage. In this case, it is 0 MB and 0 MB.

How to Make Entry of the Swap Partition in /etc/fstab

The /etc/fstab file is a configuration file that contains information about the filesystems and partitions that are mounted at boot time. To make the swap partition persistent across reboots, you need to add an entry of it in the /etc/fstab file. In this article, we will explain how to find the UUID of the swap partition and how to edit the /etc/fstab file with an example.

Finding the UUID of the Swap Partition

The UUID (Universally Unique Identifier) is a unique string that identifies a partition or a device. It is recommended to use the UUID instead of the device name (such as /dev/sda3) in the /etc/fstab file, because the device name may change depending on the hardware configuration or the boot order.

To find the UUID of the swap partition, you can use the blkid command. For example, if your swap partition is /dev/sda3, you can run the following command:

sudo blkid /dev/sda3

You will see an output like this:

/dev/sda3: UUID="12345678-1234-1234-1234-123456789012" TYPE="swap"

Note the UUID value of the swap partition. In this case, it is 12345678-1234-1234-1234-123456789012.

Editing the /etc/fstab File

To edit the /etc/fstab file, you need to use a text editor such as nano or vi. For this example, we will use nano.

First, run sudo nano /etc/fstab to open the file in nano. You will see something like this:

UUID=ef6ba050-6cdc-416a-9380-c14304d0d206 / xfs defaults 0 0

These are the existing entries in the /etc/fstab file. They are the partition on the existing hard drive /dev/sda1, and the swap file system. Be careful not to alter these entries.

We need to add a new entry to the /etc/fstab file for the swap partition. The format of the entry is:

UUID= device_UUID none swap defaults 0 0

Where device_UUID is the UUID of the swap partition that we found earlier.

For example, if the UUID of the swap partition is 12345678-1234-1234-1234-123456789012, the entry would be:

UUID=12345678-1234-1234-1234-123456789012 none swap defaults 0 0

Add this entry to the end of the /etc/fstab file and save the file.

Testing the /etc/fstab File Without Rebooting

To test the /etc/fstab file without rebooting, you can use the mount -a command. This command will mount all the filesystems listed in the /etc/fstab file that are not already mounted.

Run the following command:

sudo mount -a

If there is no error message, it means that the /etc/fstab file is valid and the swap partition is mounted correctly.

To verify that the swap partition is active, run the free -m command to see the memory usage. You will see something like this:

             total       used       free     shared    buffers     cached
Mem:          1000        800        200          0         50        300
-/+ buffers/cache:        450        550
Swap:         2000          0       2000

Note the swap size and usage. In this case, it is 2000 MB and 0 MB.


Popular posts from this blog

Cockpit vs. Webmin: A Detailed Comparison for Linux Administration

  Introduction In the realm of Linux system administration, having efficient tools for managing and monitoring servers is crucial. Two popular tools that system administrators often use are  Cockpit  and  Webmin . Both of these tools provide a graphical interface accessible via a web browser, simplifying the management of Linux systems. This blog post will explore what Cockpit and Webmin are, their purposes, a comparison table, and additional information to help you choose the right tool for your needs. What is Cockpit? Overview Cockpit is a web-based graphical interface for managing Linux systems. It is designed to be easy to use, enabling both experienced and novice administrators to manage their systems effectively. Cockpit integrates seamlessly with the system’s existing infrastructure, providing real-time monitoring and management capabilities. Purpose Cockpit is primarily used for: Monitoring system performance and resource usage Managing system services Handli...

How to Set Up Custom Screen Resolution on Fedora 38 Permanently

  If you are using Fedora 38 as your operating system, you may have encountered some issues with the screen resolution. The default resolution may not be suitable for your monitor or your preferences, and you may want to change it to a higher or lower value. However, changing the resolution from the Settings menu may not work properly, or it may not persist after a reboot. In this blog post, I will show you how to set up a custom screen resolution on Fedora 38 permanently using some simple commands and configuration files. The first step is to disable the Wayland display server, which is the default display server for Fedora 38. Wayland is a modern and secure display server, but it may not support some custom resolutions or drivers. To disable Wayland, you need to edit the /etc/gdm/custom.conf file as root. You can use any text editor of your choice, such as nano, vim, or gedit. To open the file with nano, for example, you can type the following command in the terminal: sudo nano ...

Key Concepts and Tools for a Linux System Administrator

  A Linux System Administrator needs to have a comprehensive understanding of various concepts and tools to manage, configure, and maintain Linux systems effectively. Below is a categorized list of essential skills and tools with brief descriptions. Category Key Concepts & Tools Description Operating System Linux Distributions (e.g., Fedora, Ubuntu, CentOS) Knowledge of different Linux distributions, their package management systems, and unique features. Kernel Configuration and Management Understanding how to configure and optimize the Linux kernel for different workloads. System Boot Process (GRUB, systemd) Familiarity with the boot process, bootloaders, and system initialization processes. Command Line Skills Bash Shell Scripting Ability to write and debug shell scripts for automation of tasks. Core Commands (ls, cp, mv, rm, find, grep, awk, sed) Proficiency in using basic and advanced command-line utilities for system management. File System File System Hierarchy Standard (...