How to Mount Network Share on Ubuntu, Raspberry, Linux

How to connect a linux machine to a NAS enabled router’s hard drive.

Set up the NAS enabled router:

Connect the hard drive to the router then access the admin interface and make the necessary changes. It’s a manufacturer dependent setting but generally it means flipping a switch.

Asus AC1200+ allows us to set up various users with various folder privileges. It’s always recommended to protect our networks by all means possible so set up a user with password and grant the necessary folders for read and write access.

Connect to the samba share with file explorer:

Add

client use spnego = no

to the [global] section of /etc/samba/smb.conf

Make it available for auto-mount:

This document describes how to mount CIFS shares permanently. The shares might be hosted on a Windows computer/server, or on a Linux/UNIX server running Samba. This document also applies to SMBFS shares, which are similar to CIFS but are deprecated and should be avoided if possible (link).

(This document does not describe how to host the shares yourself, only how to access shares that are hosted somewhere else. For hosting shares, use Samba.)

Prerequisites

We’re assuming that:

Network connections have been configured properly.

Your local (Ubuntu) username is ubuntuusername.

Share username on Windows computer is msusername.

Share password on Windows computer is mspassword.

The Windows computer’s name is servername (this can be either an IP address or an assigned name).

The name of the share is sharename.

You want to mount the share in /media/windowsshare.

CIFS installation

sudo apt-get install cifs-utils

On older systems:

sudo apt-get install smbfs

Mounting unprotected (guest) network folders

First, let’s create the mount directory. You will need a separate directory for each mount.

sudo mkdir /media/windowsshare

Then edit your /etc/fstab file (with root privileges) to add this line:

//servername/sharename /media/windowsshare cifs guest,uid=1000,iocharset=utf8 0 0

Where

guest indicates you don’t need a password to access the share,

uid=1000 makes the Linux user specified by the id the owner of the mounted share, allowing them to rename files,

iocharset=utf8 allows access to files with names in non-English languages. This doesn’t work with shares of devices like the Buffalo Tera Station, or Windows machines that export their shares using ISO8895-15.

If there is any space in the server path, you need to replace it by \040, for example //servername/My\040Documents

After you add the entry to /etc/fstab type:

sudo mount -a

This will (re)mount all entries listed in /etc/fstab.

Mount password protected network folders

The quickest way to auto-mounting a password-protected share is to edit /etc/fstab (with root privileges), to add this line:

//servername/sharename /media/windowsshare cifs username=msusername,password=mspassword,iocharset=utf8,sec=ntlm 0 0

This is not a good idea however: /etc/fstab is readable by everyone and so is your Windows password in it. The way around this is to use a credentials file. This is a file that contains just the username and password.

Using a text editor, create a file for your remote servers logon credential:

gedit ~/.smbcredentials

Enter your Windows username and password in the file:

username=msusername
password=mspassword

Save the file, exit the editor.

Change the permissions of the file to prevent unwanted access to your credentials:

chmod 600 ~/.smbcredentials

Then edit your /etc/fstab file (with root privileges) to add this line (replacing the insecure line in the example above, if you added it):

//servername/sharename /media/windowsshare cifs credentials=/home/ubuntuusername/.smbcredentials,iocharset=utf8,sec=ntlm 0 0

Save the file, exit the editor.

Finally, test the fstab entry by issuing:

sudo mount -a

If there are no errors, you should test how it works after a reboot. Your remote share should mount automatically.

 

Troubleshooting auto mount:

It seems there is a problem with newest Kernel versions and samba.

I’ve managed to solve this by adding vers=2.0 at mount commands or at the end of each fstab line.

Example of fstab line:

#automount asus

//router.asus.com/sharedfolder/ /media/asusshare cifs credentials=/home/pcUsername/.smbcredentials,vers=1.0

#

Raspberry WiFi auto-mount problem:

Solution 1:

Add sleep and mount -a to the rc.local file

sudo nano /etc/rc.local

to sleep 20 seconds (by calling sleep 20) and then call mount -a. This way, even though the network is NOT connected yet when the system first reads the fstab file, so the mount fails then, we force the system to wait 20 seconds here (giving the network time to connect) then I force it to call mount -a again to mount all drives in the fstab file.

Example:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
#GS notes: a *minimum* of sleep 10 is required for the mount below to work on the Pi 3; it failed with sleep 5, but worked with sleep 10, sleep 15, and sleep 30
sleep 20
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
  mount -a #GS: mount all drives in /etc/fstab
fi

exit 0
Solution 2:

Set raspberry to wait for Network at boot:

sudo raspi-config

Select Boot Options > Wait for network at Boot

Sources:

https://askubuntu.com/questions/763945/cant-connect-to-smb-share-after-upgrade-to-ubuntu-gnome-16-04

https://askubuntu.com/questions/399643/cifs-mount-through-fstab-not-mounting-at-boot

https://askubuntu.com/questions/758860/samba-share-user-password-error-after-update/763436

https://wiki.ubuntu.com/MountWindowsSharesPermanently