![如何在 Linux 上自动创建每个 USB 驱动器的网络共享?](https://linux22.com/image/1582246/%E5%A6%82%E4%BD%95%E5%9C%A8%20Linux%20%E4%B8%8A%E8%87%AA%E5%8A%A8%E5%88%9B%E5%BB%BA%E6%AF%8F%E4%B8%AA%20USB%20%E9%A9%B1%E5%8A%A8%E5%99%A8%E7%9A%84%E7%BD%91%E7%BB%9C%E5%85%B1%E4%BA%AB%EF%BC%9F.png)
我想创建一个“网络 USB 集线器”,一个中央设备,所有同事都可以将所有 USB 驱动器插入其中,然后每个人都可以访问它们。
我想知道如何设置(无头)Ubuntu以自动挂载并在SMB上共享所有USB驱动器。
答案1
下文将更详细地介绍以下步骤 通过 Samba 自动导出 USB 驱动器:
使用的工具是usbmount
,在插入U盘时调用,挂载U盘,在/etc/usbmount/mount.d
使用中run-parts
和/etc/usbmount/umount.d
断开连接时执行脚本。
smb配置文件
创建空目录/etc/samba/auto
并在末尾添加以下行
smb.conf
:
include = /etc/samba/auto/usb0.conf
include = /etc/samba/auto/usb1.conf
include = /etc/samba/auto/usb2.conf
include = /etc/samba/auto/usb3.conf
include = /etc/samba/auto/usb4.conf
include = /etc/samba/auto/usb5.conf
include = /etc/samba/auto/usb6.conf
include = /etc/samba/auto/usb7.conf
安装.d
要在插入驱动器时自动创建上述配置文件,请将此文件创建为/etc/usbmount/mount.d/50_add_samba_export
,并将其 chmod 为可执行文件:
#!/bin/bash
SHARENAME=`basename $UM_MOUNTPOINT`
cat > /etc/samba/auto/$SHARENAME.conf <<EOF
[$SHARENAME]
comment = $UM_VENDOR $UM_MODEL
path = $UM_MOUNTPOINT
read only = no
EOF
/etc/init.d/samba restart
卸载
要在卸载或移除磁盘时删除共享,请添加
/etc/usbmount/umount.d/50_remove_samba_export
chmod 为+x
:
#!/bin/bash
SHARENAME=`basename $UM_MOUNTPOINT`
rm -f /etc/samba/auto/$SHARENAME.conf
/etc/init.d/samba restart
整理
要处理之前未完全卸载而重新启动的情况,请创建一个启动脚本来清除/etc/samba/auto
。使用 init 脚本,例如/etc/init.d/auto-share-clean
:
#! /bin/sh
### BEGIN INIT INFO
# Provides: auto-share-clean
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Cleans auto samba shares
### END INIT INFO
rm -f /etc/samba/auto/*
然后:
chmod +x /etc/init.d/auto-share-clean
update-rc.d auto-share-clean defaults