Samba – Mount with Password Prompt as Non-Root User

command linenot-root-usersamba

I want to mount a password-protected SMB share (served by a Windows machine). The share is protected by a user name and password, and I may not write the password in a file, I want to be prompted for the password at mount time.

I need a solution that works even for when the user on the client machine does not have any administrative privileges, so whatever method is used to mount the share must not allow him to get root permissions. The initial installation can be done as root. Users must be able to specify arbitrary server names. My immediate need is with Ubuntu 12.04, but the wider applicable a solution is the better.

The client is headless, so I'm looking for a command-line tool.

What I tried:

  • mount.cifs: while it can be made setuid root, its authors do not consider it secure. Running it under sudo has the same problem.
  • smbnetfs, fusesmb: I couldn't convince either of them to prompt me for a password.
  • Nautilus and gvfs: gvfs-mount smb://servername/sharename fails with Error mounting location: volume doesn't implement mount.

How can I mount a Samba share from the command line, as a non-root user, with a password prompt?

Best Answer

“Error mounting location: volume doesn't implement mount” apparently translates to “I need D-Bus but it isn't available”. (Thanks to venturax's guru colleague for this information.) Within an SSH session, I can use gvfs-mount provided that dbus-daemon is launched first and the environment variable DBUS_SESSION_BUS_ADDRESS is set.

export $(dbus-launch)
gvfs-mount smb://workgroupname\;username@hostname/sharename
# Type password
ls ~/.gvfs/'sharename on hostname'

gvfs-mount and other GVFS utilities must all talk to the same D-Bus session. Hence, if you use multiple SSH sessions or otherwise use mounts across login sessions, you must:

  • start D-Bus the first time it is needed, at the latest;
  • take care not to let D-Bus end with the session, as long as there are mounted GVFS filesystems;
  • reuse the existing D-Bus session at login time if there is one.

See Reuse D-Bus sessions across login sessions for that.

Related Question