Linux auto-sharing USB device: Udev+samba

Source: Internet
Author: User
Tags goto

I. Overview

The company recently asked me to implement a USB device that is automatically shared to the network after it is plugged into Ubuntu, and can access content like Windows sharing (such as \\192.168.1.10) without writing. 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 some toss in the Ubuntu14.04 desktop version of the function, but not perfect, for example: The default is only shared out 5 USB devices, usb1~5, unplug the device also exists. The file system supports common FAT32/NTFS/EXFAT formats. Want to vomit groove, until now do not know this wonderful demand for what to do ...

  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 permission (Samba)

Second, the principle

Udev: A feature in the Linux 2.6 kernel for managing device files under/dev, located in user space and running as daemons. UDEV enforces defined rules when there are matching device files by defining Udev rules

Samba: Installing on Linux, sharing local folders with the SMB/CIFS network protocol, allowing Windows/linux hosts to access on the network

 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

sudo Install exfat-utils

2, define UDEV rules, realize USB device automatically mount

[Email protected]:~# cd/etc/udev/rules.d/#udev规则目录 [email protected]:/etc/udev/rules.d# vim --persistent-net.rules #编辑规则文件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的设备, perform the creation of the directory and mount it; 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!="The 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 #重启系统

3. Install Samba for shared directories

Install Samba Samba-common

4. Edit the Samba configuration file

[Email protected]:~#vim/etc/samba/smb.conf[USB1] Comment=USB1 Path=/MEDIA/USB1#corresponds to the first USB pathbrowseable = yes#Browse PermissionsGuest OK = yes#Anonymous sharingwriteable = yes#whether it can be written[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=yeswriteable= yes

5. Reload the configuration file

[Email Protected]:/etc/samba#  /etc/init.d/smbd Reload

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:

V. Existing issues

Only 5 USB devices are shared, usb1~5, device still exists after unplugging

Shared content is read-only and not writable

Udev rules sometimes do not take effect, the device does not unload after the USB device is unplugged and occupies the device number. such as inserting sdb1, after unplugging sdb1 still exist, re-inserted into the SDC1, the available quantity will be reduced

(Can write shell script automatically uninstall solve)

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.