original post: http://geeks.noeit.com/mount-an-smb-network-drive-on-raspberry-pi/
In this tutorial we will describe how to connect your Raspberry Pi to a network drive and permanently mount it to your system. Even though this article uses a Raspberry Pi as an example to connect to an SMB drive, the steps used can be applied to any Debian based system, such as Ubuntu.
If you have a Raspberry Pi you might have noticed that the storage possibilities are kind of limited unless you have some external storage. Even though you can get SD cards with 64+gb of storage, you probably want more if you have a lot of music and movies that you are streaming through your Pi.
There are several choices when it comes to storage for your Pi, such as network drives, flash drives, or external USB HDDs. Using a network drive you can not only access your files from the Pi, but from any computer connected to your network, which is very convenient.
Before we start, I will assume you already
- have a network drive connected to your LAN, and
- know its LAN IP address
Note: remember to change all text in red to your own values.
In order to mount the drive, you need to have cifs-utils installed on your system. If you are running a newer version of Raspbian or RaspBMC you should already have this installed. You can check whether it is installed or not by running the following command:
dpkg -s cifs-utils
If it is installed, it should output something like this:
Package: cifs-utils Status: install ok installed Priority: optional Section: otherosfs Installed-Size: 189 Maintainer: Debian Samba Maintainers <firstname.lastname@example.org> Architecture: armhf Version: 2:5.5-1 ...
If it says that it’s not installed, you need to install it:
sudo apt-get install cifs-utils
Mounting unprotected (guest) network folders
You might have public folders on your network drive that can be accessed by anyone without having to provide any credentials. These are mounted in the same way as password-protected folders (we will mount these in the next section), but with a few different options.
First, let’s create a directory on the Pi where we want to mount the directory. You will need a separate directory for each network folder that you want to mount. I will create the folders in the /media folder:
sudo mkdir -p /media/networkshare/public
Then edit your /etc/fstab file (with root privileges) and add this line:
//192.168.0.18/publicshare /media/networkshare/public cifs guest,uid=1000,gid=1000,iocharset=utf8 0 0
The first part is the IP of your network drive, and the public folder you want to mount.
The second part is the folder on your local machine where you want to mount the network share.
The third part indicates what type of drive you want to mount (cifs).
The last part is the set of options you can pass, and here’s an explanation of the ones we are using:
- guest is basically telling the network drive that it’s a public share, and you don’t need a password to access it (not to confuse with username),
- uid=1000 makes the Pi user with this id the owner of the mounted share, allowing them to rename files,
- gid=1000 is the same as uid but for the user’s group,
- iocharset=utf8 allows access to files with names in non-English languages.
Note: If there is any space in the server path, you need to replace it by \040, for example //192.168.0.18/My\040Documents
To find the uid and gid for your username, use the following command:
Now that we have added this to our /etc/fstab, we need to (re)mount all entries listed in /etc/fstab:
sudo mount -a
Now you can access your network drive from /media/networkshare/public (or wherever you chose to mount it).
Mount password-protected network folders
Mounting a password-protected share is very similar to mounting a public ones, with some minor changes to the options we pass. Let’s start by making a new folder where we want to mount the password-protected share:
sudo mkdir -p /media/networkshare/protected
Again, open /etc/fstab (with root privileges), and add this line:
//192.168.0.18/protectedshare /media/networkshare/protected cifs username=msusername,password=mspassword,uid=1000,gid=1000,iocharset=utf8 0 0
While this is a perfectly valid way of mounting your protected folder, it’s not very secure. Since /etc/fstab is readable by everyone, so are your credentials in it. So, let’s make it more secure by using a credentials file. This file will contain nothing else but your username and password, but we will make readable only to you. This way, other users on the system won’t be able to see your credentials.
Using a text editor, create a file that will contain the credentials for your protected network share:
Enter your username and password for the protected share in the file:
Save the file.
Change the permissions of the file to make sure only you can read it:
chmod 600 ~/.smbcredentials
Then edit your /etc/fstab file and change the line where we are mounting the protected share to look like this:
//192.168.0.18/protectedhare /media/networkshare/protected cifs credentials=/home/username/.smbcredentials,uid=1000,gid=1000,iocharset=utf8 0 0
Save the file.
Again, we need to (re)mount all entries listed in /etc/fstab:
sudo mount -a
Now you’re all set to access your public and protected folders from your Pi.