HowTo: RaspberryPi – Sharing folders with SMB and mount in Windows as a network drive

If you work with the RaspberryPi you will sooner or later encounter the problem of how to transfer files to the RasPi or copy them from the RasPi.

There are different ways to do that. An easy way is the transfer via FTP or SFTP.

When communicating with your RasPi over the Internet, one of them is certainly your first choice. However, if you work in the home network, it is much more comfortable to exchange the files directly via Windows Explorer.

To make this possible you have to set up a share on the RasPi, which you can then integrate under Windows as a network drive.

How to do that and what you should pay attention to in the following article.

Hints for our lovely english readers: Basically, many of the articles on Nerdiy.de are translations from the original german articles. Therefore, it may happen here and there that some illustrations are not available in english and that some translations are weird/strange/full of mistakes or generally totaly wrong. So if you find some obvious (or also not obvious) mistakes don't hesitate to leave us a hint about that in the comment section. 
Also please don't get confused, that instead of a "dot" often a "comma" is used as decimal separator. 🙂


Safety instructions

I know the following hints are always a bit annoying and seem unnecessary. But unfortunately, many people who knew it "better" from carelessness lost their eyes, fingers or other things or hurt themselves. In comparison, a loss of data is almost not worth mentioning, but even these can be really annoying. Therefore, please take five minutes to read the safety instructions. Even the coolest project is worth no injury or other annoyance. https://www.nerdiy.de/en/sicherheitshinweise/

Affiliate links / advertising links

The links to online shops listed here are so-called affiliate links. If you click on such an affiliate link and shop via this link, Nerdiy.de receives a commission from the online shop or provider concerned. The price doesn't change for you. If you do your purchases via these links, you will support Nerdiy.de in being able to offer further useful projects in the future. 🙂


Requirements

Helpful Articles:
Before you start with this article, you should have prepared the RaspberryPi so far that it can be reached via the network and controlled by SSH.

The following three articles describe what to do to prepare the RaspberryPi:
RaspberryPi – Setting up for Nerdiys!
RaspberryPi – The First Configuration!
RaspberryPi – Control the RaspberryPi via SSH
Basics of navigation in Windows Explorer

Required tools:
-none-

Required material:

In the following list you will find all the parts you need to implement this article.


Log in via SSH on the RaspberryPi

To get started, you first need to log in to RasPi with SSH on Putty. How to do it is described in the article

After entering your username and password you can start to enter the first commands.

Update package management

The package management in Linux is a "central point" over which various software packages can be installed. For this to work reliably, the lists and sources of package management should be updated before installing any new packages.

To start the package management update you have to enter the following command.
sudo apt-get update && sudo apt-get upgrade
Depending on how long your last update of the package management is, this process can take some time. First, the lists are updated in which the individual repositories are referenced.
Then the packages themselves are updated. As this additional memory is occupied, you will again asked for your consent. You have to confirm this with a "J"(in German) or a "Y" and "Enter".
When the update is complete, you will see a small summary of the duration and scope of the update.

Install the required programs or packages

To be able to share a folder, you need of course a corresponding program called Linux package. The required package is called “Samba”. You can install this “samba” with the following command:

sudo apt-get install samba
During the installation of the updates and the following packages, you may be asked if you agree that the modules to be installed will take up additional space.

You confirm these demands with a “j”(or “y”) and enter. The installation will then continue.


Create the folder to be shared

In order for you to be able to share access to a folder, you must first create a corresponding folder in your home directory.

This is done with the following command:

mkdir ~/FreigabeOrdner
This creates a folder in your home directory with the name “Share Folder”. The “~/” in front of the folder name indicates that this folder should be created in your home directory.

In the screenshot you can see the home directory before and after the folder “share folder” was created.
In the view of the home directory after creating the folder, the (red underlined) “Release Folder” will be available.


Customize the Samba configuration file

Now we have to tell “Samba” where the folder to be shared is and under what conditions we would like to release it. This can all be set in the Samba configuration file. This is called smb.conf. To edit them you need to execute the following command:

sudo nano /etc/samba/smb.conf

After opening the configuration file, you use the arrow keys to maneuver the cursor to the end of the file (ie all the way down). There you then insert the following configuration text:

[PiFreigabe]
comment=Raspi 
Sharepath=/home/pi/FreigabeOrdner
browseable=Yes
writeable=Yes
only guest=No
create mask=0740
directory mask=0750
public=no

It is easiest if you mark the text, copy it and then paste it in the text file with a right-click in the relevant place.

If you have chosen a different folder name or location for your shared folder, you will need to change the corresponding part under “path”.

These configuration settings will allow you to have read and write permissions in the folder, to be able to browse it, and to allow only users who have previously logged in with username and password to access it.

In order to save the changes you have to press CTRL + X on the keyboard and confirm the request if you want to save before closing with a “j” (or “y”=yes) and ENTER.

This saved your recent changes to the configuration file.

Samba is automatically restarted and the new configuration is taken over.


Set up a user account

Since you have configured the sharing by the information in the configuration file so that only users with username and password can access them, we now have to create a username with a suitable password.

This user name is the same as the currently used Linux user. The password may be different from this.

For example, to create the user “pi” you need to execute the following command:

sudo smbpasswd -a pi
After entering, you will be prompted twice to enter the same password. The double input is to prevent you from typing in the simple input and thus a wrong, you unknown password would be saved.

Set up network drive under Windows

With the configuration under Linux you are now finished.

Now you have to configure the shared folder in Windows as a network drive. You can then finally access the shared folder as if it were a local drive (that is, connected directly to your computer).

To configure this network drive, open the “Computer”. So the window in which you also plugged USB sticks, CD/DVD drives and hard drives are displayed.

To be able to see the menu entry that you can connect to a network drive, you must first show the menu bar (if it is not already displayed). To do this, click on the small (red circled) arrow.
Now you click in the top bar on “Netzlauferk connect”.
… and then in the opening window on “Browse”. There you should now be displayed all the computers that are present in your network.
The Raspberry Pi used in this example is called “MAGICMIRROR”. You have set the name of your RasPi and should be found here. To set up the network drive you have to click on the name of your RasPi.
This opens a window in which you should enter the username and password that entitles you to access this computer. You have previously created or specified this user/password combination.

But you also have to tell Windows that it should use the user account that was created on the RaspberryPi.

Attention now it gets a bit complicated:
This is done by setting the user name, separate from a backslash (ie, the slash on the sharp s key on a germany keyboard-layout), to the user’s domain. The domain of the user in this case is your RasPi.

Long story short:
So you do not just type in “pi” but

„nameOfYourRaspi\pi“

on.
In my example, this would then be

„magicmirror\pi“

 

The password is entered as normal in the password line.
Then you click on “OK”.

… and find yourself in this window again. Here you select now your shared folder, put a check mark at “Restore connection at registration” and click on “Ok” and “Finish”.

The window then closes and a new window opens shortly thereafter.

This is the connected network drive.

This folder now shows you the contents of the shared folder on the RasPi. All files that you now copied into this drive are copied directly to the RasPi.


You now know how to share a folder on the RasPi and connect it as a network drive on a Windows system.

I hope everything worked as described. If not or you have any other questions or suggestions, please let me know in the comments. Also, ideas for new projects are always welcome. 🙂

Fab

P.S. Many of these projects - especially the hardware projects - cost a lot of time and money. Of course I do this because I enjoy it, but if you appreciate it that I share these information with you, I would be happy about a small donation to the coffee box. 🙂

Buy Me a Coffee at ko-fi.com

Leave a Reply

Your email address will not be published. Required fields are marked *