Hotplug an external usb hard drive

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


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
onlinedeAuthor Commented:

there is no /etc/auto.removable

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)
onlinedeAuthor Commented:

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?

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)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux Distributions

From novice to tech pro — start learning today.