23

On Ubuntu 12.10, when I want to mount a hard drive, I can just click on the unmounted drive and everything works fine (by mounting to /media/username/partitionlabel).

Basically, I would like to do exactly that via command line (for a script I'm working on).

Since I do not want to automount on boot, fstab is out of the question (right?).

When I use mount on CLI, I need to specify a mountpoint (which needs to have a previously created mountpoint; also, I need to take care of permissions and whatnot) -- what I don't understand is where does the GUI take all its infos from? The mountpoint seems to depend on the partition's label, but such a directory doesn't exist before mounting. Also, the GUI way doesn't seem to care too much about a user not being root.

Is there an "easy" way to mount via CLI, just like it does on the GUI by clicking on an unmounted drive?

5 Answers 5

17

Nautilus and other file managers that mount drives (i.e., the GUI) use the udisks command.

This provides a dynamic mount, where the mount point is created for the drive on-the-fly, and when the drive is unmounted, the mount point goes away.

See man 1 udisks for details on how to use this command (either manually, or called from a script/application). To learn more about how udisks works, see man 7 udisks.

To mount a device with udisks, you must specify a valid device name for the device. Specifying the device's volume name will not work. So, you can use udisks like this:

udisks --mount /dev/sdb1

When manually using udisks from the command-line, you might be best off to simply plug in a device, then run dmesg | tail shortly thereafter (see man dmesg and man tail) to discover the device name of the newly attached device.

This may not be the best way to write a script that mounts devices with udisks, however. I don't know exactly how you should do it, and it would be difficult to know since you haven't told us precisely what you want this script to do and when you want it to mount devices.

3
  • Thanks a lot -- udisks is exactly what I was looking for! For whatever reason, I'm getting "Mount failed: Not Aouthorized" when running it via ssh; if I'm not pushing my luck, pointers would be appreciated as to how to fix it. ^^ Mar 21, 2013 at 11:44
  • 2
    @NicolaApicella How are you running it? You'll get a not authorized message if you try to run it from a remote session (including SSH) or from a crontab; by default non-root users can only mount volumes with udisks from local interactive logins. (They can still run a script from a local interactive login that uses udisks, though.) Mar 21, 2013 at 11:49
  • Yes, sorry, just noticed (and edited) my previous comment: I'm indeed running it from ssh. (And currently googling a fix.) Mar 21, 2013 at 11:49
7

Since I do not want to automount on boot, fstab is out of the question (right?).

Nope. noauto keyword makes it skip mounting at boot time.

Is there an "easy" way to mount via CLI, just like it does on the GUI by clicking on an unmounted drive?

Easiest would be to add it to /etc/fstab with noauto and use ...

mount /dir

This will have mount probe for a mountpoint and if not found then for a device in /etc/fstab. /dir needs to exist though.

If either device or UUID are provided /etc/fstab is not probed. Like so ...

mount /dev/sda1 /media/directory

Or use udisks (I will let Eliah handle that >:-) )


mount generally is super user only.

Example options:

  • rw,noauto,user mounts it as read+write but not at boot and you can do it as a user.

  • You can also add in permissions for a user in /etc/fstab for files. You can use umask for that.

  • umask=0444 : everyone read, no write, no execute.

  • umask=0333 : everyone read, execute.

  • umask=0338 : owner, group read and execute; others, nothing

5
  • OK, I'll probably go ahead and try that, since it seems to be the "way to do it" anyway. :-) So I guess I'll have to create a mountpoint-directory (with correct rights) anyway. ;-) Mar 21, 2013 at 11:41
  • @Rinzwind Will a non-root user be able to mount the device? Mar 21, 2013 at 11:49
  • 1
    @EliahKagan Yes, if you set the appropriate options in fstab (uid, user, ...). Mar 21, 2013 at 12:09
  • @NicolaApicella I'm hoping this answer might be expanded to include information about that. :) Mar 21, 2013 at 12:10
  • @NicApicella he likes to boss me around :=)
    – Rinzwind
    Mar 21, 2013 at 13:10
3

In one of the comments, you said:

For whatever reason, I'm getting "Mount failed: Not Aouthorized" when running it via ssh

I use pmount instead of udisks. It works over ssh.

If I remember correctly, this is the syntax/etc I use with it - most of mine are hidden behind aliases at this point:

pmount /dev/sdb1 my_usb
ls /media/my_usb/
eject /media/my_usb
2

Use

udisks --mount /dev/sdb1

(replace /dev/sdb1 with the device you want to mount)

1

udisksctl

An update, FYI.

The command suggested in previous comment is not udisks anymore but udisksctl in recent linux distributions.

udisksctl mount -b /dev/sdc1

refereces

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.