I use autofs to mount my NFS shares on demand in my Kubuntu.
autofs is a program for automatically mounting directories on an as-needed basis. Auto-mounts are mounted only as they are accessed, and are unmounted after a period of inactivity. Because of this, automounting NFS/Samba shares conserves bandwidth and offers better overall performance compared to static mounts via
In your case this is useful:
When specifying a CIFS share in a map file, specify
-fstype=cifs and precede the share location with a colon (
mntpoint -fstype=cifs ://example.com/shrname
Example: Mount read-write, specifying a user and group to own the files:
mntpoint -fstype=cifs,rw,uid=myuserid,gid=mygrpid ://example.com/shrname
Example: Mount read-write, specifying a username and password to use to connect to the share:
mntpoint -fstype=cifs,rw,username=myuser,password=mypass ://example.com/shrname
A map file is
/etc/auto.* or e.g.
/etc/auto.master.d/*. Read the documentation and learn how to configure the daemon.
At the first glance it looks like this is not the perfect solution for you because it seems you have to store the credentials in a file. This would be a map file (like in the example above) or a separate file e.g.
credentials.txt as you can see here. However there is this comment there:
I suppose you could make
credentials.txt a named pipe and run a password prompting program to feed the pipe.
If this is right then I expect this "password prompting program" may be just a single
printf you invoke by hand.
What I'm asking for is an easy way to do this without having to manually invoke the command at a prompt, and without having to store (or fudge) the credentials in a file.
Linux can mount/unmount various filesystem via
umount.* executables. This
credentials=/etc/credentials.txt option in the linked example is in fact an option to
mount.cifs. I think if you use something like
mount -t foo …
mntpoint -fstype=foo …
it will try to find and run
mount.foo, passing all other options to it.
So you should create
mount.mycifs as a wrapper over
mount.cifs. It should prompt you for your credentials somehow (straightforward
xterm -e … maybe, use
dialog or something else; but read this please), add
-o username=…,password=… or
-o credentials=… to the rest of options and pass them to
mount -t cifs) which does the actual mounting.
If you have
umount.cifs then make
umount.mycifs a symlink to it.
-fstype=mycifs in your map file without any options related to credentials.
/sbin/mount.mycifs is a quick and dirty proof of concept. Understand what it does before you run it in your OS because it will be run as
root, I'm a random guy on the Internet and you shouldn't trust me.
DISPLAY=:0 XAUTHORITY=/home/ola/.Xauthority xterm -e /bin/bash -c '\
read -p "user:" u; \
read -sp "password:" p; \
printf "username=%s\npassword=%s" "$u" "$p" > "$0"; \
mount -t cifs "$@" -o credentials="$tmpf"
It should be owned by
root:root or whatever is proper for
mount.* in your OS. Don't forget to make it executable (
sudo chmod a+x /sbin/mount.mycifs), it won't work otherwise. Notice there's a nasty hack with
XAUTHORITY that allows the
autofs daemon to display
xterm window on your(?) screen but in general it shouldn't do it. The hack is only for the daemon,
mount -t mycifs … should be able to display
xterm without the hack if invoked from within your desktop environment.
To make it less dirty you should write yet another program or script and run it with your local user's limited permissions before you access a directory where your CIFS would be automounted. This script should wait for a signal from
mount.mycifs, prompt you for the credentials (it can display windows etc. without nasty hacks) and pass them to
mount.mycifs which shouldn't display any windows nor prompts on its own.