Hotplug an external usb hard drive

Posted on 2008-11-04
Last Modified: 2013-12-06
hi all,

i'm wanting to hotplug an external usb device on my debian box.
I can mount the drive using: mount /dev/sdb1 /mnt/usb
the question is how do i hotplug this device?

thanx in advance


Question by:onlinede
    LVL 3

    Accepted Solution

    If you are using as desktop machine usually is enough to add the user to the plugdev group. That will enable the user to mount all usb disks connected. Gnome and KDE automatize this, and enable umount them as well very easily.

    If it's for a server, you should use autofs instead:

    apt-get install autofs

    In the config file: /etc/auto.master add:
    /mnt       /etc/auto.removable      --timeout=60

    In the config file /etc/auto.removable add:
    usb         -fstype=vfat,rw,gid=100,umask=002,[other options you want]       :/dev/sdb1

    The you just do:
    /etc/init.d/autofs restart

    And whenever the sdb1 is connected, /mnt/usb will work. After 60 seconds of inactivity, it will umount automatically, so it's safe to remove it. (if you try to list it again, it will be mounted again). Tune the timeout to match your needs.

    For memory cards the timeout can be adjusted to a much lower timeout, so they can be extracted anytime safely. Add a line:
    /mnt/cards       /etc/auto_cards.removable      --timeout=2

    and in /etc/auto_cards.removable configure the devices that should be considered cards.

    Author Comment


    there is no /etc/auto.removable

    LVL 3

    Expert Comment

    Yeah, you could name it as you want, so you should create the file.

    In /etc/auto.master you set which file will be loaded, in this example I used /etc/auto.removable, but if you prefer you could use other name (like the /etc/auto_cards.removable of the second example)

    Author Comment


    shouldn't this: /mnt     /etc/auto.removable      --timeout=60 be /mnt/usb     /etc/auto.removable     --timeout=60

    also [other options you want]?

    usb         -fstype=vfat,rw,gid=100,umask=002,[other options you want]       :/dev/sdb1

    I'm lost with this section? what if I remove it so it reads:

    usb         -fstype=vfat,rw,gid=100,umask=002,       :/dev/sdb1

    will this work?

    LVL 3

    Expert Comment

    no, /mnt is the base directory. Later on auto.removable yo set usb, disk, and other labels, that will be mounted on /mnt/<label>

    The other options of course you should remove it if your don't need other options. :) Perhaps it's interesting to add sync, that avoids write cache, that can cause data loss on removable media (if you remove the media before the cache gets flushed to disk).

    To resume:

    /mnt     /etc/auto.removable      --timeout=60

    usb         -fstype=vfat,rw,gid=100,umask=002,sync       :/dev/sdb1

    (without the semicolon if you are not adding more options)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
    This article will explain how to establish a SSH connection to Ubuntu through the firewall and using a different port other then 22. I have set up a Ubuntu virtual machine in Virtualbox and I am running a Windows 7 workstation. From the Ubuntu vi…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now