如何在 Linux 终端下创建新的文件系统/分区
分区 文件系统 CFdisk
![](/data/attachment/album/201511/30/010226fsjxv1ajaavkbjj7.png)


在 Linux 中创建分区或新的文件系统通常意味着一件事：安装 Gnome Parted 分区编辑器（GParted）。对于大多数 Linux 用户而言，这是唯一的办法。不过，你是否考虑过在终端创建这些分区和文件系统？当然可以！以下就是方法！


### 使用 CFdisk 创建一个基本的 Linux 分区


以下是如何在命令行中创建一个基本的 Linux 分区的正确方案。要做的第一件事就是先打开你的终端。若你已打开，你需要找到你想要创建分区的磁盘。这可以使用一个简单的命令来找到。



```
lsblk

```

![cfdisk-lsblk](/data/attachment/album/201511/30/010226qc0o50hcokokm515.png)


当你运行了 `lsblk`，你应该会看到当前系统上每个磁盘的详细列表。看看这个列表，然后找出你想要使用的磁盘。在本文中，我将使用 `sdb` 来进行演示。


在终端输入这个命令。它会显示一个功能强大的基于终端的分区编辑程序。



```
sudo cfdisk /dev/sdb

```

![cfdisk-empty-layout](/data/attachment/album/201511/30/010226wkalek56f5vy5a85.png)


**注意**: 使用在 `lsblk` 命令输出的你想要使用的磁盘来替换 `sdb`。


当输入此命令后，你将进入分区编辑器中，然后访问你想改变的磁盘。


由于磁盘分区的不同，这取决于用户的需求，这部分的指南将在 **如何建立一个分离的 Linux home/root 分区布局**。


首先，需要创建根分区。这需要根据磁盘的字节数来进行分割。我测试的磁盘是 32 GB。


在 CFdisk 中使用键盘上的方向键选择需要分配的空间。你找到后，请使用箭头键选择 [ NEW ]，然后按 Enter 键。


![cfdisk-create-root-partition](/data/attachment/album/201511/30/010227jmz93ou2o3o1b3on.png)


该程序会要求你输入分区大小。一旦你指定好大小后，按 Enter 键。这将被称为根分区（或 /dev/sdb1）。


接下来该创建 home 分区（/dev/sdb2）了。你需要在 CFdisk 中再选择一些空闲分区。使用箭头选择 [ NEW ] 选项，然后按 Enter 键。输入你的 home 分区的大小，然后按 Enter 键来创建它。


![cfdisk-create-home-partition](/data/attachment/album/201511/30/010227trp7cc75z4n0rddr.png)


最后，需要创建交换分区。像前两次一样，先找一些空闲分区，并使用箭头选择 [ NEW ] 选项。之后，算下你 Linux 想使用多大的交换分区。


**注意**: 交换分区通常和计算机的内存差不多大。


![cfdisk-specify-partition-type-swap](/data/attachment/album/201511/30/010227mhjsoscx2qfsz4nz.png)


现在，创建了交换分区，该指定其类型。使用上下箭头来选择它。之后，使用左右箭头选择 [ TYPE ] 。找到 Linux swap 选项，然后按 Enter 键。


![cfdisk-write-partition-table](/data/attachment/album/201511/30/010227hixwtd5boif7qhdv.jpg)


所有分区创建后。然后就是将其写入到磁盘。使用右箭头键，选择 [ WRITE ] 选项，然后按 Enter 键。这将直接将新创建的分布写入到磁盘中。


### 使用 mkfs 创建文件系统


有时候，你并不需要一个整个重新分区，你只想要创建一个文件系统而已。你可以在终端直接使用 `mkfs` 命令来实现。


![cfdisk-mkfs-list-partitions-lsblk](/data/attachment/album/201511/30/010228pcmfycgyw22a2dg2.png)


首先，找出你要使用的磁盘。在终端输入 `lsblk` 找出来。它会打印出列表，之后只要找到你想创建文件系统的分区或盘符。


在这个例子中，我将使用第二个硬盘的 `/dev/sdb1` 作为第一个分区。可以对 `/dev/sdb` 使用 mkfs（这将会使用整个分区）。


![cfdisk-mkfs-make-file-system-ext4](/data/attachment/album/201511/30/010228k5z33xbzdxoxvutx.png)


要在一个特定的分区上创建新文件系统，只需输入



```
sudo mkfs.ext4 /dev/sdb1

```

在终端。应当指出的是，`mkfs.ext4` 可以换成任何你想要使用的的文件系统。


### 结论


虽然使用图形工具编辑文件系统和分区更容易，但终端可以说是更有效的。终端的加载速度更快，点击几个按钮即可。GParted 和其它工具一样，它也是一个完整的工具。我希望在本教程的帮助下，你会明白如何在终端中高效的编辑文件系统。


你是否更喜欢使用基于终端的方法在 Linux 上编辑分区？不管是不是，请在下面告诉我们。




---


via: <https://www.maketecheasier.com/create-file-systems-partitions-terminal-linux/>


作者：[Derrik Diener](https://www.maketecheasier.com/author/derrikdiener/) 译者：[strugglingyouth](https://github.com/strugglingyouth) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![A photograph of the internal components of a hard drive.](https://www.maketecheasier.com/assets/uploads/2023/07/create-file-systems-partitions-linux-00-featured-image.jpg)

Partition editing or making new file systems on Linux usually means one thing: installing the Gnome Parted partition editor (GParted). For most Linux users, this is the only way to go about it. Still, what if you could edit these partitions and file systems right in the terminal? You can! Here’s how!

**Note**: you might want to [back up your partition](https://www.maketecheasier.com/back-up-partitions-linux-apart-gtk/) first before making any changes to your hard disk.

## Creating a Basic Linux Partition Layout with CFdisk

Here’s how to make a basic Linux partition scheme right from the command line.

- The first thing to do is open your terminal. Once you’re there, you’ll need to
[identify what hard drive you’re looking to change](https://www.maketecheasier.com/lsblk-list-storage-information-linux/). This can easily be figured out with one simple command.

lsblk

![cfdisk-lsblk A terminal showing the output of lsblk.](https://www.maketecheasier.com/assets/uploads/2023/07/create-file-systems-partitions-linux-01-basic-lsblk.png)

- Once you’ve run
`lsblk`

, you should get a detailed list of each hard drive currently on your system. Look through this generated list, and figure out the denotation to the drive you want to change. Here I’ll be using`sdb`

for the sake of example.

- In your terminal enter this command. It will launch a powerful terminal-based partition editing program.

sudo cfdisk /dev/sda

![cfdisk-empty-layout A terminal showing the default interface for cfdisk.](https://www.maketecheasier.com/assets/uploads/2023/07/create-file-systems-partitions-linux-02-cfdisk-free-space.png)

**Note:** replace `sda`

with whatever `lsblk`

told you your hard drive was.

When this command is entered, you’ll be inside the partition editor and will have total access to the hard drive you wish to modify.

Since hard drive partitions are different, depending on a user’s needs, this part of the guide will go over how to set up a split Linux home/root system layout. You can [learn more about Linux partition schemes here](https://www.maketecheasier.com/quick-guide-to-linux-partition-schemes/).

To start, a root partition will need to be created. This will require a bit of math as the gigabytes on the hard drive need to be divided up. My test drive is 16 GB.

- In CFdisk using the arrow keys on your keyboard, select some free space. Once you’ve found some, use the arrow key to select “[ NEW ]” and press the
`Enter`key.

- The program will ask you to input the partition size. Once you’ve specified the size, press the
`Enter`key. This will be the root partition (or “/dev/sdb1”).

![cfdisk-create-root-partition A terminal showing the creation of the first partition in cfdisk.](https://www.maketecheasier.com/assets/uploads/2023/07/create-file-systems-partitions-linux-03-create-new-root-partition.png)

- It’s time to create the home partition (/dev/sdb2). Once again, you’ll need to select some free space in CFdisk. Use the arrow key to select the “[ NEW ]” option, and press the
`Enter`key. Input the size of your home partition, and press the`Enter`key to create it.

![cfdisk-create-home-partition A terminal showing the create of the second partition in cfdisk.](https://www.maketecheasier.com/assets/uploads/2023/07/create-file-systems-partitions-linux-04-create-second-partition.png)

- Finally, you need to create the swap partition. Find some free space, and use the arrow key to select the “[ NEW ]” option. After that calculate exactly how big your Linux swap partition needs to be.

**Tip: **Learn more about swap partitions and how you can [configure your system to use swap effectively](https://www.maketecheasier.com/swap-partitions-on-linux/).

- With the swap partition available, it’s time to specify its type. Highlight it with the up and down arrow keys. After that, use the left and right arrow keys to select “[ TYPE ].” Find Linux swap in the menu, and press
`Enter`.

![cfdisk-specify-partition-type-swap A terminal showing the process of changing the partition type in cfdisk.](https://www.maketecheasier.com/assets/uploads/2023/07/create-file-systems-partitions-linux-05-change-partition-type.png)

- All of the partition creation is out of the way. All that’s left is to write it to the disk. Using the right arrow key, select the “[ WRITE ]” option, and press the
`Enter`key. This will write your newly created layout directly to the hard drive.

![cfdisk-write-partition-table A terminal showing the commit process for cfdisk.](https://www.maketecheasier.com/assets/uploads/2023/07/create-file-systems-partitions-linux-06-commit-changes-to-disk.png)

**Tip**: do you know [you can move your Home directory to another partition](https://www.maketecheasier.com/move-home-folder-ubuntu/)? Here is how it is done.

## Using Fdisk to Create Linux Partition Layouts

Aside from cfdisk, you can also use the more traditional `fdisk`

utility to create and modify disk partitions inside Linux. One of the advantages of this approach is that fdisk often comes by default on most Linux distros.

- Run
`fdisk`

along with the disk that you want to partition:

sudo fdisk /dev/sda

![A terminal showing the default interface for fdisk.](https://www.maketecheasier.com/assets/uploads/2023/07/create-file-systems-partitions-linux-08-fdisk-dev-sda.png)

- type
`g`then press`Enter`to wipe your current partition table and recreate it in the newer “GPT” format.

![A terminal showing the disklabel setup for fdisk.](https://www.maketecheasier.com/assets/uploads/2023/07/create-file-systems-partitions-linux-09-create-new-gpt-label.png)

- Type “n 1” then press
`Enter`twice to create the first partition.

![A terminal showing the creation of the first partition in fdisk.](https://www.maketecheasier.com/assets/uploads/2023/07/create-file-systems-partitions-linux-10-create-first-partition.png)

- fdisk will ask for the starting sector of your first partition. Press
`Enter`to select the default value for your disk.

- You need to determine the overall size of your partition. You can use symbols such as M (megabyte), G (gigabyte) and T (terabyte) to refine your partition’s size. For example, “+8G” will set your partition at exactly 8 gigabytes.

![A terminal showing the output for the partition creation process in fdisk.](https://www.maketecheasier.com/assets/uploads/2023/07/create-file-systems-partitions-linux-11-set-proper-disk-size-first-partition.png)

- Once done, type “n 2” and press
`Enter`twice to create the second partition.

- Press
`Enter`again to accept the default starting sector, then provide the size that you want for your second partition. In my case, I will write “+4G” to set the overall size to 4 gigabytes.

![A terminal showing the output for the creation of the second partition in fdisk.](https://www.maketecheasier.com/assets/uploads/2023/07/create-file-systems-partitions-linux-12-create-second-partition.png)

- Type “n 3” then press
`Enter`twice to create your last partition. - Press
`Enter`to accept the default starting sector. However, unlike the previous partitions, you can leave the second prompt blank and press`Enter`.

![A terminal showing the output for the third partition creation process in fdisk.](https://www.maketecheasier.com/assets/uploads/2023/07/create-file-systems-partitions-linux-13-create-third-partition.png)

- After that, type “t 3”, and press
`Enter`twice to modify the type of the last partition. - Inside, write “19”, and press
`Enter`to set it to “Linux swap.”

![A terminal showing process for changing the partition type in fdisk.](https://www.maketecheasier.com/assets/uploads/2023/07/create-file-systems-partitions-linux-14-modify-third-partition-type.png)

- Lastly, type “wq” and press
`Enter`to save your new partition table layout.

## Creating File Systems With mkfs

Sometimes you don’t need to make an entire partition layout. Sometimes you just need to [make a filesystem](https://www.maketecheasier.com/choosing-the-best-linux-filesystem/). This can easily be accomplished directly in the terminal with the `mkfs`

command.

- Enter
`lsblk`

in your terminal. It’ll print out a list. Find the partition or drive you want to make a file system on.

![cfdisk-mkfs-list-partitions-lsblk A terminal showing the output of lsblk with a disk with three distinct partitions.](https://www.maketecheasier.com/assets/uploads/2023/07/create-file-systems-partitions-linux-15-modified-lsblk-contents.png)

In this example, I’ll point it to the first partition of the secondary drive “/dev/sda1.” It’s also possible to just point mkfs to “/dev/sda” (to make use of the entire drive).

- Enter the following command to create the new file system on a specific partition.

sudo mkfs.ext4 /dev/sda1

![cfdisk-mkfs-make-file-system-ext4 A terminal showing the output of the mkfs.ext4 program.](https://www.maketecheasier.com/assets/uploads/2023/07/create-file-systems-partitions-linux-16-create-ext4-filesystem.png)

It should be noted that `mkfs.ext4`

can be changed to whatever file system you want to use. For example, you can create a “FAT” partition by running `mkfs.vfat`

instead:

sudo mkfs.vfat /dev/sda1

![A terminal showing the output of the mkfs.vfat program.](https://www.maketecheasier.com/assets/uploads/2023/07/create-file-systems-partitions-linux-17-create-fat-partition.png)

**Good to know: **Learn how your computer stores data in file systems by looking at some of the [best file recovery tools in Linux](https://www.maketecheasier.com/ways-recover-deleted-files-linux/) today.

## Frequently Asked Questions

### Do you need to unmount your drive before running cfdisk?

Ideally yes. However, desktop environments today tend to automatically remove a drive’s device file whenever you unmount it from your file manager. This can be an issue since cfdisk relies on an accessible device file in order to format a drive properly.

To fix this issue, you can either run cfdisk even if your drive is still mounted or unmount it using the `umount`

utility: `sudo umount /media/$USER/your-device`

.

### Is it possible to list all the available partition types in fdisk?

Yes. The Type submenu fdisk provides a handy list of all the available partition types that it can format to. To access this, you need to type `t` followed by the number of the partition that you want to modify. Next, press `Shift` + `L` to print a list of the available partition types along with their type number.

### Would clearing the partition table completely wipe my drive?

No. A partition table is a small binary file that sits at the beginning of every disk drive. It serves as a “phone book” for your operating system so that it can find the right sectors to load a file system from.

Because of that, removing the partition table will not delete any existing data inside your hard disk. To [properly remove the data inside your drives](https://www.maketecheasier.com/completely-delete-file-in-linux/), you can “zero” its contents by piping “/dev/zero” to your device file using dd: `sudo dd status=progress if=/dev/zero of=/dev/sda`

.

Image credit: [Sajad Nori via Unsplash](https://unsplash.com/photos/sIX4eDtak7k). All alterations and screenshots by Ramces Red.

Our latest tutorials delivered straight to your inbox