Automount Samba Shares with CIFS

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
Post Reply
altair4
Level 20
Level 20
Posts: 11458
Joined: Tue Feb 03, 2009 10:27 am

Automount Samba Shares with CIFS

Post by altair4 »

Automount Samba Shares with CIFS:

The advantage of a cifs mount is that it is does not depend on the File Manger / gvfs or any samba client processes since it is Linux Kernel based.

The best approach would be to do a manual mount first to settle on the desired set of options then set it up in fstab to have it mount automatically.

Note: Make sure a cifs "helper" utility is installed:

Code: Select all

sudo apt install cifs-utils
The basic syntax of a manual cifs mount is:
sudo mount -t cifs //server/share /mountpoint -o comma,seperated,list,of,options

server can be expressed as:
hostname = least reliable since cifs has no idea what a NetBIOS name is.
hostname.local = more reliable but the server must be running Linux, Win10, MacOS, or any network device designed for MacOS
ip-address = most reliable but the server must have a static ip address

/mountpoint has some consequences associated with it:

Under /media or in your home directory it will create mount icons on your desktop if your desktop allows it and on the side panel of your file manager. It can also interfere with a systemd automount should you decide to use that ( explained below ).

Under /mnt and it will do none of that. For the most part this is the desired location.


What follows is a list of use cases:

** Access a server as a guest user:
sudo mount -t cifs //server/share /mountpoint -o guest
Depending on what OS the server is running and how it is set up you will likely end up with a share that is writeable only to root.

You could replace root with your primary user by adding a uid=your-name:
sudo mount -t cifs //server/share /mountpoint -o guest,uid=altair
It will be readable to everyone on your system but writeable only to altair.

You can make it so it is writeable to every local user:

Code: Select all

sudo mount -t cifs //server/share /mountpoint -o guest,uid=altair,nounix,dir_mode=0777,file_mode=0666
You can even change it so only members of a specific group has write access:

Code: Select all

sudo mount -t cifs //server/share /mountpoint -o guest,uid=altair,gid=plugdev,nounix,dir_mode=0775,file_mode=0664
Altair will be the owner of the mounted share and write access will be permitted to him and everyone who is a member of the plugdev group.

** Accessing a server with credentials ( user name and password ):

Code: Select all

sudo mount -t cifs //server/share /mountpoint -o username=altair,password=altairpw,uid=altair
You could also place the credentials in a separate file and then secure it if you want.

## Create a file at /etc/samba/credentials

## With the credentials one item per line:
username=altair
password=altairpw

Then the line would be:

Code: Select all

sudo mount -t cifs //server/share /mountpoint -o credentials=/etc/samba/credentials,uid=altair
All of the other options ( nounix, dir_mode, file_mode, gid ) can be added.

SMB Dialects:

SMB dialects are not versions of samba or CIFS. They are the underlying protocol of the SMB system. CIFS automatically negotiates with the server on first contact the best smb dialect to use starting from SMB2.1 all the way up to SMB3.11.

If you have a server that can only be accessed by SMB1 or SMB2.0 cifs cannot make a connection. But you can override this limitation with the vers option:

SMB1 : vers=1.0
SMB2 : vers=2.0


NOTE: As of Linux Kernel 5.15 ( Mint 21 ) the sec=ntlm option is no longer available. If your device requires it and upgrading it to more current standards is not possible you are out of luck.
There is also the issue of security mode. By default cifs uses a security mode which is standard on all current operating systems. But if your server requires something earlier it can be set with this option:
sec=ntlm

So a manual mount of a guest accessible server that uses only SMB1 and requires a lower security level would look like this:
sudo mount -t cifs //server/share /mountpoint -o guest,uid=altair,vers=1.0,sec=ntlm
Atutomount in /etc/fstab:

Once you find the manual mount settings that are successful you can add a declaration to have it automount in /etc/fstab - but with a syntax change. I will use the one that allows everyone write access:

Code: Select all

//server/share /mountpoint cifs guest,uid=altair,nounix,dir_mode=0777,file_mode=0666,nofail 0 0
There may be a problem with this approach. There is a timing issue when booting into Linux in that the declarations in fstab may be read before the entire Linux networking stack is up and operational so it fails to connect.

There are two approaches to remedy this:

[1] You can set the mountpoint under /media ( or your home directory ) and add noauto,user and end up with links on the side panel of your File Manager that are "actionable" to an ordinary user - click on it and it will mount. Click or right click it again and it will unmount:

Code: Select all

//server/share /media/mountpoint cifs guest,uid=altair,nounix,noauto,user,dir_mode=0777,file_mode=0666 0 0
Remember that it will not mount at boot only when launched by the side link in your file manager.

[2] You can also do a systemd automount.

In this case I will create the mount point under /mnt:

Code: Select all

//server/share /mnt/mountpoint cifs guest,uid=altair,nounix,noauto,x-systemd.automount,dir_mode=0777,file_mode=0666 0 0
You can also add another option to have the share automatically unmount after left idle:
x-systemd.idle-timeout=300 == System will unmount share if idle for 300 seconds.


You then have to make systemd happy by running the following commands:

Code: Select all

sudo systemctl daemon-reload
sudo systemctl restart remote-fs.target
A systemd automount will also not mount at boot but unlike [1] it will mount when the mountpoint is accessed. It is seamless to the user or any process that accesses the mount point.

Note: Mount points under /media or your home directory when using a systemd.automount do not play well together. Udisks2 interferes with this process and will automount at login regardless of the noauto option. That is why it it would be best to have the mount point under /mnt.
Last edited by altair4 on Fri Sep 15, 2023 6:38 am, edited 3 times in total.
Please add a [SOLVED] at the end of your original subject header if your question has been answered and solved.
antcj
Level 3
Level 3
Posts: 148
Joined: Thu Jun 28, 2012 11:17 pm

Re: Automount Samba Shares with CIFS

Post by antcj »

Oops i posted in wrong area
so here it is again
Altair
using the cifs method you have posted, with systemd automount.

I have noticed that if the server is down then i no longer have access to local files on the pc.
I cant even open home folder.
i have seen this on 2 pc with mint20, after a clean install. I also noticed it on mint 19.3
Ive obviously missed something?
altair4
Level 20
Level 20
Posts: 11458
Joined: Tue Feb 03, 2009 10:27 am

Re: Automount Samba Shares with CIFS

Post by altair4 »

antcj wrote: Wed Oct 14, 2020 11:38 pm Oops i posted in wrong area
so here it is again
Altair
using the cifs method you have posted, with systemd automount.

I have noticed that if the server is down then i no longer have access to local files on the pc.
I cant even open home folder.
i have seen this on 2 pc with mint20, after a clean install. I also noticed it on mint 19.3
Ive obviously missed something?
I don't see how this line in fstab ...
//192.168.5.102/ant /mnt/ant cifs guest,uid=ant,nounix,noauto,x-systemd.automount,dir_mode=0777,file_mode=0777 0 0
.. would cause those symptoms.

It's not being mounted at boot or at login but when you access the mount point. I suppose the system might freeze for a bit if you try to access the mount point and the server is not there .... I guess it depends on when your symptom occurs.

Does it occur immediately after login or only after when you access the mount point when the server isn't present?

If it's the latter add another option to your list: x-systemd.mount-timeout=10
Systemd will try for 10 seconds ( user adjustable ) to mount share then stop if unreachable.

Just remember that every time you edit fstab you need to redo the systemd 2-step process to activate it.
Please add a [SOLVED] at the end of your original subject header if your question has been answered and solved.
User avatar
Moem
Level 22
Level 22
Posts: 16233
Joined: Tue Nov 17, 2015 9:14 am
Location: The Netherlands
Contact:

Re: Automount Samba Shares with CIFS

Post by Moem »

Mod note:
Some confusing posts removed at the request of the author. :)
Image

If your issue is solved, kindly indicate that by editing the first post in the topic, and adding [SOLVED] to the title. Thanks!
RobertGM
Level 2
Level 2
Posts: 75
Joined: Mon Dec 09, 2019 4:58 pm

Re: Automount Samba Shares with CIFS

Post by RobertGM »

While this post is a bit old, I have tried to follow the instructions but get lost at
The basic syntax of a manual cifs mount is:
I have tried and tried to get Samba working from Software Manager; including all the utilities mentioned in other posts on this topic. The various computers, all using Linux (3 x 20.1, 1 x 19.3) Mint appear to be seen by each other (according to GAdmin). But the folders (with computer names) will not open.
During the subsequent uninstall and re-install of Samba, etc, I received error messages regarding passwords.
I have tried some of the other backup/file sharing tools but I prefer the way Samba works by being able to specify shared folders (not whole computers).
I can start a new topic if you prefer.
altair4
Level 20
Level 20
Posts: 11458
Joined: Tue Feb 03, 2009 10:27 am

Re: Automount Samba Shares with CIFS

Post by altair4 »

(according to GAdmin)
Gadmin-samba?

If so the answer to this question:
I can start a new topic if you prefer.
Is it would be best if you do.

Gadmin-samba will result in a non-operative samba server. It's not your fault as it doesn't belong in the repositories. It should have been removed a decade ago.

You will need to start over on any machine you used it on and you don't do that by removing and reinstalling a bunch of packages. It's fairly simple to do and I can go through the steps in your other post.
Please add a [SOLVED] at the end of your original subject header if your question has been answered and solved.
RobertGM
Level 2
Level 2
Posts: 75
Joined: Mon Dec 09, 2019 4:58 pm

Re: Automount Samba Shares with CIFS

Post by RobertGM »

Thanks for the response altair4.
I started a new Topic: local network not working.
Infidelus
Level 4
Level 4
Posts: 273
Joined: Mon Feb 25, 2019 3:14 pm
Location: United Kingdom

Re: Automount Samba Shares with CIFS

Post by Infidelus »

Thanks for this guide. I've found it very useful switching from NFS to CIFS for a couple of shares I wanted to mount.

Just one question:

Are the dir_mode & file_mode mode entries required for fstab? The file server I'm connecting to already has permissions set for my user account and obviously anything I set in fstab (that gives more permissions) isn't going to change those permissions.

I'm assuming these are just optional entries if I wanted to limit someone's permissions without changing them on the file server?
Gaming exclusively on Linux since 2017. Windows can suck it!
altair4
Level 20
Level 20
Posts: 11458
Joined: Tue Feb 03, 2009 10:27 am

Re: Automount Samba Shares with CIFS

Post by altair4 »

You are correct. It is optional depending on your use case on the client.

dir_mode / file_mode only apply to how the client "views" the mounted share. It has no affect on the server.
Please add a [SOLVED] at the end of your original subject header if your question has been answered and solved.
Infidelus
Level 4
Level 4
Posts: 273
Joined: Mon Feb 25, 2019 3:14 pm
Location: United Kingdom

Re: Automount Samba Shares with CIFS

Post by Infidelus »

Perfect. Thanks :)
Gaming exclusively on Linux since 2017. Windows can suck it!
Randombloke
Level 1
Level 1
Posts: 1
Joined: Wed Nov 16, 2022 3:26 am

Re: Automount Samba Shares with CIFS

Post by Randombloke »

altair4 wrote: Sat Jul 25, 2020 8:50 am Automount Samba Shares with CIFS:

There are two approaches to remedy this:

[1a] You can set the mountpoint under /media ( or your home directory ) and add noauto,user and end up with links on the side panel of your File Manager that are "actionable" to an ordinary user - click on it and it will mount. Click or right click it again and it will unmount:

Code: Select all

//server/share /media/mountpoint cifs guest,uid=altair,nounix,noauto,user,dir_mode=0777,file_mode=0666 0 0
Remember that it will not mount at boot only when launched by the side link in your file manager.

You then have to make systemd happy by running the following commands:

Code: Select all

sudo systemctl daemon-reload
sudo systemctl restart remote-fs.target
[1b] adding in x-systemd.after=network-online.target in /etc/fstab to the mount options.

This is not my solution, but helped me solving this exact behaviour. The fstab entry would look something like this:

Code: Select all

//server/share /media/mountpoint cifs cifs _netdev,x-systemd.after=network-online.target,guest,uid=altair,nounix,noauto,user,dir_mode=0777,file_mode=0666 0 0
Now the share is available after boot. This is my source.
User avatar
SMG
Level 25
Level 25
Posts: 31971
Joined: Sun Jul 26, 2020 6:15 pm
Location: USA

Re: Automount Samba Shares with CIFS

Post by SMG »

Moderator note: karloucho's question can now be found here Cannot see automount Samba Shares.

As indicated in the red/pink box at the top of this forum, "Don't add support questions to tutorials please; start your own topic..."

ibm450's post has been moved to their topic.
Image
A woman typing on a laptop with LM20.3 Cinnamon.
Post Reply

Return to “Tutorials”