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.
- 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.
- 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. - 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 offdisk
you are using). - 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. - Activate the swap partition: Use the
mkswap
command to initialize the new swap partition, and then use theswapon
command to activate it. You can verify that the swap partition is active by using theswapon -s
command, which will show a list of all active swap spaces on the system. - 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
- Replace
sdXN
with the device name and partition number of the swap partition (for example,/dev/sda2
). - 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.