linux系统外接硬盘_linux通过fstab自动挂载外接磁盘
目錄
格式化硬盤
將磁盤接到機器上,進入dev目錄,查看連接的磁盤是否被系統識別,比如以下的的sda是接入小機的TF卡,是我們要掛載在系統上的存儲設備。
dolphin@localhost:~/work$ cd /dev
dolphin@localhost:/dev$ ll
total 4
crw------- 1 root root 5, 1 Oct 26 02:25 console
crw------- 1 root root 10, 62 Jan 28 2018 cpu_dma_latency
brw-rw---- 1 root disk 179, 0 Jan 28 2018 mmcblk1
brw-rw---- 1 root disk 8, 0 Jan 28 2018 sda
crw-rw-rw- 1 root root 1, 5 Jan 28 2018 zero
通過mkfs命令將磁盤格式化,如下指令將其格式化成了ext4格式,格式化后會生成新的UUID,這個UUID在后續的掛載操作中也會用到。
dolphin@localhost:~$ sudo mkfs -t ext4 /dev/sda
mke2fs 1.44.1 (24-Mar-2018)
Found a gpt partition table in /dev/sda
Proceed anyway? (y,N) y
Creating filesystem with 980224 4k blocks and 245280 inodes
Filesystem UUID: b6f4aa37-6bf5-4c78-a1e7-c8ce64507196
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
如果是要在磁盤下創建新的分區,比如樹莓派在燒錄鏡像后有多余的空間,可以用以下方法:
sudo fdisk /dev/mmcblk0
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.
Command (m for help): p
Disk /dev/mmcblk0: 14.9 GiB, 15931539456 bytes, 31116288 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: 0x7ee80803
Device Boot Start End Sectors Size Id Type
/dev/mmcblk0p1 8192 98045 89854 43.9M c W95 FAT32 (LBA)
/dev/mmcblk0p2 98304 6242303 6144000 3G 83 Linux
Command (m for help): n
Partition type
p primary (2 primary, 0 extended, 2 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (3,4, default 3): 4
First sector (2048-31116287, default 2048): 6300000
Last sector, +sectors or +size{K,M,G,T,P} (6300000-31116287, default 31116287): 31116287
Created a new partition 4 of type 'Linux' and of size 11.9 GiB.
Command (m for help): w
The partition table has been altered.
Command (m for help): p
Disk /dev/mmcblk0: 14.9 GiB, 15931539456 bytes, 31116288 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: 0x7ee80803
Device Boot Start End Sectors Size Id Type
/dev/mmcblk0p1 8192 98045 89854 43.9M c W95 FAT32 (LBA)
/dev/mmcblk0p2 98304 6242303 6144000 3G 83 Linux
/dev/mmcblk0p4 6300000 31116287 24816288 11.9G 83 Linux
創建新分區后可以進行格式化,比如以下選項將磁盤格式化為FAT32,適合TF卡等需要在多個操作系統打開的情況:
sudo mkfs -t vfat /dev/mmcblk0p4
mkfs.fat 4.1 (2017-01-24)
sudo reboot
創建掛載目錄
假如我們要將新的磁盤掛載到dolphin用戶下,我們需要在dolphin下創建新的文件夾,比如“work”,通過“ll”查看dolphin下的文件信息,新建的work屬于root,而同目錄下其它文件屬于dolphin。
dolphin@localhost:~$ sudo mkdir /home/dolphin/work
dolphin@localhost:~$ cd /home/dolphin
dolphin@localhost:~$ ll
total 1984
-rw-r--r-- 1 dolphin dolphin 3771 Apr 4 2018 .bashrc
drwxr-xr-x 2 root root 4096 Oct 26 01:40 work/
設置自動掛載
除了上文格式化可以獲得新的UUID,也可通過blkid來獲取已有分區的UUID。
dolphin@localhost:~$ sudo blkid
[sudo] password for dolphin:
/dev/mmcblk1p4: LABEL="ROOTFS" UUID="a88085ec-5629-4f58-a711-26305b1954e2" TYPE="ext4" PARTLABEL="rootfs" PARTUUID="614e0000-0000-4b53-8000-1d28000054a9"
/dev/mmcblk1: PTUUID="1e720000-0000-4c3a-8000-0ef9000017bc" PTTYPE="gpt"
/dev/mmcblk1p1: PARTLABEL="uboot" PARTUUID="3b250000-0000-4a58-8000-13c300000c60"
/dev/mmcblk1p2: PARTLABEL="trust" PARTUUID="55530000-0000-4a11-8000-2045000016f1"
/dev/mmcblk1p3: PARTLABEL="boot" PARTUUID="d4160000-0000-444b-8000-2ee500000856"
/dev/sda: UUID="b6f4aa37-6bf5-4c78-a1e7-c8ce64507196" TYPE="ext4"
用vim編輯器打開fstab文件
sudo vim /etc/fstab
往文件中加入以下內容:
UUID=b6f4aa37-6bf5-4c78-a1e7-c8ce64507196 /home/dolphin/work ext4 user,rw 0 2
重啟后通過mount查看所有掛載的設備,檢查“sda”是否成功掛載:
dolphin@localhost:~/work$ mount
/dev/sda on /home/dolphin/work type ext4 (rw,nosuid,nodev,noexec,relatime,data=ordered,user)
tmpfs on /run/user/1000 type tmpfs (rw,nosuid,nodev,relatime,size=24320k,mode=700,uid=1000,gid=1000)
這時掛載的work屬于root,當前用戶沒有讀寫權限,通過chown更改文件夾的所有者,執行后可以看到work的用戶和組變成了dolphin:dolphin:
(chown [-cfhvR] [--help] [--version] user[:group] file...)
dolphin@localhost:~$ sudo chown -R dolphin:dolphin /home/dolphin/work dolphin@localhost:~$ ll
total 1984
-rw-r--r-- 1 dolphin dolphin 3771 Apr 4 2018 .bashrc
drwxr-xr-x 2 dolphin dolphin 4096 Oct 26 01:40 work/
通過在work目錄中創建文件夾來檢查讀寫權限:
dolphin@localhost:/home$ cd dolphin/work
dolphin@localhost:~/work$ mkdir my111
dolphin@localhost:~/work$ ll
total 32
drwxr-xr-x 2 dolphin dolphin 4096 Oct 26 02:21 my/
drwxrwxr-x 2 dolphin dolphin 4096 Oct 26 02:22 my111/
dolphin@localhost:~/work$
掛載window共享
通用Internet文件系統(CIFS)也稱為服務器信息塊(SMB),是適用于MicrosoftWindows服務器和客戶端的標準文件和打印機共享系統。Samba服務可用于將Linux文件系統作為CIFS/SMB網絡文件共享進行共享,并將Linux打印機作為CIFS/SMB打印機共享進行共享。
通過安裝'sambaclient'可以查看共享:
firefly@firefly:~$ smbclient -L //192.168.199.1
WARNING: The "syslog" option is deprecated
Enter WORKGROUP\firefly's password:
Sharename Type Comment
--------- ---- -------
IPC$ IPC IPC Service (HiWiFi File Transition)
tf Disk
VR Disk
HiWiFi-Share Disk
mobile Disk
Reconnecting with SMB1 for workgroup listing.
Server Comment
--------- -------
Workgroup Master
--------- -------
WORKGROUP HIWIFI
通過以下指令可以掛載共享文件夾:
sudo apt-get install cifs-utils
mkdir -p smb && cd smb
sudo mount -o username=guest,password="" //192.168.199.1/VR smb
#以下指令用于掛載后權限不對的問題
sudo mount -t cifs -o username=20235403,password="20235403",iocharset=utf8,rw,dir_mode=0777,file_mode=0777,vers=1.0 //okiirobot.jios.org/1 ~/work/mnt
作為共享盤
安裝samba服務軟件:
sudo apt install samba cifs-utils
需要修改samba的配置文件:
sudo smbpasswd -a "用戶名"
# 輸入密碼
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf_backup
sudo vim /etc/samba/smb.conf
systemctl restart smbd
在conf中加入以下內容,表示共享了一個work的文件夾:
[work]
path = /home/firefly/work
public = yes
writable = yes
為了避免亂碼,在 /etc/samba/smb.conf 中的 [global] 段加上:
#local:zh_CN.UTF-8
display charset = UTF-8
unix charset = UTF-8
dos charset = cp936
#local:zh_CN.GB2312/zh_CN.GBK
display charset = cp936
unix charset = cp936
dos charset = cp936
重啟服務后,可以在其它電腦上查看共享:
即可在該共享文件夾中進行讀寫操作。
總結
以上是生活随笔為你收集整理的linux系统外接硬盘_linux通过fstab自动挂载外接磁盘的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pdf拆分成一页一页pdf拆分成一页一页
- 下一篇: 牙周病的一级预防包括哪些?