Home Linux ServerLinux Fundementals Disable Swap in Linux

Disable Swap in Linux

by Thạch Phạm
Published: Last Updated on

In this tutorial, AZDIGI will show you how to disable Swap in Linux.

I. What is Swap?

Swap, also known as virtual RAM, is used to support storing data in hard disk memory when physical memory (RAM) is full. Sometimes Swap is also used in parallel to increase cache capacity even if the physical memory is not used up yet. Swap is usually used by default on Linux, Ubuntu or CentOS operating systems.

II. When should we use Swap?

One of the most important situations where a Swap is needed is when your system is running out of physical memory (RAM).

It can be understood simply: you can never predict the amount of memory for all programs on the operating system. So when the problem occurs (memory overflow RAM), Swap will do the job of maintaining all normal operations even though the speed is significantly slower instead of stopping the whole system, making the data vulnerable because it has not been written to the hard disk.

III. When do we not need to use Swap?

In case your server has enough RAM or does not require Swap space usage or having Swap significantly reduces your system performance, then you should consider disabling the Swap partition.

IV. How to disable SWAP in Linux

To disable SWAP in Linux, we follow these 3 steps.

Step 1: SSH into your Linux system

To disable SWAP in Linux, we first need to SSH or access your VPS/server with root privileges. If you don’t know how to SSH into your VPS/Server, you can refer to the following tutorial:

After successfully SSH, we continue with step 2 to examine before disabling Swap in Linux.

Step 2: Investigate physical memory usage

Before actually disabling swap space, you need to first visualize the physical memory usage of the server and then determine the partition containing Swap, by running the commands below.

AZDIGI Tutorial
free -h

Here are the results of my VPS.

Check Swap partition usage.

Find the used size of Swap, here mine is 23M (23 Megabytes). If the size used is 0B or close to 0 Bytes like mine, it is understandable that the Swap partition is not used much and can be safely disabled.

Next, we use the blkid command, find the line TYPE=”swap” to identify the swap partition, as shown below:

AZDIGI Tutorial
Define Swap partition with commandblkid .

Or alternatively, you can use the commandlsblk following to search and identify the [SWAP] partition as below:

Define Swap partition with command lsblk .

So both results give Swap partition:

AZDIGI Tutorial

Now let’s go to step 3 to start disabling Swap in Linux.

Step 3: Execute the commands to disable Swap in Linux

After identifying the Swap partition in step 2, we execute one of the following two commands to disable Swap in Linux.

AZDIGI Tutorial
swapoff /dev/mapper/centos-swap
swapoff -a

After running one of the two commands above, you can recheck with the free -h command to check if the Swap partition has been disabled.

AZDIGI Tutorial
free -h

The Swap partition has been disabled.

To permanently disable the Swap partition in Linux, open the /etc/fstab file, search for the Swap line and disable the whole line by adding a # (hashtag) in front of the line.

Permanently Disable Swap Partition in Linux.

Then save it and restart the Linux server with thereboot command.

AZDIGI Tutorial

So we have finished disabling Swap in Linux.

V. Summary

With just 3 simple steps above, you can disable Swap in Linux. Remember to only disable Swap when absolutely necessary.

Hope this article will be of help to you. Wishing you success!

If you need assistance, you can contact support in the ways below:

Đánh giá

Tham gia nhóm hỗ trợ Server - Hosting

Tham gia nhóm Hỗ trợ Server - Hosting & WordPress để cùng nhau hỏi đáp và hỗ trợ các vấn đề về WordPress, tối ưu máy chủ/server.

Tham gia ngay

Bài viết cùng chuyên mục

AZDIGI – Không chỉ là đơn vị hàng đầu trong lĩnh vực Web Hosting và Máy chủ, chúng tôi mong muốn mang lại những kiến thức bổ ích nhất và luôn cập nhật thường xuyên cho cộng đồng người đam mê thiết kế website, công nghệ,…

Vui lòng không sao chép nội dung nếu chưa xin phép. Designed and Developed by PenciDesign