How do I attach a swap partition to Linux?


A swap file, often referred to as a “paging file” in Windows or “swap space” in Unix-like operating systems, is a space on a storage device (usually a hard drive or SSD) that is used as virtual memory by the operating system when the physical RAM (Random Access Memory) is fully utilized.

When your computer runs out of physical RAM, it moves data from the RAM to the swap file, freeing up memory for other processes. This process is known as “paging out” or “swapping out.” Conversely, when data is needed from the swap file and there is available RAM, the operating system moves data back from the swap file into the RAM, known as “paging in” or “swapping in.”

While swap files help prevent system crashes due to insufficient memory, excessive swapping can significantly slow down your computer because accessing data from the swap file on a disk is much slower than accessing it from RAM. Therefore, it’s important to have enough physical RAM to avoid excessive swapping.

Swap files are typically managed automatically by the operating system, but users can often adjust settings related to swap file size and behavior to optimize system performance based on their specific needs and hardware configuration.

How to Check Swap Space

To check swap space usage on a Linux system, you can use the free command or the swapon command. Here’s how

Using the free command Open a terminal and type:

  • free -h

This command will display information about both physical memory (RAM) and swap space. Look for the line labelled “Swap” to see the swap space usage.

Using the swapon command Open a terminal and type:

  • swapon –show

create a new partition

  • fdisk /dev/sdb
  • Replace /dev/sdb with your disk identifier.
  • Then, create a new partition by typing n
  • select the partition type (usually primary), set the partition size +20 G
  • and set the partition type to Linux swap (type 82)
  • Finally, save the changes by typing w.
Format the partition: Once the partition is created, format it as swap.
Activate swap:Activate the swap partition.
  • mkswap /dev/sdb1

This command enables the swap on the specified partition.

Make it persistent: To make the swap partition automatically enabled on boot, you need to add an entry to the /etc/fstab file. Open the file in a text editor and add the following line:

  • vim /etc/fstab
Verify: You can verify that the swap partition is active by running
  • swapon –show

That’s it! You’ve successfully created a swap partition on Linux. Make sure you have adequate free space on your disk and that creating a swap partition is the best solution for your system’s performance needs.

Remove a Swap File on Linux
  • swapon -s
  • swapoff dev/sdb1

Remove the Swap File Entry: You might have an entry for the swap file in /etc/fstab to make it persistent across reboots. If so, you should remove this entry to prevent the system from attempting to activate the swap file during boot. Open /etc/fstab in a text editor like nano or vim:

  • vim /etc/fstab
  • Then, remove the line that refers to the swap file.

Delete the Swap File: Once the swap file is disabled and its entry removed from vim /etc/fstab, you can safely delete it using the rm command:

After completing these steps, the swap file should be removed from your system. Make sure to verify that the swap file is no longer present and that swap space usage is as expected.

To create a swap file in an existing Linux data partition, you can follow these steps:

To create a swap file in Linux, you can use the dd command along with the mkswap and swapon commands. Here’s how you can do it:

  • dd if=/dev/zero of=swapfile bs=512MB count=2
  • mkswap /swapfile
  • swapon /swapfile
  • chmod 600 /swapfile

Replace with the desired size of the swap file in megabytes. For example, if you want a 2GB swap file, you would use 2048 as the value for .

To make the swap file permanent, you can add an entry for it in the /etc/fstab file:

  • vim /etc/fstab

This ensures that the swap file is activated automatically during system boot.

Leave a Comment