前言
在 Linux 中要使用 Samba 文件協議來實現 NAS 配置,首先要掛載硬盤。本文來簡述如何在 Linux 中來掛載硬盤。
格式化
將硬盤插入到主機對應接口。
查看硬盤信息
fdisk -l
可以看到插入的硬盤 /dev/sdb1 的文件系統類型為:FAT32 (LBA) 。

格式化硬盤
使用 fdisk 命令對 /dev/sdb1 進行分區
fdisk /dev/sdb1
具體過程如下:
root@debyogile:/home/yogile# fdisk /dev/sdb1
Welcome to fdisk (util-linux 2.33.1).
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 has been deleted.
# 添加硬盤新分區
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
# 添加分區,默認的分區格式是 primary
Select (default p):
Using default response p.
# 分區號默認 1
Partition number (1-4, default 1):
# 指定分區的起始扇區,一般默認 2048
First sector (2048-62668799, default 2048):
# 指定分區的終止扇區,一般默認最大值
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-62668799, default 62668799):
Created a new partition 1 of type 'Linux' and of size 29.9 GiB.
Partition #1 contains a vfat signature.
# 詢問刪除簽名,確認:y
Do you want to remove the signature? [Y]es/[N]o: y
The signature will be removed by a write command.
# 保存修改
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
復查硬盤信息
fdisk -l
可以看到插入的硬盤 /dev/sdb1 的硬盤類型為:Linux 。

格式化文件系統
將硬盤文件系統格式化為 ext4 。
mkfs -t ext4 /dev/sdb1

掛載硬盤
臨時掛載
要臨時掛載硬盤到 /mnt:
mount /dev/sdb1 /mnt
查看掛載點:
df -h

永久掛載
Debian 使用 UUID 來實現硬盤自動掛載。
-
通過 blkid 查找所有硬盤的 UUID:
sudo blkid可以看到掛載的硬盤 /dev/sdb1 的 UUID 為:
ad5f412a-0a0c-42af-afd3-eecec6fd96d2,TYPE 為:ext4。
-
添加自動掛載點
sudo vim /etc/fstab在最后一行添加:
UUID=ad5f412a-0a0c-42af-afd3-eecec6fd96d2 /mnt ext4 defaults 0 0 -
執行掛載
sudo mount -a -
查看掛載點:
df -h這時,
sudo reboot重啟后掛載點依然存在。
測試掛載
-
查看硬盤掛載文件夾
cd /mnt ls可以看到默認創建的文件夾 lost+found :

-
hdparm測試硬盤讀寫速度下載 hdparm
sudo apt install hdparm測試硬盤讀寫速度
hdparm -Tt /dev/sdb1可以看到讀取 10948.93 MB/sec ,寫入 131.46 MB/sec ,這是由於本文是在虛擬機上實現的,讀寫有誤,請根據實際查看。

