Linux auto-sharing USB device: Udev+samba

Source: Internet
Author: User
Tags create directory goto

I. Overview

The company recently asked me to implement the USB device into Ubuntu, automatically shared to the network, like Windows share (such as \\192.168.1.10) access to content inside, do not need write permission. At that time listen to this demand, I this new person said stunned, immediately search the relevant documents on the Internet, and run to ask the supervisor, this function has been realized? The supervisor said with certainty that it was necessary. The technical documents found on the Internet only plug into the USB device automatically mounted, automatic sharing did not search, this had to rely on their own.

After a toss-up finally in the Ubuntu14.04 desktop version of the implementation of this feature. This document shares 5 USB devices by default, USB1~5;USB device insertion is automatically shared to the "\\ host ip\share" directory, the shared directory disappears instantly when unplugged, and the file system supports common FAT32/NTFS/EXFAT formats.

  Implementation process:

1. Insert USB device, auto mount (Udev rules)

2, Mount, use the SMB protocol to share to the network, the client has Read permissions (Samba server)

3, background automatic execution of the uninstall device script


Second, the principle

  Use Udev to efficiently and dynamically manage Linux device files

  Automatic mounting of USB devices with Udev

  Installation and configuration of the Samba server under CentOS 6.3

Third, the configuration

( Note: After the Ubuntu 14.04 Samba is installed, the service starts up by default, and neither SELinux nor firewalls affect the service, and other distributions may need to be configured. )

1, install exFAT package, support exFAT format file system

[Email protected]:~# apt-get Install exfat-utils


[Email protected]:~# cd /etc/udev/rules.d/#udev规则目录 [email protected]:/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 "#当检测到sdb的设备, execute create directory and mount After the device is removed, 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" [email protected]:/etc/udev/rules.d# shutdown -r now# Restart System 


3. Install Samba

[Email protected]:~# apt-get Install samba Samba-common


4. Edit Samba configuration file, share/media directory

[Email protected]:~# vim/etc/samba/smb.conf [Share] comment = Share path =/media# shared path browseable = yes Guest OK = yes# Anonymous share writeable = Yes[email protected]:/etc/samba#/etc/init.d/smbd reload# Reload


5, write a script to prevent the system bug does not automatically uninstall the device

[Email protected]:~# vim usb.sh #!/bin/bashusb1=/dev/sdb?usb2=/dev/sdc?usb3=/dev/sdd?usb4=/dev/sde?usb5=/dev/sdf?# Variable assignment while true; #无限循环doif! [-e $usb 1] &&! [-e $usb 2] &&! [-e $USB 3] &&! [-e $USB 4] &&! [-e $usb 5] #如果不存在USB1-to device files then/bin/umount/media/usb* 2>/dev/null/bin/rmdir/media/usb* 2>/dev/null# perform uninstall and delete Record operation Else Exitfisleep # done[email protected]:~# chmod +x usb.sh# execute once per second


6. Move script to/shell directory

[Email protected]:~# mkdir/shell[email protected]:~# MV Usb.sh/shell


7. The script starts from start

[Email protected]:~# vim/etc/rc.local nohup/shell/usb.sh & #脚本后台执行


Iv. Testing

  √ USB 1: Kingston 2G, FAT32

  √ u disk 2:sandisk 8G, FAT32

   √ usb 3:sandisk 8G, NTFS

  √ USB 4: Reader 8G, ExFAT

 √ mobile HDD: Zalman 60G, NTFS

The effect is as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/70/B1/wKiom1W7bBvhzhLJAAPHS5myWSw511.jpg "title=" 111031467996749.png "alt=" Wkiom1w7bbvhzhljaaphs5mywsw511.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/70/AE/wKioL1W7bg2SAOuwAAHcSV3dDBE073.jpg "style=" float: none; "title=" 111031572689184.png "alt=" Wkiol1w7bg2saouwaahcsv3ddbe073.jpg "/>


This article is from "Ops rookie. Log" blog, make sure to keep this source http://sjy000.blog.51cto.com/8412654/1680704

Linux auto-sharing USB device: Udev+samba

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.