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 a swap partition using the fdisk and fallocate commands

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article introduces how to use fdisk and fallocate commands to create swap partitions, the content is very detailed, interested friends can refer to, hope to be helpful to you.

The swap partition is used to maintain the contents of the memory when the physical memory (RAM) is filled. When RAM is exhausted, Linux moves inactive pages in memory to swap space, freeing up memory for system use. Even so, swap space should not be considered a substitute for physical memory.

In most cases, it is recommended that the size of swap memory be 1 to 2 times the size of physical memory. That is, if you have 8GB memory, the swap space should be between 8-16 GB.

If the swap partition is not configured in the system, when the memory is exhausted, the system may kill the running process / application and cause the system to crash. In this article, we will learn how to add swap partitions to Linux systems. There are two ways to do this:

Use the fdisk command

Use the fallocate command

* methods (using the fdisk command)

Typically, the system's * * block hard disk will be named / dev/sda, and the partitions in it will be 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 do * * 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.

On how to use fdisk and fallocate commands to create swap partition to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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: 0

*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

Servers

Wechat

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

12
Report