HowTo: Auto Mounting Samba Shares Using AutoFS

Write tutorials for Linux Mint here
More tutorials on https://github.com/orgs/linuxmint/discu ... /tutorials and (archive) on https://community.linuxmint.com/tutorial
Forum rules
Don't add support questions to tutorials; start your own topic in the appropriate sub-forum instead. Before you post read forum rules
altair4
Level 20
Level 20
Posts: 11704
Joined: Tue Feb 03, 2009 10:27 am

HowTo: Auto Mounting Samba Shares Using AutoFS

Post by altair4 »

HowTo: Auto Mounting Samba Shares Using AutoFS

This HowTo assumes you can already access a samba share but want to have these shares available automatically when you log into your machine.

AutoFS is a unique utility that allows a user to automatically connect to, mount, and use a samba share when the desired mount point is accessed and disconnects when not in use. It offers some distinct advantages over other methods.

** Unlike the standard fstab method there is no delay in booting the machine if the target server is not present and because of the way it designed there is no delay if the server goes down before you logoff the client.

** It is not gvfs based so it provides further advantages:
-- gvfs has been rewritten and appears to be quite buggy at the moment so AutoFS can be used to replace Gigolo.
-- No gvfs dependency means it can be used in KDE ( It's used in OSX so it's truly cross platform )
-- No gvfs dependency means you can access samba shares in non-gvfs aware applications.
-- You have control over where the share is mounted

[1] Install the following packages:

Code: Select all

sudo apt-get install cifs-utils
sudo apt-get install autofs
[2] Edit /etc/auto.master as root ( gksu gedit /etc/auto.master) and add the following line after the "+auto.master" line:
# Note that if there are entries for /net or /misc (as
# above) in the included master map any keys that are the
# same will not be seen as the first read key seen takes
# precedence.
#
+auto.master
/mnt/Samba /etc/auto.sambashares --timeout=30 --ghost
/mnt/Samba is the parent folder under which all your samba shares will be mounted and can be set anywhere but it's best to stay away from your home directory and definitely stay away form /media since the system uses that for other things.
/etc/auto.sambashares is where you define how the shares are to be mounted - called the map file.
timeout is the amount of time before an unused share is unmounted.
ghost creates “ghost” versions (empty directories) of all the mount points - mounted or not.

[3] Create a new file which will define the actual mount - the map file:

Code: Select all

gksu gedit /etc/auto.sambashares
[4] Using the following as templates add the appropriate line to /etc/auto.sambashares:

[4a] If the share allows guest access you can use one of these variations:

Code: Select all

ShareName -fstype=cifs,rw,uid=1000,iocharset=utf8 ://server/share
ShareName -fstype=cifs,rw,uid=1000,guest,iocharset=utf8 ://server/share
ShareName -fstype=cifs,rw,username=guest,password=xxx,uid=1000,iocharset=utf8 ://server/share
After each variation is tried you need to restart autofs:

Code: Select all

sudo service autofs restart
The xxx in the password field is literal. And the reason for the variations is it is dependant on the server OS and the version of Samba / SMB being used on that server.

[4b] If you require full credentials to access the remote share you can use one of these:

Code: Select all

ShareName -fstype=cifs,rw,username=name,password=secret,uid=1000,iocharset=utf8 ://server/share
ShareName -fstype=cifs,rw,credentials=/home/altair/secret.txt,uid=1000,iocharset=utf8 ://server/share
The credentials file requires you to create in your own home directory a secret.txt file ( as in /home/altair/secret.txt ) that has the following content - example:

Code: Select all

username=altair
password=altairspw
In all cases your remote share will appear at /mnt/Samba/ShareName so choose something meaningful to you for ShareName.

[5-Optional] To restrict access to the auto.sambashares files so no one but root can see the username and password you can do this:

Code: Select all

sudo chmod 0600 /etc/auto.sambashares
And you can do the same for your secret file:

Code: Select all

chmod 0600 /home/altair/secret.txt
[6] Restart autofs:

Code: Select all

sudo service autofs restart
How this will work:
*** For all the shares specified in auto.sambashares you do not mount them under Network in your File Manager or "Connect to Server".
*** You access ( and mount ) them by accessing the mount point itself under /mnt/Samba in whatever application you are using.
*** Autofs will create the /mnt/Samba directory and the /mnt/Samba/ShareName mount point by itself based on the files you edited above.
*** The mount will automatically unmount itself after 30 ( based on "--timeout=30" in auto.master ) seconds of non use but you can adjust that to your needs. This is really all transparent to the user since any access will keep it alive.

EXAMPLE: This is an example of a /etc/auto.sambashares file that defines multiple shares on different servers:

Code: Select all

Box1-Downloads -fstype=cifs,rw,uid=1000,iocharset=utf8 ://box1.local/downloads
Box2-Documents -fstype=cifs,rw,uid=1000,iocharset=utf8 ://box2.local/documents
NAS-Test -fstype=cifs,rw,uid=1000,username=name,password=secret,sec=ntlm,iocharset=utf8 ://nas/test
Example notes:
*** The server can be expressed in the normal samba accepted formats, namely:
ip address ( ://192168.0.100/downloads )
mDNS qualified host name which all Linux and OSX machines use ( ://box1.local/downloads )
netbios or hostname alone ( ://box1/downloads )
*** It can accept certain options so in the nas example above I had to add the the old "sec=ntlm" option because the nas won't accept access without it.

More templates contained in this thread:

Multiple concurrent users setup: viewtopic.php?p=766521#p766521

Multiple non-concurrent users setup: viewtopic.php?p=822762#p822762
Last edited by altair4 on Thu Jul 21, 2022 7:08 am, edited 26 times in total.
Please add a [SOLVED] at the end of your original subject header if your question has been answered and solved.
altair4
Level 20
Level 20
Posts: 11704
Joined: Tue Feb 03, 2009 10:27 am

Re: HowTo: Auto Mounting Samba Shares Using AutoFS

Post by altair4 »

So what do you do when you have multiple concurrent users on the same client machine?

Let's say you have 2 users named john and mary.

[1] Create another directory for each local user, change ownership to that user, and change permissions to allow access only to that user. So for john:

Code: Select all

sudo mkdir /mnt/john
sudo chown john /mnt/john
sudo chmod 0770 /mnt/john
Do the same for the mary user

[2] Replace the one /etc/auto.sambashares line in /etc/auto.master to one line for each user:
/mnt/john/Samba /etc/auto.sambashares-john --timeout=30 --ghost
/mnt/mary/Samba /etc/auto.sambashares-mary --timeout=30 --ghost
[3] Then create those files:
/etc/auto.sambashares-john
/etc/auto.sambashares-mary

[4] Change the templates that I had in the original post to reflect the uid of each user. So for example mary's file at /etc/auto.sambashares-mary would look something like this:
server-share -fstype=cifs,rw,uid=1001,iocharset=utf8 ://server/share
Where 1001 is mary's uid number. To find that number run this command:

Code: Select all

id mary
[5] An now restart the autofs service:

Code: Select all

sudo service autofs restart
Last edited by altair4 on Thu Feb 13, 2014 3:39 pm, edited 2 times in total.
Please add a [SOLVED] at the end of your original subject header if your question has been answered and solved.
JulianGalbraith

Re: Save to a Samba Location in all Applications ( AutoFS )

Post by JulianGalbraith »

Hi. I found your topic searching around because I needed to access a samba share mounted in nemo with another application. Obviously it was mounting it somewhere since I can run and open files directly.



I would like to add that if users are looking to access the shares accross all application that all they need to do is:

1. Mount it with nemo
2. Navigate in their application to /run/user/$USERNAME/gvfs
3. It might be a good idea to add gvfs to your shortcut panel in the file browser.


Your method is great for certain purposes. But this is what I really needed for mine. Thanks!
altair4
Level 20
Level 20
Posts: 11704
Joined: Tue Feb 03, 2009 10:27 am

Re: HowTo: Auto Mounting Samba Shares Using AutoFS

Post by altair4 »

My post is in the HowTo section of the forum. As such it details a specific way to achieve a certain goal a certain way. Anyone having problems implementing it should feel free to post here and I will endeavor to try and find out why it is not working for them.

What this is not is a venue for others to propose alternatives to the posted method. That is not true because it is my HowTo but because it is simply a HowTo. This is the case not because it is in this forum but is pretty much the accepted norm in all forums.

Any member is always free to create their own Howto's and regardless of how much I might disagree with the approach I would afford the author the same courtesy.
Please add a [SOLVED] at the end of your original subject header if your question has been answered and solved.
ianmoone

Re: HowTo: Auto Mounting Samba Shares Using AutoFS

Post by ianmoone »

ok so I went through this and am trying to automount the drive on my aios media player I can access the mediaplayer through filezilla"/192.168.0.10/root/tmp/usbmounts/sda1"
but I am no sue if this is correct way to automount in the auto.samdashares file, I have this as the line "server-share -fstype=cifs,rw,uid=1000,iocharset=utf8 ://192.168.0.10/root/tmp/usbmounts/sda1"
after I added that line and saved it I sudo sevice autofs restart
the thing is I am not sure if it is actually mounted
altair4
Level 20
Level 20
Posts: 11704
Joined: Tue Feb 03, 2009 10:27 am

Re: HowTo: Auto Mounting Samba Shares Using AutoFS

Post by altair4 »

In reverse order:
the thing is I am not sure if it is actually mounted
Go into your file manager and go to /mnt/Samba/server-share. It's either mounted or not.
I have this as the line "server-share -fstype=cifs,rw,uid=1000,iocharset=utf8 ://192.168.0.10/root/tmp/usbmounts/sda1"
This HowTo concerns automounting a samba share and "//192.168.0.10/root/tmp/usbmounts/sda1" is not how you specify a samba share. It's also not at all clear that you created a samba share on the device you are trying to access. The format is: ://server/share
Please add a [SOLVED] at the end of your original subject header if your question has been answered and solved.
ianmoone

Re: HowTo: Auto Mounting Samba Shares Using AutoFS

Post by ianmoone »

I don't have /mnt/Samba/server-share I have /mnt/Samba/etc/auto.sambashares-timeout=30 I uess I messed that up.
I guess what I am actually dong is trying to be able to access the Aios mediaplayer which I am pretty sure this is totally different from what this tut is doing .
altair4
Level 20
Level 20
Posts: 11704
Joined: Tue Feb 03, 2009 10:27 am

Re: HowTo: Auto Mounting Samba Shares Using AutoFS

Post by altair4 »

If you haven't installed the samba server and created a samba share on this Aios mediaplayer thingy then you are indeed not using the correct HowTo.
I don't have /mnt/Samba/server-share I have /mnt/Samba/etc/auto.sambashares-timeout=30 I uess I messed that up.
Clearly I have done a poor job if that is how you read my HowTo. I will try to rewrite parts of it to make it more clear.
Please add a [SOLVED] at the end of your original subject header if your question has been answered and solved.
kwisher

Re: HowTo: Auto Mounting Samba Shares Using AutoFS

Post by kwisher »

altair4,

Great How-To. I am definitely bookmarking this for future reference when I have time to give it a try. I know you meant these instructions for Mint users but I am running Manjaro currently. Do you mind if I append instructions for Manjaro once I try your How-To? First thing I noticed is that the config files that you mentioned for editing are located at /etc/autofs, but so far that is the only difference.

Thank you.
altair4
Level 20
Level 20
Posts: 11704
Joined: Tue Feb 03, 2009 10:27 am

Re: HowTo: Auto Mounting Samba Shares Using AutoFS

Post by altair4 »

I don't know how relevant having a Manjaro addendum to this HowTo would be but if you want to modify it and use it on the Manjaro forum I don't see how I can stop you :)
Please add a [SOLVED] at the end of your original subject header if your question has been answered and solved.
kwisher

Re: HowTo: Auto Mounting Samba Shares Using AutoFS

Post by kwisher »

altair4 wrote:I don't know how relevant having a Manjaro addendum to this HowTo would be but if you want to modify it and use it on the Manjaro forum I don't see how I can stop you :)
I didn't mean Manjaro specific instructions per se, Just that other distro's might change the location of the config files.
altair4
Level 20
Level 20
Posts: 11704
Joined: Tue Feb 03, 2009 10:27 am

Re: HowTo: Auto Mounting Samba Shares Using AutoFS

Post by altair4 »

Note: This is a continuation of the original post which I reserve for additional templates as I think of them.

*** You can use environment variables to make a more flexible map mount:

[1] Multiple non-concurent users requiring credentials example:

Code: Select all

server-share -fstype=cifs,rw,iocharset=utf8,credentials=${HOME}/cred.secret,uid=${UID} ://server/share
Each user will have to create their own cred.secret file in their own home directory with this kind of content:

Code: Select all

username=altair4
password=altair4password
So if the user altair4 was logged into the system he would pass his credentials at /home/altair4/cred.secret and mount /server/share to /mnt/Samba/server-share with owner set to uid=1000.

[2] Multiple non-concurrent user mounts of their samba server's home directory ( i.e., a samba [homes] share ) example:

Code: Select all

server-Home -fstype=cifs,rw,iocharset=utf8,credentials=${HOME}/cred.secret,uid=${UID} ://server/${USER}
So if the user altair4 was logged into the system he would pass his credentials at /home/altair4/cred.secret and mount /server/altiar4 to /mnt/Samba/server-Home with owner set to uid=1000.
Please add a [SOLVED] at the end of your original subject header if your question has been answered and solved.
michaelzap
Level 3
Level 3
Posts: 166
Joined: Sat Sep 11, 2010 9:32 pm

Re: HowTo: Auto Mounting Samba Shares Using AutoFS

Post by michaelzap »

I've been having trouble with gvfs in Mate 16 x64, and I've never liked that fstab would try to mount my home server shares when my laptop was away from home, so this is a very useful tutorial for me - thanks!

So far I haven't gotten it to work, and I'm wondering what I might test to figure out why.

/home/zap/auto.master:

Code: Select all

/home/zap/mounts /home/zap/auto.sambashares --timeout=60 --ghost
+auto.master
/home/zap/auto.sambashares:

Code: Select all

colmena -fstype=cifs,rw,username=zap,password=mypassword,uid=1000,iocharset=utf8 ://colmena/shares
mp3s -fstype=cifs,rw,uid=1000,iocharset=utf8 ://vicio/mp3s
videos -fstype=cifs,rw,uid=1000,iocharset=utf8 ://vicio/videos
software -fstype=cifs,rw,uid=1000,iocharset=utf8 ://vicio/software
When I restart the autofs service it creates the mounts subdirectory in /home/zap but doesn't mount the shares there. Previously I tried this using the /mnt/samba and it did create the mount points, but they didn't work when I would try to open them (couldn't display contents - it may have been recently deleted). In the end I want to access these mounts in /home/zap/mounts (because I have FTP bookmarks that use this location), but I can create symlinks if I shouldn't mount autofs shares there.

How can I debug this to get it working? I don't see anything related in .xsessionerrors. Thanks!
altair4
Level 20
Level 20
Posts: 11704
Joined: Tue Feb 03, 2009 10:27 am

Re: HowTo: Auto Mounting Samba Shares Using AutoFS

Post by altair4 »

I've never tried autofs with the map file ( in your case /home/zap/auto.sambashares ) outside of /etc but theoretically it should work. But I'm fairly certain that the automount process will be looking for the auto.master file in /etc/ not in your home directory.

I would edit the existing master file where it is:

/etc/auto.master:

Code: Select all

/home/zap/mounts /home/zap/auto.sambashares --timeout=60 --ghost
+auto.master
As for the map file let's take the first mount as an example:
colmena -fstype=cifs,rw,username=zap,password=mypassword,uid=1000,iocharset=utf8 ://colmena/shares
Make sure the mount itself works by temporarily mounting it to say ... your Public folder by running this command in a terminal:

Code: Select all

sudo mount -t cifs //colmena/shares /home/zap/Public -o rw,username=zap,password=mypassword,uid=1000,iocharset=utf8
Note: To unmount:

Code: Select all

sudo umount /home/zap/Public
Please add a [SOLVED] at the end of your original subject header if your question has been answered and solved.
michaelzap
Level 3
Level 3
Posts: 166
Joined: Sat Sep 11, 2010 9:32 pm

Re: HowTo: Auto Mounting Samba Shares Using AutoFS

Post by michaelzap »

Thanks very much for your help with this.

I actually tried the auto.master file (as well as the auto.sambashares file) in etc also, but that didn't make any difference.

I'm not able to mount the shares normally via cifs in a terminal using either the server name or its ip, so that's the issue.

Code: Select all

zap@bangarang ~ $ sudo mount -t cifs //vicio/mp3s /home/zap/Public -o rw,uid=1000,iocharset=utf8
mount error: could not resolve address for vicio: Unknown error
zap@bangarang ~ $ sudo mount -t cifs //192.168.0.121/mp3s /home/zap/Public -o rw,uid=1000,iocharset=utf8
Password for root@//192.168.0.121/mp3s: <- not sure why it was asking me for that, as this share does not require a password
zap@bangarang ~ $ sudo mount -t cifs //colmena/shares /home/zap/Public -o rw,username=zap,password=mypassword,uid=1000,iocharset=utf8
mount error: could not resolve address for colmena: Unknown error
zap@bangarang ~ $ sudo mount -t cifs //192.168.0.120/shares /home/zap/Public -o rw,username=zap,password=mypassword,uid=1000,iocharset=utf8
Unable to find suitable address.
zap@bangarang ~ $ 
This almost seems like some sort of firewall blocking the traffic... Any ideas?
altair4
Level 20
Level 20
Posts: 11704
Joined: Tue Feb 03, 2009 10:27 am

Re: HowTo: Auto Mounting Samba Shares Using AutoFS

Post by altair4 »

Didn't you say you had this mounting from fstab once? Do you still have how you did that?
sudo mount -t cifs //192.168.0.121/mp3s /home/zap/Public -o rw,uid=1000,iocharset=utf8
Password for root@//192.168.0.121/mp3s: <- not sure why it was asking me for that, as this share does not require a password
Pass it the option guest:
sudo mount -t cifs //192.168.0.121/mp3s /home/zap/Public -o guest,rw,uid=1000,iocharset=utf8
Not being able top access this by ip address is a problem and you're right it looks like either a firewall problem or you are not in the same subnet.

Turn off your own firewall settings if you enabled any:

Code: Select all

sudo ufw disable
Do the same on the server - BTW, is the server a Linux box or a Windows box?

Is your own machine in the same ip range as the server - is your linux box 192.168.0.xxx?
Please add a [SOLVED] at the end of your original subject header if your question has been answered and solved.
michaelzap
Level 3
Level 3
Posts: 166
Joined: Sat Sep 11, 2010 9:32 pm

[SOLVED] Re: HowTo: Auto Mounting Samba Shares Using AutoFS

Post by michaelzap »

Looks like it's just not a great idea to try to mount these shares directly into your home directory (as far as I can tell). It was creating a mounts directory in my home directory that was owned by root, but then it didn't create the subdirectories for each mounted share. I switched back to doing it the way your tutorial outlines but mounted the shares by ip address and now it works.

/etc/auto.master:

Code: Select all

/mnt/samba /etc/auto.sambashares --timeout=60 --ghost
+auto.master
/etc/auto.sambashares:

Code: Select all

colmena -fstype=cifs,rw,username=zap,password=mypassword,uid=1000,iocharset=utf8 ://192.168.0.140/shares <-- note that I had this ip wrong before, so that also caused confusion
mp3s -fstype=cifs,rw,uid=1000,iocharset=utf8 ://192.168.0.121/mp3s
videos -fstype=cifs,rw,uid=1000,iocharset=utf8 ://192.168.0.121/videos
software -fstype=cifs,rw,uid=1000,iocharset=utf8 ://192.168.0.121/software
Now I just need to create symlinks in my home directory so that I don't need to change my FTP bookmarks and whatnot (though I could do that also).

Just since you asked, colmena is a Debian machine with Samba shares and vicio is a linux-based NAS device.

Thanks for your help and for putting together this tutorial!
altair4
Level 20
Level 20
Posts: 11704
Joined: Tue Feb 03, 2009 10:27 am

Re: [SOLVED] Re: HowTo: Auto Mounting Samba Shares Using Aut

Post by altair4 »

michaelzap wrote:Looks like it's just not a great idea to try to mount these shares directly into your home directory (as far as I can tell). It was creating a mounts directory in my home directory that was owned by root, but then it didn't create the subdirectories for each mounted share. I switched back to doing it the way your tutorial outlines but mounted the shares by ip address and now it works.
To be honest I never tried to have the mount points in a home directory. I should probably add that warning to the HowTo. I'd like to figure out why it's doing that though - I would think it would allow you to mount it anywhere.

EDIT: I just did a simple test of putting in my home directory and I'm not experiencing the same issue. By any chance do you have an encrypted home directory?
Please add a [SOLVED] at the end of your original subject header if your question has been answered and solved.
dclement

Re: HowTo: Auto Mounting Samba Shares Using AutoFS

Post by dclement »

Hello,

I'm trying to follow this tutorial to access the Samba shares on my NAS, but I'm struggling to get it to work. I seem to be running into the same error as michaelzap, but unlike him I couldn't solve it.

I can mount a share manually:

Code: Select all

sudo mount -t cifs -o username=daniel,password=<mypassword> //192.168.0.10/public ~/Public
or access it in the file browser under "Network".

Here is the matching entry in auto.sambashares:

Code: Select all

public fstype=cifs,rw,username=daniel,password=<mypassword>,uid=1000,iocharset=utf8 ://192.168.0.10/public
But if I try to browse the mount points under /mnt/Samba, I get e.g. "Cannot display folder contents / cannot find "public" - it may have been erased recently" (do my best to translate from French).

However, the firewall is not running on that PC. What am I doing wrong?

TIA - best regards, Daniel
altair4
Level 20
Level 20
Posts: 11704
Joined: Tue Feb 03, 2009 10:27 am

Re: HowTo: Auto Mounting Samba Shares Using AutoFS

Post by altair4 »

Please post your master file contents:

Code: Select all

cat /etc/auto.master
Please add a [SOLVED] at the end of your original subject header if your question has been answered and solved.
Post Reply

Return to “Tutorials”