Ubuntu – Auto-mounting network shares per user

11.04cifsmountsharing

I have a server that has a number of CIFS shares that need to be accessed on a per-user basis. For example, I have a Music share which I have full access to, but my wife has read-only access.

When either myself or my wife log into our Ubuntu 11.04 laptop I would like these shares to be automatically mounted per user. Now I understand that if I mount as -t cifs without specifying a user then it will use the USER environment variable. However, I also need to specify a password, so how can I do that when each user has a different password?

I think my questions are:

  1. Is there a way for me to have a per-user /etc/fstab?
  2. If not, is there a way to specify that a mount is only applicable to a certain user?
  3. Also, the share password is always the same as the local password. Is there a way to specify that this password should just pass through from the client to server rather than having to specify it in a credentials file somewhere?

Or maybe I'm missing something and there's a completely different solution. Can anyone help?

Best Answer

There are probably several solutions possible; here's is how I would do it. (Disclaimer: untested!)

The mount.cifs command can read the username and password from the USER and PASSWD environmental variables, but it can also read them from a "credentials" file, that you specify on the command line with the -o cred=/path/to/credentials/file option.

The credentials-file approach is IMHO simpler to implement.

  1. Create a text file $HOME/.Music.cred to store the credentials; the file should have this format:

    user=your-username-on-cifs-server
    password=the-password
    domain=leave-this-blank-unless-really-using-windows-domains
    
  2. Protect the $HOME/.Music.cred file; run this command in a terminal:

    chmod go-rw $HOME/.Music.cred
    
  3. Now you should be able to mount the CIFS share //server/music on directory MyMusicFolder using this command:

    sudo mount -t cifs -o cred=$HOME/.Music.cred //server/music $HOME/MyMusicFolder
    

    You can enable each user to run this through passwordless sudo by adding a line to /etc/sudoers: (one line per user)

    # replace every occurence of `user` with the actual account name
    user ALL= NOPASSWD: /bin/mount -t cifs -o cred=/home/user/.Music.cred //server/music /home/user/MyMusicFolder
    
  4. If the command from step 3. worked correctly, you can make it automatic in several ways:

    • save it into a shell script into your home directory and make that script an auto-started application (you have to do this for every user that needs to mount CIFS shares);
    • save it into a shell script /etc/X11/Xsession.d/30mount-cifs-shares so that it will work for any user.

Alternatively, you can replace steps 3. and 4. above with the use of pam-mount:

  1. install package libpam-mount

  2. configure /etc/security/pam_mount.conf.xml with:

    <debug enable="1" />
    <volume server="server" path="music" mountpoint="~/MyMusicFolder" options="cred=/home/%(USER)/.Music.cred" />
    

References:

Related Question