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.
For the sake of simplicity, I will assume you have a Synology NAS and name things accordingly. But this will work regardless of what NAS you have, just change folder names for whatever NAS and naming convention you have.
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.
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 for our NAS, such that our computer can use it to esthablish a connection. Use the same credentials that you would type in when connecting to your NAS via a file browser.
Let's create the file /etc/smb-credentials by typing the following command:
$ sudo edit /etc/smb-credentials
and enter the login credentials such that the file looks like this:
username=peter
password=supersecurepassword123
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 our NAS.
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> Use your 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.Now repeat this for all the shares you want to mount.
Let's assume the following:
/synology/home and /synology/documents192.168.1.23/etc/smb-credentialspeterFor these assumptions, your file should look roughly 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
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:
//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
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.