Swap is an essential part of Linux systems, especially for systems with limited physical memory. It provides a backup for the RAM, ensuring that the system runs smoothly even when the RAM is fully utilized. Swap can exist as a file or a partition.
For performance reasons, a dedicated swap partition is preferred over a swap file, especially for systems with a large amount of RAM. This article details the steps to add a swap partition in Linux using existing partitions.
Using tools such as lsblk, you can get a list of existing partitions and decide which one to use or resize to make space for the swap partition. Before proceeding, ensure you've backed up any critical data, as adjusting partitions can lead to data loss.
Steps to add a swap partition in Linux:
- Check existing partitions using lsblk.
$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 238.5G 0 disk ├─sda1 8:1 0 200M 0 part /boot/efi └─sda2 8:2 0 238.3G 0 part /
Identify an unused partition or space where you'd like to create the wap partition.
- Convert the chosen partition to swap. Replace X with your actual partition number.
$ sudo mkswap /dev/sdX
- Activate the new swap partition.
$ sudo swapon /dev/sdX
- Update the fstab file to mount the swap partition during boot.
$ echo "/dev/sdX none swap sw 0 0" | sudo tee -a /etc/fstab
- Confirm that the swap partition is active.
$ cat /proc/swaps Filename Type Size Used Priority /dev/sdX partition 2097148 0 -1
- Check swap usage using the free command.
$ free -h total used free shared buff/cache available Mem: 1.9G 155M 1.2G 8.8M 566M 1.6G Swap: 2.0G 0B 2.0G
Ensure that the Swap line reflects the correct size and usage of your swap partition.
Now, your system is set up with a new swap partition, ensuring smoother performance even when RAM usage is high. Remember that the speed of accessing data from swap, especially if it's on a traditional HDD, is slower than RAM. Therefore, always monitor your system's RAM usage and consider adding more physical RAM if frequently full.
Mohd Shakir Zakaria is an experienced cloud architect with a strong development and open-source advocacy background. He boasts multiple certifications in AWS, Red Hat, VMware, ITIL, and Linux, underscoring his expertise in cloud architecture and system administration.
Comment anonymously. Login not required.