Tuesday, March 9, 2010

HOW TO MOUNT USB DEVICE LINUX

USB storage devices have the enormous advantage that for the most part they use a standard set of protocols. Thus, instead of needing individual drivers, as does much computer hardware, a standard driver permits access to the devices, making them very portable and able to easily work on many platforms.

Automounting

Mounting

By default, storage devices that are plugged into the system mount automatically in the /media directory, open a file browser window for each volume and place an icon on your desktop. If you plug in a usb hard disk with many partitions, all of the partitions will automatically mount. This behaviour may not be what you want so you can configure it as shown below.

If the volumes have labels the icons will be named accordingly, otherwise they will be named "disk" and as more volumes are added, you can get "disk-1" and so on.

To change the volume label see RenameUSBDrive.

Configuring Automounting

To enable or disable automount open a terminal and type gconf-editor followed by the [Enter] key.

Browse to /apps/nautilus/preferences/media_automount.

The media_automount key controls whether to automatically mount media. If set to true, then Nautilus will automatically mount media such as user-visible hard disks and removable media on start-up and media insertion.

There is another key /apps/nautilus/preferences/media_automount_open. This controls whether to automatically open a folder for automounted media. This key can also be set in the Nautilus (file manager) window. From the Edit menu in Nautilus select Preferences and then select the Media tab.

If set to true, then Nautilus will automatically open a folder when media is automounted. This only applies to media where no known x-content/* type was detected; for media where a known x-content type is detected, the user configurable action will be taken instead. This can be configured as shown below.

Configuring Program Autostart

To control which programs automatically start when you plug in a device, go to System->Preferences->Removable Drives and Media.

Ubuntu 9.10 (Karmic Koala)

In Ubuntu 9.10 (Karmic Koala) Removable Drives and Media is no longer in the Preferences menu but you can still do this as follows.

This user configurable action for specific media types can be configured in the Nautilus (file manager) window. From the Edit menu in Nautilus select Preferences and then select the Media tab. There you can select what happens when media is inserted. For example we need to turnoff audio CD auto play. CD audio "can do nothing" or "ask what to do" or "open folder".

For more complex scenarios, see UsbDriveDoSomethingHowto.

Unmounting/Ejecting

Before you disconnect the device, don't forget to unmount it. This can be done by right clicking the desktop icon and selecting "Unmount" (or in some cases, "Eject"). In Ubuntu 9.10 (Karmic Koala), in the file manager window you can also click on an "eject" button against the name of the mounted volume.

Automounting (Ubuntu Server)

By default, disk drives do not automount in Ubuntu Server Edition. If you are looking for a lightweight solution that does not depend on HAL/DBUS, you can install "usbmount".

Manually Mounting

Using mount

Get the Information

IconsPage/IconGNOMETerminal.png Sometimes devices don't automount, in which case you should try to manually mount it. First, you must know what device we are dealing with and what filesystem it is formatted with. Most flash drives are FAT16 or FAT32 and most external hard disks are NTFS.

sudo fdisk -l

Find your device in the list, it is probably something like /dev/sdb1. For more information about filesystems, see LinuxFilesystemsExplained.

Create the Mount Point

Now we need to create a mount point for the device, let's say we want to call it "external". You can call it whatever you want, just please don't use spaces in the name or it gets a little more complicated - use an underscore to separate words (like "my_external"). Create the mount point:

sudo mkdir /media/external

Mount the Drive

IconsPage/example.png We can now mount the drive. Let's say the device is /dev/sdb1, the filesystem is FAT16 or FAT32 (like it is for most USB flash drives), and we want to mount it at /media/external (having already created the mount point):

sudo mount -t vfat /dev/sdb1 /media/external -o uid=1000,gid=100,utf8,dmask=027,fmask=137

The options following the "-o" allow your user to have ownership of the drive, and the masks allow for extra security for file system permissions. If you don't use those extra options you may not be able to read and write the drive with your regular username.

Otherwise if the device is formatted with NTFS, run:

sudo mount -t ntfs-3g /dev/sdb1 /media/external

IconsPage/IconNote.png You must have the ntfs-3g driver installed. See MountingWindowsPartitions for more information.

Unmounting the Drive

IconsPage/example.png When you are finished with the device, don't forget to unmount the drive before disconnecting it. Assuming /dev/sdb1 mounted at /media/external, you can either unmount using the device or the mount point:

sudo umount /dev/sdb1

or:

sudo umount /media/external

You cannot unmount from the desktop by right clicking the icon if the drive was manually mounted.

Using pmount

There is a program called pmount available in the repositories which allows unprivileged users to mount drives as if they were using sudo, even without an entry in /etc/fstab. This is perfect for computers that have users without RootSudo access, like public terminals or thin clients.

pmount can be used with the same syntax as mount (but without sudo), or quite simply as follows:

pmount  [ label ] 

Example:

  •  pmount /dev/sdb1 flash_drive
    This will mount the device /dev/sdb1 at /media/flash_drive

If you leave off the label option, it will mount by default at /media/device

To unmount the device, use pumount, like so:

pumount 

Example:

  •  pumount /dev/sdb1

For more help, you can see the man pages for pmount and pumount.

Unmounting Explained

IconsPage/IconHint2.png Before disconnecting devices, you must unmount them first. This is similar to "Safely Remove" in Windows in that the device won't unmount until data is finished being written to the device, or until other programs are finished using it. This applies to all types of storage devices, including flash drives, flash cards, external hard drives, ipods and other media players, and even remote storage like Samba or NFS shares.

Failure to unmount before disconnecting the device can result in loss of data and/or a corrupted file system. There are no exceptions to this rule. Be safe - unmount your drives before disconnecting them!

Other Useful Commands

To see a list of your USB devices (the vendor and device ID's), run:

lsusb

To see all attached storage devices and their partitions, run:

sudo fdisk -l

To see information about currently mounted systems, simply run:

mount

No comments:

Post a Comment