Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create swap Partition under Linux

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to create a swap partition under Linux". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to create a swap partition under Linux.

Linux's swap partition (swap), or memory replacement space (swap space), is an area of disk that can be a partition, a file, or a combination of them. The function of swap partition is that when the system's physical memory is tight, Linux will save the infrequently accessed data in memory to swap, so that the system will have more physical memory to serve each process, and when the system needs to access the content stored on swap, it will load the data on swap into memory, that is, swap out and swap in.

The first method (using the fdisk command)

Typically, the first hard drive of the system is named / dev/sda, and the partitions in it are named / dev/sda1, / dev/sda2. In this article, we use a hard disk with two primary partitions, the two partitions are / dev/sda1 and / dev/sda2, and we use / dev/sda3 to swap partitions.

First create a new partition

$fdisk / dev/sda

Press n to create a new partition. The system will ask you which cylinder to start from, just press enter to use the default value. Then the system asks you which cylinder to end, and here we enter the size of the swap partition (such as 1000MB). Here we enter + 1000m.

Swap

Now we have created a disk with the size of 1000MB. But we didn't set the partition type, we press t and enter to set the partition type.

Now we are going to enter the partition number, here we enter 3, then enter the disk classification number, swap the partition type is 82 (to display all available partition types, press l), and then press w to save the disk partition table.

Swap

Next, use the mkswap command to format the swap partition:

$mkswap / dev/sda3

Then activate the new swap partition:

$swapon / dev/sda3

However, our swap partition will not be mounted automatically after reboot. To achieve permanent mount, we need to add content to the / etc/fstab file. Open the / etc/fstab file and enter the following line:

$vi / etc/fstab/dev/sda3 swap swap default 0 0

Save and close the file. Now you can use our swap partition every time you restart.

The second method (using the fallocate command)

I recommend this method because it is the easiest and fastest way to create swap space. Fallocate is one of the most undervalued and least used commands. The fallocate command is used to pre-allocate blocks / sizes for files.

To create swap space using fallocate, we first create a file called swap_space under the / directory. Then assign 2GB to the swap_space file:

$fallocate-l 2G / swap_space

We run the following command to verify the file size:

$ls-lh / swap_space

Then change the file permissions to make / swap_space more secure:

$chmod 600 / swap_space

This way only root can read and write the file. Let's format the swap partition again (although this swap_space is a file, we mount it as if it were a partition):

$mkswap / swap_space

Then enable swap space:

$swapon-s

The disk partition is remounted after each reboot. So to make it persistent, as above, we edit / etc/fstab and enter the following line:

/ swap_space swap swap sw 0 0

Save and exit the file. Now our swap partition will be mounted all the time. After reboot, we can run free-m on the terminal to check whether the swap partition is in effect.

At this point, I believe you have a deeper understanding of "how to create a swap partition under Linux". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 216

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report