2

I have a SAMBA/CIFS server where the users have their own directories. My client machines run Linux or Windows. What I am trying to do is to use that share in both OSes. But my users can login in any computer, and here is my problem: In Linux, I need to change the /etc/fstab and include something like this:

//sambaserver/username /mnt/samba smbfs username=<username>,password=<pass> 0 0  

My problem is I can't set the username and password statically, but I need it dynamic (I am not sure that is the proper term). So, trying to put it shortly: when some user logs in to the system I have to mount the share as its home directory, but having in mind that this user can login in other computer, so the /etc/fstab must 'catch' the username and password.
I hope my question is clear. Thanks in advance.

3
  • /mnt/samba is a single location and you are using a shared environment so that's not really going to work for you (you could easily have multiple users on at once). But you might have better luck with something from their home directory instead. You can't know the user's password on sambaserver but you can create a credentials file in each user's home directory. Then drop a call to smbmount in their .profile with "-o credentials=<0600 file owned by them in their homedir>". If that's something that could work for you, I can write up something a little more clear. Jun 9 '15 at 3:54
  • Thanks for the reply @SmilingDragon! I create the shares, so, as administrator, I know the users passwords. The location /mnt/samba was an example. Can you explain me your solution in deep? Maybe it could work better than mine.
    – verovan
    Jun 9 '15 at 19:00
  • Ok @verovan, I've made a proper answer detailing how I'd approach this, with some tweaks to make it a bit more future proof (allows user's to manage their own passwords if they change them etc) Jun 9 '15 at 21:19
1

Rather than try to mount samba shares for the logged in user in a single reusable location (which will cause issues if you have more than one user logged in), consider mounting into a location unique to each user (eg /home/<username>/sambaserver, or /mnt/samba/<username> in a pinch)

If you are working with Linux, smbmount is a simple user-space samba mounter that should do what you need.

Create a little script a bit like this:

#!/bin/sh
# Default samba server to connect to
SAMBASERVER="sambaserver"
USERNAME="$USER"

if [ -n "$1" ]
then
  # We've been given a different samba server
  SAMBASERVER="$1"
fi

if [ -n "$2" ]
then
  # We've been given a different user on the samba server to use
  USERNAME="$2"
fi

CREDSFILE="${HOME}/.${SAMBASERVER}.credentials"

# Try and make the mount point if it doesn't yet exist (and set suitable mount dir perms)
if [ ! -d "${HOME}/${SAMBASERVER}" ]
then
  mkdir "${HOME}/${SAMBASERVER}" && chmod 0555 "${HOME}/${SAMBASERVER}" || exit 1
fi

if [ ! -f "$CREDSFILE" ]
then
  echo "$CREDSFILE not found!"
  exit 1
fi

smbmount "\\${SAMBASERVER}\${USERNAME}" "${HOME}/${SAMBASERVER}" -o user="$USERNAME",credentials="$CREDSFILE" || exit 1

(Untested - you may need to tweak/debug/etc)

Then create a credentials file in each user's home directory called (for example) .sambaserver.credentials and set it's permissions to 0600, owned by that user:

username=<samba user>
password=<samba password>

Lastly, put a call to your above mount script in their .profile. eg: /usr/local/bin/mount-user-home.sh or /usr/local/bin/mount-user-home.sh sambaserver somedifferentusername for special cases.

This should work with multiple samba servers, multiple users & multiple users with multiple samba servers.

0

You might be looking for automount.

DESCRIPTION The automount program is used to manage mount points for autofs, the inlined Linux automounter. automount works by reading the auto.master(5) map and sets up mount points for each entry in the master map allowing them to be automatically mounted when accessed. The file systems are then automatically umounted after a period of inactivity.

So you can dynamically mount specific user directories using fixed credentials or even LDAP/AD tickets.

Example configuration for fixed password file:

$ cat auto.homedir
* -fstype=cifs,rw,credentials=/home/&/.smbcredentials,uid=&,gid=&,file_mode=755,dir_mode=755,users,sec=ntlmssp ://nas.example.com/&

Default configuration on CentOS 8 checks if the password file is present. If not, it will try to use kerberos,

1
  • ok, not sure why this showed up the in questions today....
    – sbluhm
    Oct 25 '20 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.