linux创建分区_在Linux中创建分区-分步指南

linux创建分区

In this tutorial, we’ll be covering the steps to create a partition in Linux. This can help you allocate different memory regions for specific uses. Creating partitions can also help you install multiple operating systems on your machine and minimize the damage in case of disk corruption. 

在本教程中,我们将介绍在Linux中创建分区的步骤。 这可以帮助您为特定用途分配不同的内存区域。 创建分区还可以帮助您在计算机上安装多个操作系统,并在磁盘损坏的情况下最大程度地减少损坏。

如何在Linux中创建分区? (How to Create a Partition in Linux?)

In this tutorial, we will utilize the fdisk command to create a disk partition. The fdisk utility is a text-based command-line utility for viewing and managing disk partitions on a Linux system.

在本教程中,我们将使用fdisk命令创建磁盘分区。 fdisk实用程序是基于文本的命令行实用程序,用于查看和管理Linux系统上的磁盘分区。

Before we create a partition on our system, we need to list all the partitions on our system. This is essential as we need to choose a disk before we partition it. 

在系统上创建分区之前,我们需要列出系统上的所有分区。 这是至关重要的,因为我们需要在分区之前选择一个磁盘。

To view all the partitions currently on your system, we use the following command.

要查看您系统上当前的所有分区,我们使用以下命令。


sudo fdisk -l

You might be prompted to enter your password again to verify your sudo privileges. Here we called the fdisk command with the -l to list the partitions. You should get an output similar to the following.

可能会提示您再次输入密码以验证您的sudo权限。 在这里,我们使用-l调用fdisk命令以列出分区。 您应该获得与以下类似的输出。

Fdisk L
Fdisk List
Fdisk列表

Now, we choose one disk from this list to partition. For this tutorial, we will choose the disk. To create partitions, we use the ‘command mode’ of the fdisk command. To enter the command mode, we use this command in our terminal.

现在,我们从该列表中选择一个磁盘进行分区。 在本教程中,我们将选择磁盘。 要创建分区,我们使用fdisk命令的“命令模式”。 要进入命令模式,我们在终端中使用此命令。


sudo fdisk [disk path]

If you see an output similar to this, you have successfully entered the command mode.

如果看到类似的输出,则说明您已成功进入命令模式。

Partitioning A Disk
Partitioning A Disk
分区磁盘

使用命令模式 (Using the command mode)

Once we enter the command mode, many beginners might get confused due to the unfamiliar interface. The command mode of fdisk uses single character commands to specify the desired action for the system. You can get a list of available commands by pressing ‘m’, as shown below.

进入命令模式后,由于界面不熟悉,许多初学者可能会感到困惑。 fdisk的命令模式使用单字符命令来指定系统所需的操作。 您可以通过按“ m”键获得可用命令的列表,如下所示。

Fdisk M
Fdisk M
软盘

创建一个分区 (Creating a partition)

Our main objective here is to create a partition. To create a new partition, we use the command ‘n’. This will prompt you to specify the type of partition which you wish to create.

我们这里的主要目标是创建一个分区。 要创建一个新分区,我们使用命令'n' 。 这将提示您指定要创建的分区类型。

If you wish to create a logical partition, choose ‘l’. Alternatively, you can choose ‘p’ for a primary partition. For this tutorial, we will create a primary partition.

如果要创建逻辑分区,请选择“ l”。 或者,您可以为主要分区选择“ p” 。 在本教程中,我们将创建一个主分区。

Create Partition
Create Partition
创建分区

Now, we will be asked to specify the starting sector for our new partition. Press ENTER to choose the first available free sector on your system. Next, you’ll be prompted to select the last sector for the partition.

现在,将要求我们为新分区指定起始扇区。 按ENTER键选择系统上第一个可用的空闲扇区。 接下来,系统将提示您选择分区的最后一个扇区。

Either press ENTER to use up all the available space after your first sector or specify the size for your partition.

按ENTER键用完第一个扇区后的所有可用空间,或指定分区的大小。

Sector Type
Sector Type
行业类型

As shown in the screenshot above, we chose to create a 10 MB partition for this demonstration. Here ‘M’ specifies the unit as megabytes. You can use ‘G’ for gigabytes.

如上面的屏幕快照所示,我们选择为该演示创建一个10 MB的分区。 在这里,“ M”将单位指定为兆字节。 您可以使用'G'表示千兆字节。

If you don’t specify a unit, the unit will be assumed to be sectors. Hence +1024 will mean 1024 sectors from the starting sector.

如果未指定单位,则将假定该单位为扇区。 因此,+ 1024表示从起始扇区开始的1024个扇区。

设置分区类型 (Setting the partition type)

Once we create a partition, Linux sets the default partition type as ‘Linux’. However, suppose we wish my partition type to be the ‘Linux LVM’ partition. To change the ID for our partition, we will use the command ‘t’. 

创建分区后,Linux将默认分区类型设置为“ Linux”。 但是,假设我们希望我的分区类型是“ Linux LVM”分区。 要更改分区的ID,我们将使用命令“ t”。

Now, we get prompted to enter the HEX code for our desired partition ID. We don’t remember the HEX code for the partition types on top of our heads.

现在,系统提示您输入所需分区ID的十六进制代码。 我们不记得最容易想到的分区类型的十六进制代码。

So we will take the help of the ‘L’ command to list all the HEX codes for the available partition types. This list should look as shown below.

因此,我们将借助“ L”命令列出可用分区类型的所有十六进制代码。 该列表应如下所示。

Partition Type 1
Partition Type List
分区类型列表

We see that the HEX code 8e is the partition ID for the ‘Linux LVM’ partition type. Hence, we will enter the required HEX code. The following output gives us the confirmation that our partition ID has been changed successfully. 

我们看到,十六进制代码8e是“ Linux LVM”分区类型的分区ID。 因此,我们将输入所需的十六进制代码。 以下输出使我们确认分区ID已成功更改。

Partition Code
Partition Code
分区码

完成变更 (Finalising the changes)

Now that we have created a new partition and given it our desired partition ID, we need to confirm our changes. All the changes made until this point are saved in the memory, waiting to be written on our disk.

现在,我们已经创建了一个新分区并为其提供了所需的分区ID,我们需要确认所做的更改。 到此为止所做的所有更改都保存在内存中,等待写入我们的磁盘中。

We use the command ‘p’ to see the detailed list of partitions for our current disk as seen in the screenshot below.

我们使用命令“ p”来查看当前磁盘分区的详细列表,如下面的屏幕快照所示。

Create a Partition
List of all partitions
所有分区列表

This allows us to confirm all the changes we have done to the disk before making them permanent. Once you have verified the changes, press ‘w’ to write the new partition on your disk.

这使我们可以在对磁盘进行永久更改之前确认对磁盘所做的所有更改。 确认更改后,按“ w”将新分区写入磁盘。

If you don’t wish to permanently write your new partition to the disk, you can enter the command ‘q’. This will exit the fdisk command mode without saving any changes.

如果您不想将新分区永久写入磁盘,则可以输入命令“ q”。 这将退出fdisk命令模式而不保存任何更改。

格式化分区 (Formatting a partition)

Once you create a new partition, it is advisable to format your new partition using the appropriate mkfs command.

创建新分区后,建议使用适当的mkfs命令格式化新分区。

This is because using a new partition without formatting it may cause issues in the future. To see the list of all available mkfs commands, we enter the following in our command line.

这是因为使用新分区而不进行格式化可能会在将来引起问题。 要查看所有可用的mkfs命令的列表,请在命令行中输入以下内容。


sudo mkfs

This gives us a list of available mkfs commands. If we wish to format a partition on our current disk with the ext4 file system, we use this command.

这为我们提供了可用的mkfs命令列表。 如果希望使用ext4文件系统格式化当前磁盘上的分区,请使用此命令。


sudo mkfs.ext4 [partition path]

结语 (Wrapping up)

That’s it! You now know how to create a partition in Linux using the fdisk command.. You can reserve space for specific tasks. And in case one partition gets corrupted, you don’t need to worry about data on other partitions.

而已! 现在,您知道如何使用fdisk命令在Linux中创建分区。您可以为特定任务保留空间。 而且,如果一个分区损坏了,则无需担心其他分区上的数据。

As each partition is treated as a separate disk, data on other partitions remains safe. The fdisk utility is a powerful tool for the task of managing disk partitions, but it can often be confusing for new users.

由于每个分区都被视为一个单独的磁盘,因此其他分区上的数据仍然安全。 fdisk实用程序是用于管理磁盘分区的功能强大的工具,但是对于新用户而言,它通常会造成混乱。

We hope this tutorial was able to help you understand how to create a new disk partition in Linux using the fdisk utility. If you have any feedback, queries or suggestions, feel free to reach out to us in the comments below.

我们希望本教程能够帮助您了解如何使用fdisk实用程序在Linux中创建新的磁盘分区。 如果您有任何反馈,疑问或建议,请随时通过以下评论与我们联系。

翻译自: https://www.journaldev.com/40113/create-a-partition-in-linux

linux创建分区

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值