CLI mounting vs. GUI mounting
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 useumask
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
Related videos on Youtube
tilefrae
Updated on September 18, 2022Comments
-
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 over 9 yearsFor me no, because he write to console "START SLEEP" after sleeping interval :(
-
SleuthEye over 9 years
printf
is buffered. tryfflush(stdout)
after the firstprintf
-
mch over 9 yearsstdout is line buffered, you want to add a
\n
to print it immediately -
Paul Hankin over 9 yearspollingDelay isn't defined.
-
Leushenko over 9 yearspossible duplicate of Why does printf not flush after the call unless a newline is in the format string?
-
-
NicApicella about 11 yearsThanks a lot --
udisks
is exactly what I was looking for! For whatever reason, I'm getting "Mount failed: Not Aouthorized" when running it viassh
; if I'm not pushing my luck, pointers would be appreciated as to how to fix it. ^^ -
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 usesudisks
, though.) -
Eliah Kagan about 11 years@Rinzwind Will a non-root user be able to mount the device?
-
NicApicella about 11 years@EliahKagan Yes, if you set the appropriate options in fstab (uid, user, ...).
-
Eliah Kagan about 11 years@NicolaApicella I'm hoping this answer might be expanded to include information about that. :)
-
Rinzwind about 11 years@NicApicella he likes to boss me around :=)
-
tilefrae over 9 years@The Paramagnetic Croissant not working fflush(stdout) help me.