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

重启服务后,可以在其它电脑上查看共享:

即可在该共享文件夹中进行读写操作。


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部