Windows Networking on the Raspberry Pi

The goal of this article is to configure the Raspberry Pi so that it can integrate into an existing Windows Networking environment without needing to make changes to any of the other, perhaps numerous, Windows PCs.  I know that there are numerous other ways to achieve networking like this though so please don’t feel the need to tell me about them in the comments below.  Please note that I will be doing a version of this article for the Apple Macintosh too.

A good way to do some programming on the Pi is to access the Pi file system over the network.  The advantage this gives you is that you can speedily browse the web for snippets of code under Windows and then copy / paste them directly into a file on the Pi.  This method works really well for me and it also allows me to easily back up my source code onto my main Windows machine.  Or you might just want to turn your Pi into a NAS device!

So this guide is intended for someone who is familiar with Windows Networking already.  If you have never used it before there are plenty of good guides out there which you can read.  Perhaps try and network two normal Windows PCs before you try this.

So we’re going to use Raspbian for this, first off make sure you download the most recent Raspbian image and burn it onto an SD card.

Before we get going I just want to mention a Windows program that you should familiarise yourself with.  Putty (above) is a terminal client program that will basically allow you to have the Raspberry Pi command prompt (the Shell) inside a desktop window (see below).  Raspbian comes with an SSH (Secure Shell) server that makes this possible.

Don’t confuse this with a full remote desktop program though, it just gives you the command prompt.   If you were to try and type startx inside Putty it wouldn’t work.  Putty only supports text based ouput.  However you can do things like install programs, move files around, reboot the Pi etc.  Basically everything you would normally be able to do if you were using the normal Linux command prompt on the Pi itself.

Where am I going with this?  You can copy and paste Linux commands directly into Putty from a web site such as this without having to manually type them out.  It will save you having to carefully check that you’ve typed the commands in correctly and therefore bags of time.

You can download Putty from here (putty.exe is the one you want).  It doesn’t have an installer package, it’s just a standalone exe file.  When you run it you’ll see the configuration screen below.

Enter the IP address of your Pi into the host name field and click the Open button.  If you don’t know the IP address just type ifconfig at the Pi command prompt and the address is on the second line just after inet addr.  You can ignore the security warning (below) and click yes (you’ll only see that warning once).  Note the Save and Load buttons, you need to provide a name under Saved Sessions to use those.

You’ll then have the usual login prompt, login with the same username and password as you would use on the Pi itself.  A paste in Putty is done by a right click of the mouse.  Copy and paste the following command into Putty and press enter.

sudo apt-get update

This just updates the package list so it’s ready for when we want to start installing new software, which is next!

We’re going to install a utility called Samba, yes like the Brazilian dancing but actually nothing like it.  It’s a suite of tools that allow Linux based machines to interact with Windows Networks and it’s also available for Raspbian.

Here is the next command to paste into Putty;

sudo apt-get install samba samba-common samba-common-bin winbind

A message saying that about 50 MB of disk space is required will show along with a prompt asking if you want to continue.  Just type y and press enter and the installation will begin, it may take a few minutes to finish.  If you’re wondering what winbind is; this is a small background process that announces your Pi by name to Windows PCs on the network, allowing them to use the actual host name instead of the IP address.

If you go to a Windows PC now, bring up a command prompt and enter ping raspberrypi (or whatever host name your Pi has) you’ll see that you get a reply.  You can also use the same host name in Putty from now on.  We’re not quite finished yet though.

We now need to edit the configuration file for Samba to specify how we want things to work, its a bit laborious but we only need to do it once.  I recommend you also do this in Putty so you can continue to copy and paste.  So we’re going to change to the Samba config folder, make a backup of the default config file and then edit the file using a program called nano.  Enter the following commands one by one.

cd /etc/samba

sudo cp smb.conf smb.backup

sudo nano smb.conf

You will now see the top of the config file, it should look something like this;

#
# Sample configuration file for the Samba suite for Debian GNU/Linux.
#
#
# This is the main Samba configuration file. You should read the
# smb.conf(5) manual page in order to understand the options listed
# here. Samba has a huge number of configurable options most of which
# are not shown in this example

A hash symbol indicates a comment in this file, so any line that starts with one is either a comment or a disabled setting.  Scroll down until you find [global].  You should see a line saying workgroup = WORKGROUP.  Change this to match the workgroup or domain of your Windows PCs, this will make the Pi visible when you browse the network in Windows.  Also add a setting called realm and one called netbios name just below and set them to the host name of the Pi.

   workgroup = WORKGROUP
   realm = raspberrypi
   netbios name = raspberrypi

Press Ctrl – O then enter to save.  Do this periodically so you don’t lose any changes.  Don’t concern yourself with what all the various settings mean, you can read up on this if you really want to know.  The comments often describe them quite well though.

A bit further down you should find this line, remove the semi colon.

;   name resolve order = lmhosts host wins bcast
   name resolve order = lmhosts host wins bcast

Next scroll down until you find;

####### Authentication #######

A bit below that you should see this line, remove the hash.

#   security = user
   security = user

So this is turning on user level security.  It will allow you to access the Pi from a Windows PC using your Raspbian login (for example ; pi / raspberry).  Save your changes.

So we now need to set up some Samba shares, these are equivalent to Windows shares and allow you to see the files from another computer.  Let’s make two shares.  One which is the home directory of the Raspbian user (cd ~ at the command prompt) and one NAS type share that all users can access.

Scroll right, right down until you find this.

#======================= Share Definitions =======================

Just under it you’ll see [homes].  Modify the settings so that they match what is below, copy and paste if desired.

[homes]
   comment = Home Directories
   browseable = no
   valid users = @users
   writable = yes
   create mask = 0700
   directory mask = 0700

A bit further below you’ll see a line saying read only, change the yes to no.

   read only = yes
   read only = no

Press Ctrl – X, y then enter to save and exit nano.  We now need to create the physical folder for the NAS-all-users share.  For this example I’m going to make the location of the NAS folder /home/samba-share/nas (which is on the Pi SD card) however you may want to change this to share a USB drive that you’ve connected to the Pi.  In which case the path would be something like /media/usb0.  You’ll need to check out what the drive name is by using the command below.  If you need some help with mounting NTFS drives, have a look here.

ls /media

The name usb0 may not be the same for you.  You can then just substitute /home/samba-share/nas for the correct /media/usb0 path in all the text that follows.

If your nas folder is going to be on the SD card then you should copy and paste the following commands into Putty one by one.  Otherwise you can skip them.

sudo mkdir -p /home/samba-share/nas

sudo chown -R root:users /home/samba-share/nas/

sudo chmod -R ug+rwx,o+rx-w /home/samba-share/nas/

The first line creates the actual folder and the second two set the permissions on it so that all users can have read, write and delete permissions.  We now need to add the settings to smb.conf to make this folder visible as a Samba share, so run nano again.

sudo nano smb.conf

Scroll right to the end of the file and paste in the following settings.

[nas]
   comment = All users
   path = /home/samba-share/nas
   valid users = @users
   force group = users
   create mask = 0660
   directory mask = 0771
   writable = yes
   read only = no

Press Ctrl – X, y then enter to save and exit nano.  The last thing to do is to create a Samba user to match your Raspbian user, then we can reboot and it should be ready to go.  Copy and paste the following command, you will be prompted to enter a password twice, enter the same password that you use to login to Raspbian with (e.g. raspberry).  Note that when you type a password no letters are shown, this is normal Linux/Raspbian behaviour.

sudo smbpasswd -a pi

You should then see Added user pi.  That is the configuration side of things done now, you can go ahead and reboot the pi.  You can send the reboot command from Putty, but you’ll then see a messaging saying that the server closed the connection.  This is obviously because it’s going down for a reboot!

sudo reboot

While the Pi reboots you can go onto your Windows PC and get ready to browse the network.  On Windows XP you should see something like this.

And on Windows Vista or 7 something like this.  If you’re using Windows 8, please have a look at the comments on this article at the end.

Double click the Pi server icon in Windows and you should be prompted for a username and password.  Use the same that you use for Raspbian (e.g. pi / raspberry).  You should then see two shares; nas and pi.  In some cases you may see the nas share only, before being prompted for the login details.  If this happens you’ll be prompted when you try to access the nas share whereupon the pi share should become visible.

Once you’re in, go into the pi share and to test it’s working just right click and go New > Folder.  If it lets you create and name the folder you’re in business.  Maybe try copying a large file over to the Pi and see what kind of transfer speed you get.

You can check that the changes you made from Windows are now visible on the Pi itself.  Either login on the Pi or via Putty and just type ls from the home directory.  Below we can see the “dave” folder I created from Windows.

So that is pretty much it.  If you’re interested in seeing a good way to do some code editing from Windows with the Pi just being used to test and run the program then read on, otherwise you’re done.

So if you already have a preferred code editing tool then you can just use that.  Otherwise I can recommend a nice lightweight program called Notepad++.  This program is tremendously convenient for code editing since it integrates with the Windows shell right click menu, allowing you to right click on any file to edit it.

So from Windows, browse into the pi share.  Right click and go New > Text Document and name the file test.c.  You now need to open this file in your Windows code editor.  If you’re using your own program you may need to re-browse to the pi share from the File > Open menu.  However with Notepad++ you can just right-click the file and select Edit with Notepad++.

Copy and Paste the following code into your code editor.

#include <stdio.h>

main()
{
    printf("Hello World\n");
}

So you should now have something like this.

Save the file from your code editor and we’ll now go onto the Pi to compile and run it.  You can either log onto the Pi itself or use Putty.  After logging in type ls and you should see the test.c file, that you created from Windows, is shown.  Enter the following commands to compile and run the program (gcc is the c compiler program).

gcc test.c -o test.bin

./test.bin

If everything has worked you should see the Hello World message, as shown below.

Every time you make changes to test.c you will need to recompile it using the gcc command above.  Try doing that now with the code below.  This will make your Pi count to a million, press Ctrl – C while running if you want to stop it.

#include <stdio.h>

main()
{
    int i;
    for (i = 1; i <= 1000000; i++)
    {
        printf("%d\n", i);
    }
}

The good thing about Notepad++ is that it provides some nice features that colourise the source code.  Most code editing tools will do this as it makes the code easier for a human to read.  We’ve just been using C, but Notepad++ will also do the same thing for other languages like Python too.

From Windows have a look inside the python_games folder and check out the code in the .py files that are in there.  Good luck.