fdisk是Linux系统中最常用的分区工具,通过这个命令也可以查看系统中所有可用的分区,但是这个命令只支持MBR的分区表(这句话应该只对某些系统,CentOS7-1810适用,Debian9.5和openSUSE15应该已经支持GPT分区表,下面对fdisk的操作都是在Debian9.5下操作),所以这个命令不能对大于2T的硬盘进行分区,大于2T的硬盘我们需要用GPT分区表来进行分区,GPT分区我们就要用gdisk或parted分区工具来进行分区。

Linux 内核是如何理解硬盘的?
作为人类,我们可以很轻松地理解一些事情;但是电脑就不是这样了,它们需要合适的命名才能理解这些。

在 Linux 中,外围设备都位于 /dev 挂载点,内核通过以下的方式理解硬盘:

/dev/hdX[a-z]:     IDE 硬盘被命名为 hdX
/dev/sdX[a-z]:      SCSI 硬盘被命名为 sdX
/dev/xdX[a-z]:      XT 硬盘被命名为 xdX
/dev/vdX[a-z]:     虚拟硬盘被命名为 vdX
/dev/fdN:              软盘被命名为 fdN
/dev/scdN or /dev/srN:         CD-ROM 被命名为 /dev/scdN 或 /dev/srN

fdisk 允许我们在每块硬盘上创建最多四个主分区。它们中的其中一个可以作为扩展分区,并下设多个逻辑分区。1-4 扇区作为主分区被保留,逻辑分区从扇区 5 开始。

一、fdisk参数使用方法:

fdisk [-l] 设备名

参数
-l :显示指定磁盘设备的分区表信息,如果没有指定磁盘设备,则显示/proc/partitions 文件中的信息,也就是系统中所有的磁盘设备。

举例:
-----------------------------------------------------------
root@debian:~# fdisk  -l          #没有指定任何磁盘设备

Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors       #第一块磁盘/dev/sda容量20G
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos                     #分区表的类型为MBR (dos表示MBR分区表,gpt表示GPT分区表)
Disk identifier: 0xefdc905c

Device          Boot    Start          End             Sectors   Size  Id       Type         #磁盘/dev/sda下的分区
/dev/sda1     *          2048        37748735   377466881     8G        83     Linux
/dev/sda2                37750782      41940991   4190210   2G         5       Extended
/dev/sda5      37750784     41940991          4190208          2G        82      Linux swap / Solaris

Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 sectors      #第二块磁盘/dev/sdb容量5G
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

root@debian:~#
-----------------------------------------------------------
使用 "fdisk -l" 查看分区信息,不同的发行版可能显示的信息不同,通过上面的信息能够看到我们添加的两块硬盘(/dev/sda 和 /dev/sdb)的信息。我们解释一下这些信息,其上半部分是硬盘的整体状态,/dev/sda 硬盘的总大小是20GB等信息。

信息的下半部分是分区的信息,共 7 列,含义如下:

Device:分区的设备文件名。
Boot:是否为启动引导分区,在这里 /dev/sda1 为启动引导分区。
Start:起始柱面,代表分区从哪里开始。
End:终止柱面,代表分区到哪里结束。
Sectors:分区的大小,单位是扇区。
Size:分区的容量
id:分区内文件系统的 ID。
Type:分区的文件系统是什么。

二、fdisk交互模式磁盘分区

通过命令“fdisk 设备文件名” 就可以进入fdisk程序的交互模式,在交互模式中可以通过输入fdisk程序所提供的指令完成相应的操作,举例如下

-----------------------------------------------------------
root@debian:~# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.29.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xe3779e93.

Command (m for help): m                                                                   #输入m可以获得帮助信息

Help:

DOS (MBR)
a   toggle a bootable flag                                                                        #设置可引导标记
b   edit nested BSD disklabel                                                                #编辑 bsd 磁盘标签
c    toggle the dos compatibility flag                                                  #设置 DOS 操作系统兼容标记

G  eneric
d  delete a partition                                                                                 #删除一个分区
F  list free unpartitioned space                                                           #列出空闲的未分区空间
l    list known partition types                                                                 #显示已知的分区类型。82 为 Linux swap 分区,83 为 Linux 分区
n    add a new partition                                                                            # 新建分区
p    print the partition table                                                                    #显示当前磁盘的分区列表
t    change a partition type                                                                      #改变分区类型
v    verify the partition table                   #验证分区表
i    print information about a partition            #打印有关分区的信息

Misc
m    print this menu                     #显示帮助菜单
u    change display/entry units                        #改变显示记录单位
x    extra functionality (experts only)             #附加功能(仅专家)

Script
I    load disk layout from sfdisk script file           #从sfdisk脚本文件加载磁盘布局
O    dump disk layout to sfdisk script file           #将磁盘布局转储到sfdisk脚本文件

Save & Exit
w    write table to disk and exit                 #保存退出
q    quit without saving changes              #不保存退出

Create a new label
g    create a new empty GPT partition table         #新建空白GPT分区表,这个应该就表明fdisk已经支持GPT,还需要进一步的认证。
G    create a new empty SGI (IRIX) partition table         #新建空白IRIX分区表
o    create a new empty DOS partition table         #新建空白DOS分区表
s    create a new empty Sun partition table          #新建空白SUN磁盘标签

Command (m for help):
-----------------------------------------------------------

三、分区管理

1、新建主分区

root@debian:~# fdisk    /dev/sdb            #要新建分区的磁盘/dev/sdb不要加数字加数字就变成了分区

Welcome to fdisk (util-linux 2.29.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xd9b1702f.

Command (m for help):  n              #输入n新建分区,默认直接创建MBR分区表
Partition type
p primary (0 primary, 0 extended, 4 free)       #主分区
e extended (container for logical partitions)       #扩展分区
Select (default p): p                  #输入p新建主分区
Partition number (1-4, default 1):           #输入分区号,直接回车就行,默认为1
First sector (2048-10485759, default 2048):      #输入起始扇区,直接回车就行,默认2048最前面开始
Last sector, +sectors or +size{K,M,G,T,P} (2048-10485759, default 10485759):  +1G                 #这里输入新增分区的大小可以通过扇区数来增加,也可以通过+size{K,M,G,T,P}方式来增加,这里要增加1G的容量就表示为+1G

Created a new partition 1 of type 'Linux' and of size 1 GiB.      #提示创建了1G的分区

Command (m for help):  p                     #输入p查看创建的分区
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos                        #分区表类型为MBR
Disk identifier: 0xd9b1702f

Device    Boot   Start   End     Sectors   Size   Id   Type
/dev/sdb1      2048   2099199   2097152   1G   83   Linux

2、剩余可用的空间

Command (m for help):  F                     #输入F查看剩余可用的空间
Unpartitioned space /dev/sdb: 4 GiB, 4293918720 bytes, 8386560 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes

Start     End     Sectors   Size
2099200   10485759   8386560    4G                   #可以看到可用空间为4G

3、创建扩展分区及逻辑分区

Command (m for help):  n                     #输入n继续创建分区
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p):  e                          #输入e创建扩展分区
Partition number (2-4, default 2):                  #输入扩展分区号,直接回车就可以
First sector (2099200-10485759, default 2099200):          #输入起始扇区数,直接回车就可以
Last sector, +sectors or +size{K,M,G,T,P} (2099200-10485759, default 10485759): +2G                                    #增加2G的容量

Created a new partition 2 of type 'Extended' and of size 2 GiB.       #创建扩展分区成功

Command (m for help):  n                       #输入n继续创建分区
Partition type
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)                      #只有创建了扩展分区才会有这个选项
Select (default p):  l                         #输入l创建逻辑分区

Adding logical partition 5                          #扩展分区号从5开始
First sector (2101248-6293503, default 2101248):
Last sector, +sectors or +size{K,M,G,T,P} (2101248-6293503, default 6293503): +1G

Created a new partition 5 of type 'Linux' and of size 1 GiB.

Command (m for help):  p
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xd9b1702f

Device    Boot  Start   End     Sectors   Size   Id   Type
/dev/sdb1     2048   2099199   2097152   1G   83   Linux           #主分区
/dev/sdb2      2099200     6293503   4194304    2G    5   Extended               #扩展分区
/dev/sdb5       2101248    4198399   2097152    1G   83   Linux               #逻辑分区

4、删除分区

Command (m for help): d                    #输入d删除分区
Partition number (1,2,5, default 5): 2               #删除扩增分区2,上面的逻辑分区也会一同删除

Partition 2 has been deleted.

Command (m for help): p
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xd9b1702f

Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 2099199 2097152 1G 83 Linux

Command (m for help):

5、修改分区类型

Command (m for help):  l                      #查看可用分区类型


#其中82为Linux swap分区、83为Linux分区、8e为LinuxLVM分区、b为Windows FAT32分区、e为Windows FAT16分区。

Command (m for help): t                  #输入t更改分区类型
Partition number (1,2,5, default 5): 5        #更改分区5的类型

Partition type (type L to list all types): 8e            #输入更改分区类型为8e,这里输入l也可以查看分区的类型

Changed type of partition 'Linux' to 'Linux LVM'.          #提示更改成功

Command (m for help): p
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xd9b1702f

Device   Boot    Start    End     Sectors   Size   Id   Type
/dev/sdb1       2048   2099199   2097152   1G   83   Linux
/dev/sdb2       2099200   6293503   4194304    2G   5   Extended
/dev/sdb5       2101248   4198399   2097152    1G   8e   Linux LVM         #更改后的分区类型

Command (m for help):

6、保存修改的结果

Command (m for help):  w                        #输入w保存配置,如果不想保存可以输入q退出
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

使用w指令保存后,则在fdisk中所做的所有操作都会生效,且不可回退。如果分区表正忙,则需要重启机器后才能使新的分区表生效。

四、新建GPT分区

不是所有的发行版带的fdisk都支持GPT分区这里请注意

1、新建分区

Command (m for help): p

Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos                  #现在的分区类型为MBR
Disk identifier: 0xd9b1702f

Command (m for help): g            #输入g创建GPT分区
Created a new GPT disklabel (GUID: 8C0F4613-2830-40E1-8D23-596D1B7F5DDB).

Command (m for help): n            #新建分区
Partition number (1-128, default 1):         #可以看到这里没有扩展分区的概念了
First sector (2048-10485726, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-10485726, default 10485726): +1G

Created a new partition 1 of type 'Linux filesystem' and of size 1 GiB.

Command (m for help): p
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 8C0F4613-2830-40E1-8D23-596D1B7F5DDB

Device    Start   End   Sectors   Size   Type                                               #GPT下输入p显示的分区信息和MBR下略有不同,没有分区类型id的选项
/dev/sdb1   2048       2099199     2097152         1G        Linux filesystem       #成功创建了一个主分区

Command (m for help):

2、 修改分区的类型

Command (m for help): l                                #查看分区的类型,GPT下同样的分区类型对应的编号不同了,这里要注意

上面只是显示了部分按空格显示更多的信息

Command (m for help):  t                                          #输入t更改分区类型,这里输入l也可以查看分区的类型
Partition number (1,2, default 2):  2                      #输入要更改的分区
Hex code (type L to list all codes):  31                  #输入分区类型的编号

Changed type of partition 'Linux filesystem' to 'Linux LVM'.               #更改分区类型成功

Command (m for help): p                                      
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 836D334F-D0EE-4AD3-8581-EC9ABFF31F50

Device    Start    End       Sectors   Size   Type
/dev/sdb1      2048         2099199    2097152        1G          Linux filesystem
/dev/sdb2    2099200    4196351     2097152      1G          Linux LVM                                #分区类型为已经更改

Command (m for help):

fdisk分区命令的更多相关文章

  1. Linux文件系统管理 fdisk分区命令

    概述 我们在安装操作系统的过程中已经对系统硬盘进行了分区,但是如果我新添加了一块硬盘,想要正常使用时,在Linux中有专门的分区命令 fdisk 和 parted.其中 fdisk 命令较为常用,但不 ...

  2. Linux的VMWare中Centos7磁盘分区管理 fdisk分区和制作文件系统格式化和开机自动挂载

    一.硬盘的组成零件扇区 磁道 磁盘容量 磁盘分区 简介 硬盘由容量.柱面数.磁头数.扇区数 C/H/S, Cylinder, Head, Sector(柱面/磁头数/扇区数) 1.磁头数表示硬盘总共有 ...

  3. fdisk 磁盘分区命令

    fdisk fdisk磁盘分区命令 -v    打印 fdisk 的版本信息并退出.-l    列出指定设备的分区表信息并退出. 如果没有给出设备,那么使用那些在 /proc/partitions ( ...

  4. linux 使用fdisk分区扩容

    标签:fdisk分区 概述 我们管理的服务器可能会随着业务量的不断增长造成磁盘空间不足的情况,在这个时候我们就需要增加磁盘空间,本章主要介绍如何使用fdisk分区工具创建磁盘分区和挂载分区,介绍两种情 ...

  5. 第8章 文件系统管理(2)_挂载、fdisk分区及分配swap分区

    3. fdisk分区 3.1 fdisk命令分区过程 (1)添加新硬盘 (2)查看新硬盘#fdisk –l (3)使用fdisk命令分区:#fdisk /dev/sdb Fdisk交互指令说明 命令 ...

  6. fdisk分区硬盘并shell脚本自动化

    最近工作需要用到对硬盘进行shell脚本自动化分区和mount的操作,google了一些资料,下面做个总结. 如果硬盘没有进行分区(逻辑分区或者扩展分区,关于两者概念,自行google),我们将无法将 ...

  7. linux笔记:文件系统管理-fdisk分区

    fdisk命令分区过程: 1.添加新硬盘 2.查看新硬盘: fdisk -l 3.使用fdisk命令分区: fdisk 硬盘设备文件名(如:fdisk /dev/sdb) fdisk交互指令说明: 4 ...

  8. linux入门基础_centos(二)--fdisk分区

    课时10 使用fdisk进行磁盘管理             fdisk是来自IBM的老牌分区工具,支持绝大多数的操作系统,几乎所有的linux的发行版本都装有fdisk,包括在linux的rescu ...

  9. fdisk分区和挂载

    查看硬盘情况 fdisk -l 修改分区类型为LVM sudo fdisk /dev/sda (/dev/sda为对应的设备名,也可能是其它名字) 按n新建分区 按p选择主分区 按t改变分区的syst ...

随机推荐

  1. WUSTOJ 1347: GCD(Java)互质

    题目链接:1347: GCD Description 已知gcd(a,b)表示a,b的最大公约数. 现在给你一个整数n,你的任务是在区间[1,n)里面找到一个最大的x,使得gcd(x,n)等于1. I ...

  2. Maven学习存档(2)——settings.xml配置

    二.settings.xml配置 2.1 原文 <?xml version="1.0" encoding="UTF-8"?> <!-- Lic ...

  3. C#汉字转换成全拼的拼音

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. 基于【 springBoot+jsoup】一 || 爬取全国行政区划数据

    一.代码演示 如果中途中断,可进行刷选过滤已拉取省份数据 /** * TODO * * @author kevin * @createTime 2019-11-18 19:37 */ @RestCon ...

  5. CSS重排和重绘

    一.什么是重绘Repaint和重排 (回流 reflow) 重绘:当元素的一部分属性发生改变,如外观.背景.颜色等不会引起布局变化,只需要浏览器根据元素的新属性重新绘制 ,使元素呈现新的外观叫做重绘. ...

  6. 安卓开发之生成cache目录和files目录

    package com.lidaochen.test; import android.os.Bundle; import android.support.v7.app.AppCompatActivit ...

  7. Dubbo面试

    DUBBO原理.应用与面经总结 SPI 你是否了解SPI,讲一讲什么是SPI,为什么要使用SPI? SPI具体约定:当服务的提供者,提供了服务接口的一种实现之后,在jar包的META-INF/serv ...

  8. Dubbo服务发布、引用

    DUBBO原理.应用与面经总结 Dubbo原理和源码解析之服务暴露 Dubbo原理和源码解析之服务引用 服务发布 服务的发布总共做了以下几件事,这个也可以从日志log上看出来: 暴露本地服务 暴露远程 ...

  9. 七、玩转select条件查询

    前言: 电商中:我们想查看某个用户所有的订单,或者想查看某个用户在某个时间段内所有的订单,此时我们需要对订单表数据进行筛选,按照用户.时间进行过滤,得到我们期望的结果. 此时我们需要使用条件查询来对指 ...

  10. SQL SERVER-孤立用户

    --SQL SERVER用户管理分为两层,实例级的login和数据库级的用户 --login的SID和数据库用户的SID必须一样才行 --数据库搬迁login在重建时生成新的sid,与原来数据库的用户 ...