Somehow it's not trivial to find a simple and straightforward guide to how to best set up a network share (e.g. a folder on your NAS) in Linux, which is why I wrote this.
The following fstab preset will likely be what you are looking for. If you don't know what this means, skip this part!
//<nas-ip-or-hostname>/<share> /nas/<share> cifs x-systemd.automount,noauto,user,uid=<user>,gid=<user>,credentials=/etc/cifs-credentials 0 0
Be sure to:
<user> with your local username/etc/cifs-credentials (with 1. line username=... and 2. line password=...)chmod 600 to that fileremote-fs.targetdone.
Okay, so this guide assumes some basic knowledge of how your NAS is set up and how to work with the linux terminal.
If you are a novice, here are some notes so you won't get confused:
The edit command
This command opens a file (like edit /path/to/file) on your system with your default editor. If you don't know what that means I suggest replacing edit with nano to be sure that you will have the easiest experience.
Nano is a simple text-editor where you navigate with your arrow-keys, you save with Ctrl+O and you exit with Ctrl+X (+ Enter to confirm). Pay attention to the bottom of your terminal as it will ask you to confirm certain actions using a prompt displayed there.
Know the difference between your local user and the user of your NAS.
With local user, I mean the literal username that you have on your system. If you open a terminal, you will likely see something like peter@debian ~ $ In this case peter is your username and it's case-sensitive, so even when the system may show you prompts like "Hello Peter", you will need to write your local user always exactly as you see it in your terminal.
Your NAS user on the other hand is the login credential that you use when connecting to your NAS. So maybe it's pete or family. This will also be important, but don't confuse the two.
Know how to connect to your NAS
Whenever I mention the IP or address of your NAS, it is not important if you write out the literal IP like for example 192.168.1.23 or the hostname like Synology.local.
Make sure that you really know the actual IP or hostname of your NAS.
If your hostname doesn't work, try adding/removing the .local postfix or fallback to the IP-address.
Know what shares and folders are
Do you know what's the difference between a share on your NAS and the folders inside a share? If not, go research it.
To put it simply, a share is usually the top-most "folder" you see when you log into your NAS from a file browser. They are a bit more fancy than a normal folder. If you have multiple shares, you need multiple mounts. So make sure you know what "folders" on your NAS you need accessible on your system.
The $ is silent
Commands are usually writenn as $ command --with arguments. The $ just means that you execute it as your user (as opposed to # with means root in case of commands, but can also stand for a comment instead of a command. this can be confusing at first, so I will not use that and stick to $ here).
So, we want to make our NAS share available on our system, but how?
Firstly, we need a local folder. This folder will then be used to mount the NAS-folder, making it look like it's on your system.
For the sake of simplicity, I will assume you have a Synology NAS and name things appropriatly. This will work regardless of what NAS you have. Please change folder names as you please.
Let's say you have the following shares on your NAS:
\\Synology
├── home
├── documents
└── movies
To connect to all these folders, let's create the following local folder structure on the PC:
/synology
├── home
├── documents
└── movies
First, create the parent-folder
$ sudo mkdir /synology
Then create one sub-folder for every share you want, in our case:
$ sudo mkdir /synology/home
$ sudo mkdir /synology/documents
$ sudo mkdir /synology/movies
Next, we will need to store our login credentials to our NAS so our system can use it to esthablish a connection.
Let's create the file /etc/smb-credentials by typing the following command: $ sudo edit /etc/smb-credentials and enter the NAS-login credentials such that the file looks like this:
username=...
password=...
(where ... is replaced by your username/password in clear text)
IMPORTANT: As this file includes clear-text login credentials, we need to protect it. Enter the following command to lock this file down so only our system and administrators can access it:
$ sudo chmod 600 /etc/smb-credentials
If you intend to connect multiple remotes with different logins, you can simply create more files:
# first file
$ sudo edit /etc/synology-smb-credentials
$ sudo chmod 600 /etc/synology-smb-credentials
# second file
$ sudo edit /etc/truenas-smb-credentials
$ sudo chmod 600 /etc/truenas-smb-credentials
Lastly, let's tell our system to use the local folders and credentials we just set up to mount the appropriate shares from your NAS.
Next, open a graphical text editor of your choice and copy the following line into the editor:
//<ip>/<share> /synology/<share> cifs x-systemd.automount,noauto,user,uid=<user>,gid=<user>,credentials=/etc/smb-credentials 0 0
In your graphical editor, replace/fill-in the following sections:
//<ip>/<share>: This should point to the IP/hostname of your NAS and the share you want to use/synology/<share> Replace <share> with the respective local folder you created in step 1uid=<user>: the part of <user> should be replaced by your local username of the user on your PC.gid=<user>: the same, replace <user> with your local username.If you don't have a synology and/or created a different credential file, be sure the replace the file path in the command and the local folder to the exact path you previously defined.
Now repeat this for all the shares you want to mount.
Yes, this may be overwhelming, so here is an example:
Let's say you have a Synology NAS, and:
/synology/...192.168.1.23/etc/smb-credentialspeterFor the three example shares we previously used, your text editor should look like this:
//192.168.1.23/home /synology/home cifs x-systemd.automount,noauto,user,uid=peter,gid=peter,credentials=/etc/smb-credentials 0 0
//192.168.1.23/documents /synology/documents cifs x-systemd.automount,noauto,user,uid=peter,gid=peter,credentials=/etc/smb-credentials 0 0
//192.168.1.23/movies /synology/movies cifs x-systemd.automount,noauto,user,uid=peter,gid=peter,credentials=/etc/smb-credentials 0 0
Another example. This time your NAS is a TrueNAS, you only refernce it by it's hostname and your respective local folders and credential file are named differently and you have different folders:
//TRUENAS/home /nas/home cifs x-systemd.automount,noauto,user,uid=peter,gid=peter,credentials=/etc/truenas-smb-credentials 0 0
//TRUENAS/public /nas/public cifs x-systemd.automount,noauto,user,uid=peter,gid=peter,credentials=/etc/truenas-smb-credentials 0 0
I hope this makes it udnerstandable how you should edit this preset.
We will now edit the existing file /etc/fstab. Be sure to be careful and don't delete things that are already in there. Errors can result in your system unable to start at next boot.
After you enter the file, navigate to the end and make some new lines to put some space between the configuration of your system and our addition.
Open the file in an editor with $ sudo edit /etc/fstab and go into a new, empty line at the bottom of the file. For safety, make some new empty lines to put some space between the configuration of your system and what we will add now.
Next, copy what you just prepared in your graphical text editor and paste it into the file, roughly like this:
# This is stuff that is already here and was configures by your system.
# DO NOT CHANGE ANY OF THIS, EVEN IF LOOKS WAY DIFFERENT FROM THIS
dev/system/os / btrfs defaults 0 0
UUID=4076-E522 /boot/efi vfat utf8 0 2
(...)
# Let's put some space for safety between the system stuff and our changes
# Now come our changes:
//192.168.1.23/home /synology/home cifs x-systemd.automount,noauto,user,uid=peter,gid=peter,credentials=/etc/smb-credentials 0 0
//192.168.1.23/documents /synology/documents cifs x-systemd.automount,noauto,user,uid=peter,gid=peter,credentials=/etc/smb-credentials 0 0
//192.168.1.23/movies /synology/movies cifs x-systemd.automount,noauto,user,uid=peter,gid=peter,credentials=/etc/smb-credentials 0 0
Then simply save and exit. If you are using nano it's Strg+X (and then Y to save and Enter to confirm).
You could just restart your PC. But if you don't want to, just enter these commands to load and activate our changes:
$ sudo systemctl daemon-reload
$ sudo systemctl restart remote-fs.target
If everything went fine, you should now find the NAS shares you set-up in the respective folders on your system, so /synology/home, /synology/documents and so on. Again, this may differ if you used different folder names.
You should also find your NAS folders shown in your graphical file managers directly. Most of them detect these mounts and display them somewhere on the left side of the window.