0

This question has been answered many times. I have been trying to help a friend do this on his network, but either I haven't conveyed the right information or the links I have sent him do not adequately explain how to do tit incrementally and with tests along the way to ensure everything is ok and coming along smoothly. So, I ran into a situation recently, building my own NAS, where I need to mount a SMB share to my ubuntu laptop to easily store my information safely on my NAS. I just figured this out via trial and error and I wanted to explain how I did it to help my friend and other nerds out there! :)

Kyle H
  • 111
  • 5

1 Answers1

1

Make sure you have configured a basic SMB share with a network username and password on your remote machine. Do this using the UI of your NAS, Samba, etc. and ensure the firewall is configured to allow incoming connections. For me, Rockstor's UI took care of all this for me automatically when I created my share. Verify from the local computer you wish to mount the remote share on, that you can ping the IP/FQDN of the remote device.

ping 192.168.1.249

On your local computer, install smbclient to test access to your mount, samba, and cifs-utils to enable cifs mounting on your pc using your distro's package manager. You may have to google or search using your package manager to find what packages include these commands for you to install. Usually they are similarly named.

Lets first try to list shares on the remote pc from the local pc. IPorFQDN is the IP address or Fully Qualified Domain Name of your share device. username and password are the username and password you set up on the remote share

smbclient -L //IPorFQDN/ -U username%password

If that successfully prints out a list of information, including available shares on the remote device, then you have successfully logged in and validated yourself. This proves the mount is possible. You may want to remove your ~/.bash_history file, or just edit it with vi, after you next log out & in again because the password you entered on the cli will be in bash history. You can do this in one swoop at the end if you like.

Now, lets test the mount of the SMB mount to the local computer. Create a directory to mount the share to, dirsharename is anything you would like the foldername to be.

sudo mkdir /media/dirsharename

Run the following command, substituting your IP or FQDN for IPorFQDN, sharename should be your sharename as on the remote share, dirsharename should be the folder you just created above, then the options after the -o should be your username and password for the remote share. This is another case where you may want to log in and out again to clear your bash history of the password. You can do it in one swoop at the end.

sudo mount.cifs //IPorFQDN/sharename /media/dirsharename -o user=username,pass=password

This will give you error messages, or nothing if it is successful. If there's an error, fix it, or reply to this post if you need assistance. You can verify the mount was successful by running mount, or using ls to show the directory contents.

mount

//IPorFQDN/sharename on /media/dirsharename type cifs (rw,nosuid,nodev,noexec,relatime,vers=default,cache=strict,username=username,domain=,uid=0,noforceuid,gid=0,noforcegid,addr=IPorFQDN,file_mode=0755,dir_mode=0755,soft,nounix,serverino,mapposix,rsize=1048576,wsize=1048576,echo_interval=60,actimeo=1,user)

ls /media/dirsharename

Now to get it automatically mounting in the /etc/fstab file! This will be a single line entry at the bottom of the /etc/fstab file you will need to add. First, you need to add a credentials file in a secure area of your filesystem so the credentials are known automatically on boot and mount time. What I did was to make the .smbcredentials file in the /media/nas/auth directory and I set the permissions and owners:groups as I show below with the tree command... please ignore the asteriks :) The mount will be run as root during boot (or mount -a!), which with this permission structure, only the superuser will be able to see the .smbcredentials file. Another bonus is that with your .smbcredentials file located where the mount will mount, when the share is mounted, you wont be able to see the .smbcredentials file anymore... Im seeing this as just another security measure for the hell of it :)

Secure directory structure for .smbcredentials username@local:~$ sudo tree -alpug /media/
/media/
└── [drwxrwx--- root sambashare] nas
*****└── [drwx------ root sambashare] auth
**********└── [-rw------- root root ] .smbcredentials

3 directories, 1 file

This is the contents of the .smbcredentials file containing your username and password for the remote share. While setting this up I did read you can specify the AD domain in this file too after 'domain=' on a third line.

username=username password=password

At this point, you can use this mount command below to test that you can mount the remote share. However, remember to run the mount command to verify you dont already have the share mounted or any other conflicts, and use umount to remove the mount if it is already mounted.

sudo mount.cifs -o cred=/media/nas/auth/.smbcredentials //IPorFQDN/sharename /media/nas/

Finally, we get to the /etc/fstab single line entry at the end of the file. Edit this file with your favorite editor and add a line like the one below. It starts out with the remote share IP/FQDN and sharename, the dir to mount it to, the filesystem type (cifs/SMB are basically names for the same thing), the options after -o in the command above like the credentials file, user so users can mount this, default mount options, and the numbers are 0 to signify to not dump the filesystem, and 2 to signify the order with which to check the filesystems on boot, root is 1 else it should be 2.

//IPorFQDN/sharename /media/nas cifs cred=/media/nas/auth/.smbcredentials,user,defaults,rw 0 2


You should have a remote filesystem that will be mounted automatically when you boot your pc. However, there is one more test you can use to verify without a reboot that this will successfully mount using the entry in /etc/fstab without having to reboot. This command tells the pc to automatically mount everything in /etc/fstab that is to be automatically mounted. Of course, make sure that it is not already mounted using the 'mount' command to show whats mounted and 'umount' to un-mount the share if it is mounted.

sudo mount -a

Reboot! If you want to check again :)

Possible addons later on for this tutorial... I may get to are integrating this into systemd for more flexibility and to use as a systemd mount unit file. This would enable you to mount this share whenever you connected to a certain network for example... That sounds fun to do so I may just go off and research this now that I am done writing this writeup!

Please comment with updates, questions, etc. Thank you for reading and have a great day! :)

Kyle H
  • 111
  • 5
  • found that I need to troubleshoot writing to said shares. i will investigate and add to tutorial later :) ty for your patience! – Kyle H Mar 18 '20 at 22:29