在使用服务器的过程当中你会遇到磁盘空间使用完毕或者VPS空间分成系统盘跟数据盘的情况,这时候就需要挂载新的硬盘到服务器系统上才能继续保存文件,那么在下面教你如何在linux系统下挂载硬盘。

挂载硬盘

在此我们以2480G硬盘为例,进行硬盘的挂载

运行磁盘操作命令 查询当前服务器上面的所有硬盘

fdisk -l

[root@s12-196 ~]# fdisk -l

Disk /dev/sda: 480.1 GB, 480103981056 bytes, 937703088 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
Disk label type: dos
Disk identifier: 0x000e30a9

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1050623      524288   83  Linux
/dev/sda2         1050624    17827839     8388608   82  Linux swap / Solaris
/dev/sda3        17827840   937701375   459936768   83  Linux

Disk /dev/sdb: 480.1 GB, 480103981056 bytes, 937703088 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

[root@s12-196 ~]#

通过终端打印出来的信息我们可以看到当前服务器有2个硬盘 分别为

#Disk /dev/sda: 480.1 GB, 480103981056 bytes, 937703088 sectors
#Disk /dev/sdb: 480.1 GB, 480103981056 bytes, 937703088 sectors

首先要确定当前系统安装在那块硬盘上面,可以通过命令df -h来进行确认

[root@s12-196 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3       432G  2.1G  408G   1% /
devtmpfs         48G     0   48G   0% /dev
tmpfs            48G     0   48G   0% /dev/shm
tmpfs            48G   26M   48G   1% /run
tmpfs            48G     0   48G   0% /sys/fs/cgroup
/dev/sda1       488M  105M  348M  24% /boot
tmpfs           9.5G     0  9.5G   0% /run/user/0
[root@s12-196 ~]# 

从上面终端打印出来的信息当中 我们可以发现有以下2条分区配置 //boot

/dev/sda3       432G  2.1G  408G   1% /
/dev/sda1       488M  105M  348M  24% /boot

通过这2条信息 我们即可判断当前系统安装在硬盘设备 /dev/sda 上面

判断系统所在硬盘完成 那么理所当然另外一个硬盘 /dev/sdb 即为我们需要挂载的硬盘 接下来进行硬盘挂载 操作

针对全选未分区的硬盘

执行磁盘分区命令

fdisk /dev/sdb

根据提示,依次输入"n","p" "1",两次回车,"wq",分区就开始了,很快就会完成。

[root@s12-196 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.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
Building a new DOS disklabel with disk identifier 0xea2d35dd.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-937703087, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-937703087, default 937703087): 
Using default value 937703087
Partition 1 of type Linux and of size 447.1 GiB is set

Command (m for help): wq
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@s12-196 ~]# 

运行完毕 设备/dev/sdb分区即完成 通过命令 fdisk -l 验证以下

[root@s12-196 ~]# fdisk -l

Disk /dev/sda: 480.1 GB, 480103981056 bytes, 937703088 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
Disk label type: dos
Disk identifier: 0x000e30a9

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1050623      524288   83  Linux
/dev/sda2         1050624    17827839     8388608   82  Linux swap / Solaris
/dev/sda3        17827840   937701375   459936768   83  Linux

Disk /dev/sdb: 480.1 GB, 480103981056 bytes, 937703088 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
Disk label type: dos
Disk identifier: 0xea2d35dd

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   937703087   468850520   83  Linux
[root@s12-196 ~]# 

这里我们可以看到 成功分出了一个 /dev/sdb1 的磁盘分区 接下来格式化这个新建立的磁盘分区

mkfs.ext4 /dev/sdb1

`

[root@s12-196 ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
Discarding device blocks: done                            
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
29310976 inodes, 117212630 blocks
5860631 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2264924160
3578 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
    4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
    102400000

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done     

[root@s12-196 ~]#

很快磁盘分区就格式化完成了 接着建立分区挂载目录

新建挂载目录 /data

mkdir /data

需要注意的是挂载目录不是固定的 可以是data 也可以是其他的目录 这里以挂载到data目录为例进行

挂载硬盘

使用mount命令进行磁盘的挂载

mount /dev/分区名 /挂载目录

因为我们刚刚建立的分区为/dev/sdb1 所以这里的挂载命令为

mount /dev/sdb1 /data

运行完成以后 磁盘即可成功挂载到/data目录上 我们来验证一下 使用 df -h 命令

[root@s12-196 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3       432G  2.1G  408G   1% /
devtmpfs         48G     0   48G   0% /dev
tmpfs            48G     0   48G   0% /dev/shm
tmpfs            48G   26M   48G   1% /run
tmpfs            48G     0   48G   0% /sys/fs/cgroup
/dev/sda1       488M  105M  348M  24% /boot
tmpfs           9.5G     0  9.5G   0% /run/user/0
/dev/sdb1       440G   73M  418G   1% /data
[root@s12-196 ~]#

通过终端打印出来的信息 我们可以看到 磁盘分区sdb1已经成功挂载到data目录

/dev/sdb1       440G   73M  418G   1% /data

最后一步设置开机自动挂载 这步千万不要省略 或者机器重启以后前面挂载的磁盘就失效了

编辑开启启动文件

vi /etc/fstab

插入磁盘设备分区名 挂载目录 文件格式

/dev/sdb1    /data    ext4    defaults    0 0

到此硬盘挂载完成

针对以有磁盘分区的硬盘

列出所有硬盘 fdisk -l

[root@s12-196 ~]# fdisk -l

Disk /dev/sda: 480.1 GB, 480103981056 bytes, 937703088 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
Disk label type: dos
Disk identifier: 0x000e30a9

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1050623      524288   83  Linux
/dev/sda2         1050624    17827839     8388608   82  Linux swap / Solaris
/dev/sda3        17827840   937701375   459936768   83  Linux

Disk /dev/sdb: 480.1 GB, 480103981056 bytes, 937703088 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
Disk label type: dos
Disk identifier: 0xea2d35dd

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   937703087   468850520   83  Linux
[root@s12-196 ~]#

在这里我们可以看到 当前有2个硬盘 2个硬盘分别有已经划分好的分区设备

/dev/sda1 /dev/sda2 /dev/sda3 跟 /dev/sdb1

判断系统所在硬盘同上面的教程 这里主要说一下已经存在有分区的硬盘如何进行挂载 首先需要先删除已分区 同样的执行磁盘操作命令

fdisk /dev/sdb

依次输入 "d" "p" "wq"回车保存 有几个分区就运行几次 直到所有分区全部删除

[root@s12-196 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): d
Selected partition 1
Partition 1 is deleted

Command (m for help): p

Disk /dev/sdb: 480.1 GB, 480103981056 bytes, 937703088 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
Disk label type: dos
Disk identifier: 0xea2d35dd

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): wq
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@s12-196 ~]#

验证一下分区是否已经删除

[root@s12-196 ~]# fdisk -l

Disk /dev/sda: 480.1 GB, 480103981056 bytes, 937703088 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
Disk label type: dos
Disk identifier: 0x000e30a9

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1050623      524288   83  Linux
/dev/sda2         1050624    17827839     8388608   82  Linux swap / Solaris
/dev/sda3        17827840   937701375   459936768   83  Linux

Disk /dev/sdb: 480.1 GB, 480103981056 bytes, 937703088 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
Disk label type: dos
Disk identifier: 0xea2d35dd

   Device Boot      Start         End      Blocks   Id  System
[root@s12-196 ~]#

可以看到磁盘设备 /dev/sdb 已经没有 /dev/sdb1 分区
删除所有分区以后 接下来的操作方法就跟前面提到的教程一样 划分新分区 格式化硬盘 挂载硬盘到目录
到此本次课程结束

Last modification:May 10, 2022
如果觉得我的文章对你有用,请随意赞赏