Linux auto-shared USB devices: udev + Samba, udevsamba

Source: Internet
Author: User

Linux auto-shared USB devices: udev + Samba, udevsamba

I. Overview

Recently, the company asked me to insert a USB device into Ubuntu and automatically share it on the network. I can access the content like Windows sharing (for example, \ 192.168.1.10) without the write permission. After hearing this demand, I was shocked. I immediately searched the relevant documents online and ran to ask the supervisor. Is this function implemented? The supervisor must say yes. The technical documents found on the Internet are only inserted into the USB device for automatic mounting, and the automatic sharing is not found, so you have to rely on yourself.

After some hard work, I finally implemented this function in Ubuntu14.04 desktop edition, but it is still not perfect. For example, by default, only five USB devices are shared, and USB1 ~ 5. The device still exists after the device is unplugged. The file system supports common FAT32, NTFS, and exFAT formats. I really want to talk about it, but I still don't know what this wonderful demand is...

Implementation process:

1. Insert a USB device and mount it automatically (udev rules)

2. After mounting, use the SMB protocol to share the file with the network. The client has the read permission (Samba server)

 

Ii. Principles

Use udev to efficiently and dynamically manage Linux Device Files

Use udev to automatically mount usb devices

Installation and configuration of Samba server in CentOS 6.3

 

Iii. Configuration

(Note: After Samba of Ubuntu 14.04 is installed, the service starts up by default, and SELinux and firewall do not affect the service. Other releases may need to be configured .)

1. Install the exFat package and support exFAT file systems

root@Ubuntu1:~# sudo apt-get install exfat-utils

 

2. Define udev rules to enable automatic mounting of USB devices

Root @ Ubuntu1 :~ # Cd/etc/udev/rules. d/# udev rule directory root @ Ubuntu1:/etc/udev/rules. d # vim 70-persistent-net.rules # edit rule file KERNEL! = "Sdb? ", GOTO =" automount_exit "ACTION =" add ", SUBSYSTEM =" block ", RUN + ="/bin/mkdir/media/usb1 ", RUN + = "/bin/mount-o uid = 1000, user, codepage = 936, utf8 $ root/% k/media/usb1" ACTION = "remove ", SUBSYSTEM = "block", RUN + = "/bin/umount/media/usb1 ", RUN + = "/bin/rmdir/media/usb1" LABEL = "automount_exit" # When the sdb device is detected, create a directory and mount it. After the device is deleted, uninstall and delete the directory KERNEL! = "Sdc? ", GOTO =" automount_exit "ACTION =" add ", SUBSYSTEM =" block ", RUN + ="/bin/mkdir/media/usb2 ", RUN + = "/bin/mount-o uid = 1000, user, codepage = 936, utf8 $ root/% k/media/usb2" ACTION = "remove ", SUBSYSTEM = "block", RUN + = "/bin/umount/media/usb2 ", RUN + = "/bin/rmdir/media/usb2" LABEL = "automount_exit" KERNEL! = "Sdd? ", GOTO =" automount_exit "ACTION =" add ", SUBSYSTEM =" block ", RUN + ="/bin/mkdir/media/usb3 ", RUN + = "/bin/mount-o uid = 1000, user, codepage = 936, utf8 $ root/% k/media/usb3" ACTION = "remove ", SUBSYSTEM = "block", RUN + = "/bin/umount/media/usb3 ", RUN + = "/bin/rmdir/media/usb3" LABEL = "automount_exit" KERNEL! = "Sde? ", GOTO =" automount_exit "ACTION =" add ", SUBSYSTEM =" block ", RUN + ="/bin/mkdir/media/usb4 ", RUN + = "/bin/mount-o uid = 1000, user, codepage = 936, utf8 $ root/% k/media/usb4" ACTION = "remove ", SUBSYSTEM = "block", RUN + = "/bin/umount/media/usb4 ", RUN + = "/bin/rmdir/media/usb4" LABEL = "automount_exit" KERNEL! = "Sdf? ", GOTO =" automount_exit "ACTION =" add ", SUBSYSTEM =" block ", RUN + ="/bin/mkdir/media/usb5 ", RUN + = "/bin/mount-o uid = 1000, user, codepage = 936, utf8 $ root/% k/media/usb5" ACTION = "remove ", SUBSYSTEM = "block", RUN + = "/bin/umount/media/usb5 ", RUN + = "/bin/rmdir/media/usb5" LABEL = "automount_exit" root @ Ubuntu1:/etc/udev/rules. d # shutdown-r now # restart the system

 

3. Install Samba to share Directories

root@Ubuntu1:~# apt-get install samba samba-common

 

4. Edit the Samba configuration file

Root @ Ubuntu1 :~ # Vim/etc/samba/smb. conf [USB1] comment = USB1 path =/media/usb1 # The first USB Mount path, corresponding to the previously defined udev rule browseable = yes # browsing permission guest OK = yes # anonymous sharing writeable = yes # whether to write [USB2] comment = USB2 path =/media/usb2 browseable = yes guest OK = yes writeable = yes [USB3] comment = USB3 path =/media/usb3 browseable = yes guest OK = yes writeable = yes [USB4] comment = USB4 path =/media/ usb4 browseable = yes guest OK = yes writeable = yes [USB5] comment = USB5 path =/media/usb5 browseable = yes guest OK = yes
Writeable = yes

 

5. Reload the configuration file

root@Ubuntu1:/etc/samba# /etc/init.d/smbd reload

 

Iv. Test

√ U Disk 1: Kingston 2G, FAT32

√ USB flash drive 2: SanDisk 8G, FAT32

√ USB flash drive 3: SanDisk 8G, NTFS

√ USB flash drive 4: Card Reader 8 GB, exFAT

√ Mobile hard drive: zarman 60 GB, NTFS

 

The effect is as follows:

 

V. Existing Problems

● Share only five USB devices, USB1 ~ 5. After unplugging, the device still exists.

● Shared content only has read permission and cannot be written

● The udev rule sometimes does not take effect. After the USB device is pulled out, the device is not uninstalled and the device number is occupied. For example, if sdb1 is inserted, sdb1 still exists after it is pulled out, and the number of data inserted to sdc1 is reduced.

(A writable Shell script is automatically uninstalled)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.