Well, I thought this would have been easier, but I simply cannot find whether what I want is doable.
I want to mount a network Samba share from the command line in Ubuntu 20.04 (MATE) - preferably without any changes in files or saving settings somewhere; just a command line.
So first I tried:
sudo mount -t cifs -o user=MYSMBUSER //192.0.2.1/myshare /tmp/myshare
This works great - in the sense of it mounting, listing files, and providing read-only access to files; but it does not allow writing files (permission denied).
That is seemingly because of:
https://unix.stackexchange.com/questions/68079/mount-cifs-network-drive-write-permissions-and-chown
You are mounting the CIFS share as root (because you used sudo), so you cannot write as normal user. If your Linux Distribution and its kernel are recent enough that you could mount the network share as a normal user (but under a folder that the user own), you will have the proper credentials to write file
Yes, I do want to "mount the network share as a normal user", and this might seem like an option - however, my username on Ubuntu is different from the Samba username, so:
An alternative is to specify the user and group ID that the mounted network share should used, this would allow that particular user and potentially group to write to the share. Add the following options to your mount: ...
First problem - how do you unmount the above; I have done:
$ sudo umount /tmp/myshare
$
... and as it reports no errors, I assume it completed fine, but then I try:
$ ls /tmp/myshare
file_from_share01.txt file_from_share02.txt ...
... files are still listed, so share is mounted; it turns out (via https://stackoverflow.com/questions/74626/how-do-you-force-a-cifs-connection-to-unmount ) to unmount it, I have to do:
$ sudo umount -a -t cifs -l
Here is the tricky part - if I want to mount as normal user, and yet specify SMB username, I'd have to write the same command as above, but without sudo
:
$ mount -t cifs -o user=MYUSER //192.0.2.1/myshare /tmp/myshare
mount: only root can use "--options" option
So, maybe I could have otherwise ran this as non-root, if I didn't have to use the user
option - but I do. So I cannot really use this command.
One suggestion for going around this is to specify options in fstab - via mount share cifs folder without sudo :
To be able to mount that folder as a normal user, include the mount options for the cifs folder in the configuration file /etc/fstab, and add the options noauto,user,
However, I do not want to do this, because I do not want to keep records of the SMB credentials on my machine - and if I have to edit fstab, then the onus is on me to delete the entry from fstab once that is done, and usually at that time I'm tired, I could forget, blah blah.
I found that this is likely some security feature:
https://unix.stackexchange.com/questions/365308/use-mount-o-with-a-non-root-user
The -o limitation (only root can specify it) is in place to protect the system: the administrator can set up whatever options are necessary, either directly using -o or using /etc/fstab; users can only cause user-controllable file systems to be mounted or unmounted, without specifying options, because file system options allow a number of hostile scenarios. As a result, some file systems support other ways of setting certain options, such as the USER environment variable used above with CIFS.
Eh yeah, but for me its more secure not to enter any SMB paths in /etc/fstab
at all.
So, I really would like to mount just by using a command line without changing any configuration files (including /etc/fstab
) anywhere.
I hoped that there is another command, and I found gvfs-mount
- but I have the same problem as Mount SMB share with gvfs from command line :
$ dbus-launch bash
$ gvfs-mount smb://192.0.2.1/myshare
This tool has been deprecated, use 'gio mount' instead.
See 'gio help mount' for more info.
Password required for share myshare on 192.0.2.1
User [locuser]: MYUSER
Domain [WORKGROUP]:
Password:
$
At this point, it appears that it mounted, but it in fact didn't:
$ ls /run/user/1000/gvfs/
$
... and via gvfs-mount -l
, we can see there is no corresponding mount point:
$ gvfs-mount -l
...
Volume(3): test_nfs
Type: GProxyVolume (GProxyVolumeMonitorUDisks2)
Mount(0): test_nfs -> file:///tmp/test_nfs
Type: GProxyMount (GProxyVolumeMonitorUDisks2)
Mount(0): myshare on 192.0.2.1 -> smb://192.0.2.1/myshare/
Type: GDaemonMount
... and so we cannot access any files with this "mount" anyways.
Again, how do you unmount this - you cannot do umount /tmp/myshare
, it is not considered mounted even; found that here Unmount gvfs samba share with open file :
$ ls ~/.gvfs
ls: cannot access '~/.gvfs': No such file or directory
$ gvfs-mount -u smb://192.0.2.1/myshare
This tool has been deprecated, use 'gio mount' instead.
See 'gio help mount' for more info.
After this, checking with gvfs-mount -l
confirms that the share is indeed gone)
And to address the mention of gio mount
- first note, that without launching a dbus-session, it fails with:
$ gio mount smb://192.0.2.1
gio: smb://192.0.2.1: volume doesn’t implement mount
... then, within a shell started by dbus-launch bash
, it behaves exactly the same as gvfs-mount
:
$ gio mount smb://192.0.2.1/myshare
Password required for share myshare on 192.0.2.1
User [locuser]: MYUSER
Domain [WORKGROUP]:
Password:
$
Looks like it mounted, but:
$ gio mount -l
...
Volume(3): test_nfs
Type: GProxyVolume (GProxyVolumeMonitorUDisks2)
Mount(0): test_nfs -> file:///tmp/test_nfs
Type: GProxyMount (GProxyVolumeMonitorUDisks2)
Mount(0): myshare on 192.0.2.1 -> smb://192.0.2.1/myshare/
Type: GDaemonMount
... again there is no local directory mount point.
The above being said: is it possible to mount SMB share, from the command line, without changing any local files (such as, and especially /etc/fstab
), without sudo, and with r/w permissions (which I've confirmed in Windows, the SMB user MYUSER has) - and if it is, how?
Well, posting this as an answer, since it turned out, some of those
gvfs
/gio
commands apparently worked - I just wasn't apparently looking at the right mount folder; apparently in my Ubuntu 20.04 it isn't:... but instead:
... and the files are there, and I can read/write ... Now I'm puzzled how it's still there, since I think I unmounted it ... oh well.
Well, I'm happy after all these decades of development, file sharing is so easy nowadays! And looking forward to further changes, hopefully we'll see
~/.cache/.etc/gvfs-2/
in use, so we get even more breakage and even more useless answers, that we will have to spend hours trawling, in the future! Isn't development of technology great?:)
The mount point isn't under ~/.cache/gvfs/ it's under /run/user/1000/gvfs
Replace 1000 with your own uid number
I'll mount one of my own shares:
I'll check to see if it's mounted:
I'll check the contents of the mounted share:
I'll unmount the share:
I'll check to see if it is still mounted: