CLI mounting vs. GUI mounting

1,207

Solution 1

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.

Solution 2

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

Solution 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

Solution 4

Use

udisks --mount /dev/sdb1

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

Solution 5

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

Share:
1,207

Related videos on Youtube

tilefrae
Author by

tilefrae

Updated on September 18, 2022

Comments

  • tilefrae
    tilefrae over 1 year

    why my program NOT printf ?

    First asleep and then writes , but he should do it the other way round..

    #include <stdio.h>
    #include <stdlib.h>
    #ifdef _WIN32
    #include <Windows.h>
    #else
    #include <unistd.h>
    #endif
    
    void sleepMilliSecond(long milliSecondInput) {
    #ifdef _WIN32
        Sleep(milliSecondInput); // v milliSecondach
    #else
        usleep(pollingDelay * 1000); //microsekundy -> milisekundy
    #endif
    }
    
    
    
    int main(int argc, char** argv) {  
        printf("start sleep");    
        sleepMilliSecond(1000);   //sleep 1s 
        printf("stop sleep");
        return (EXIT_SUCCESS);
    }
    

    Output of program is: sleep and then he write start sleep stop sleep, WHY?

    EDIT: Working solution is:

    printf("start sleep");    
    fflush(stdout);
    sleepMilliSecond(10000); 
    printf("stop sleep");
    
    • tilefrae
      tilefrae over 9 years
      For me no, because he write to console "START SLEEP" after sleeping interval :(
    • SleuthEye
      SleuthEye over 9 years
      printf is buffered. try fflush(stdout) after the first printf
    • mch
      mch over 9 years
      stdout is line buffered, you want to add a \n to print it immediately
    • Paul Hankin
      Paul Hankin over 9 years
      pollingDelay isn't defined.
    • Leushenko
      Leushenko over 9 years
  • NicApicella
    NicApicella about 11 years
    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. ^^
  • Eliah Kagan
    Eliah Kagan about 11 years
    @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.)
  • Eliah Kagan
    Eliah Kagan about 11 years
    @Rinzwind Will a non-root user be able to mount the device?
  • NicApicella
    NicApicella about 11 years
    @EliahKagan Yes, if you set the appropriate options in fstab (uid, user, ...).
  • Eliah Kagan
    Eliah Kagan about 11 years
    @NicolaApicella I'm hoping this answer might be expanded to include information about that. :)
  • Rinzwind
    Rinzwind about 11 years
    @NicApicella he likes to boss me around :=)
  • tilefrae
    tilefrae over 9 years
    @The Paramagnetic Croissant not working fflush(stdout) help me.