Files
Linux-Docs/ConnectingSMBShares.md
T
2026-11-08 17:24:26 -05:00

160 lines
5.8 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
tags:
- Arch
- Bazzite
---
## Important Notes
1. Make sure you can connect to the share using hostname.local. see [Local Host Names](LocalHostNames.md)
2. **If changing location of the scripts** you need to update the script name to match the path exactly
3. Replace `<server-name>` everywhere below with the name of your server
4. If running [Bazzite](Bazzite.md),  becuase of how fedora immutable distros are setup you need to use `/var/mnt`. the actual path is `/var/mnt` and `/mnt` is just linked to it.
## Create systemd mount file
Create a file called `.<server-name>-creds` in your home dir and set it so it's only readable by you like before.
Put
```
username=<username>
password=<password>
```
in the file
create the file `/etc/systemd/system/mnt-<servername>.mount`
(this will be mounted to `/mnt/<server-name>`)
In the file put the below:
```
[Unit]
Description=Mount SMB share <server-name>
# We require network connection before proceeding.
Requires=network-online.target
After=network-online.target systemd-resolved.service
Wants=network-online.target systemd-resolved.service
[Mount]
"What" is what will be mounted. ie our NAS SMB share
What=//<server-name>.local/<shared-folder>
# "Where" is where it will be mounted in the filesystem
Where=/mnt/<server-name>
Type=cifs
# Let the mounted filesystem be read/write-able
# Let the mounted filesystem be owned by the 'tess' user/group
# Use the specified credential file for the connection
Options=rw,uid=<uid>,gid=<gid>,nofail,credentials=/home/**username**/.<server-name>-creds
# Lets quit trying after 30 seconds
TimeoutSec=30
[Install]
WantedBy=multi-user.target
```
Also replace `uid=<uid>,gid=<gid>` with your user and group id respectively.
They will probably both be 1000 (so `uid=1000,gid=1000`), but you can check with `id` or `id **username**`
This is telling systemd how to mount your drive.
## Create systemd automount file
Now create the automount file `/etc/systemd/system/mnt-<server-name>.automount`
In that file put:
```
[Unit]
Description=Automount SMB share <server-name>
[Automount]
# Adjust mount location to match yours.
Where=/mnt/<server-name>
[Install]
WantedBy=multi-user.target`
```
## Test and Apply configuration
Now we want to make the directory it will be mounting to
`sudo mkdir /mnt/<server-name>`
Then we reload systemd
`sudo systemctl daemon-reload`
First we start the .mount, and check for errors
```
sudo systemctl start mnt-<server-name>.mount
systemctl status mnt-<server-name>.mount
```
Then if there's no issues we can stop the normal mount and enable automount
```
sudo systemctl stop mnt-<server-name>.mount
sudo systemctl enable --now mnt-<server-name>.automount
```
(`enable` means we are telling it to run on boot, and `--now` it telling it to also start it now)
We could just enable the normal mount and have it mount on boot, but automount will mean it will only connect to the samba drive when we try to access it.
Now we should see it show up under `/mnt/<server-name>`
And if we reboot it should show up still.
## Additional Notes
For reference Aiden based it off of  [This](https://www.reddit.com/r/SteamDeck/comments/ymjnjy/mounting_smb_shares_with_systemd/) this post, just modified to this situation and me
# Automounting using AutoFS and CIFS
You will need the `autofs` and `cifs` packages, and the autofs service will need to be enabled but not running
Make a credentials file using previous steps. For this example, my credentials are in `/home/<username>/.servercreds/<servername>-creds`
AutoFS uses the file `/etc/autofs/auto.master` as its directory master. We can avoid editing this file by putting our directory map files in `/etc/autofs/auto.master.d/`.
You will need 2 files: a directory map and a file map.
Create the directory map
`/etc/autofs/auto.master.d/<server-name>.autofs`
and the key file
`/etc/autofs/auto.<server-name>`
The directory map can contain multiple maps, however we are going to make one file per map.
## Indirect Mapping
Indirect mapping allows the user to specify the mount point in the directory map and the subfolder in the key file
In the directory map we want to list the mount point for thee smb share and it's key file
`/mnt /etc/autofs/auto.<server-name>`
This tells autofs to mount the share in a subdirectory of /mnt
In the key file we need to specify the name of the subdirectory, our config options, and thee smb server location
`<server-name> -fstype=cfs,credentials=/home/**username**/.servercreds/<server-name>-creds,uid=<uid>,gid=<gid> :<server-location>`
as with systemd, replace `uid=<uid>,gid=<gid>` with your user and group id respectively.
They will probably both be 1000 (so `uid=1000,gid=1000`), but you can check with `id` or `id **username**`
If using an IP address, it needs to be put before `:<server-location`: `<ip>:<server-location>`
## Direct Mapping
Direct mapping requires the user to specify the entire mounting path in the key file
In the directory map we want to specify we replace the mount point with `/-` to indicate that we are using direct mounting
`/- /etc/autofs/auto.<server-name>`
This tells autofs to mount the share using the path found in the key file
In the key file we now need to the entire mounting path rather than just the subdirectoory
`/mnt/<server-name> -<options> :<server-location>`
## Testing configuration
To test the configuration, without the `autofs` service running, launch `automount` using
`# automount -f -v`
If you configured everytning properly, you will see
`mounted /mnt/<server-name>`
Exit `automount` and restart the `autofs` service
## Additional Notes
Tess based this guide on
https://man.voidlinux.org/auto.master
https://man.voidlinux.org/autofs
/etc/autofs/auto.master
https://computingforgeeks.com/how-to-automatically-mount-nfs-file-system-with-autofs/