How to create swap partition in Linux?

Swap partitions are an important component of any Linux system, as they allow the operating system to use a portion of the hard drive as additional memory when the system’s physical RAM is full.

In this article, we will explain how to create a swap partition on a Linux system. We will cover the process in detail, including how to determine the size of the swap partition, create the partition using the fdisk command, initialize and activate the partition using the mkswap and swapon commands, and add the partition to the system’s /etc/fstab file to ensure that it is activated automatically during the boot process.

By following these steps, you will be able to set up a swap partition on your Linux system and take advantage of the benefits of virtual memory.

  1. Determine the size of the swap partition: The general rule of thumb is to allocate twice as much swap space as the amount of physical RAM in the system. For example, if your system has 8GB of RAM, you would want to create a 16GB swap partition.
  2. Check the current partition table: Use the fdisk command to view the current partition table and determine which hard drive you want to use for the swap partition.
  3. Create a new partition: Use the fdisk command to create a new partition on the hard drive. Choose the option to create a new partition, and specify the size and location of the partition. Make sure to set the partition type to “Linux swap” or “swap” (the exact name may vary depending on the version of fdisk you are using).
  4. Write the changes to the partition table: After you have created the new partition, use the fdisk command to write the changes to the partition table. This will create the new swap partition on the hard drive.
  5. Activate the swap partition: Use the mkswap command to initialize the new swap partition, and then use the swapon command to activate it. You can verify that the swap partition is active by using the swapon -scommand, which will show a list of all active swap spaces on the system.
  6. Add the swap partition to the system: To ensure that the swap partition is activated automatically every time the system boots, you will need to add an entry for it in the /etc/fstab file. Use a text editor to add a line to the file in the following format:/dev/sdXN none swap sw 0 0
  7. Replace sdXN with the device name and partition number of the swap partition (for example, /dev/sda2).
  8. Reboot the system: After you have completed these steps, reboot the system to ensure that the changes take effect. The swap partition should now be active and available for use as virtual memory.

Keep in mind that these steps are a general outline, and the exact details may vary depending on the version of Linux you are using and the configuration of your system. If you are not familiar with partitioning and Linux system administration, it is recommended to seek the assistance of a more experienced user or a system administrator.

This was the tutorial on how to create a swap partition on your Linux system but if you are confused between a swap partition and a swap file, then here is how you can create a swap file. Try both out and check out the results.

Summary
How to create swap partition in Linux?
Article Name
How to create swap partition in Linux?
Description
This tutorial will show you step-by-step how to create, format, and activate a swap partition on your system, to optimize your system's performance.
Author

Leave a Comment