Access the Raspberry Pi File System Through Windows
by thomas-bartleet in Circuits > Raspberry Pi
69381 Views, 9 Favorites, 0 Comments
Access the Raspberry Pi File System Through Windows
Have you ever got a Raspberry Pi and gone "I've got to set it up with a monitor, mouse, and keyboard every time!" The frustation it causes to unplug/replug your monitor and keyboard/mouse, just to access the Raspberry Pi is second to none. You resort to either leave everything plugged in, costing you valuable desk-space, or worse, an unfinished project *insert nightmare spooks*! Wouldn't it just be easier to access files and scripts directly from your own machine?
This instructable will guide you to setting up remote file access on your Raspberry Pi. The best part is you will never have to connect anything to the Raspberry Pi! This will work for Windows, and should work for Mac and Linux as well.
Supplies
For this tutorial, you will need:
- A Raspberry Pi (obviously)
- A computer with LAN access (WiFi or ethernet)
- An SD card for your Raspberry Pi (8GB +)
- A power supply for your Raspberry Pi
Installing the Raspberry Pi OS
In this section, you will be installing the operating system (OS) for the Raspberry Pi onto your SD card.
A note on the OS:
If you plan to not use the Raspberry Pi desktop, I would choose the Lite version, as it strips away unnecessary software (desktop, Chromium, Jupyter etc), leaving behind only the command console and the absolutely necessary software. This maximises the memory for storing files, packages, and running custom programs, and doesn't needlessly use up resources running the GUI when you won't be using it. You can find it under Raspberry Pi OS (Other) -> Raspberry Pi OS Lite (32 bit). Otherwise, any other Raspberry Pi OS will do.
- Plug your SD card into your computer. If your computer does not have an SD card reader, you may need to buy one.
- [OPTIONAL] Format your SD card. If your computer fails to format, try a different port.
- Go to the RaspberryPi website and download the RaspberryPi Imager utility. This will help you with installing the OS image onto the SD card. Once the utility has installed, launch it.
- Click on Choose OS. When the dialog box opens, select the OS of your choice.
- Click on Choose SD card. When the dialog box opens, select your SD card.
- Make sure you have selected the right drive that holds your SD card!
- Check again!
- Click on Write. The utility will now download and install the OS onto the SD card.
Setting Up the Raspberry Pi (Part 1) - WiFi and SSH
In this section, you will be enabling SSH and WiFi on your Raspberry Pi. SSH stands for Secure Shell, and is essentially a secure way of remotely accessing command lines. You can find more information about SSH over here.
- Plug your SD card with the OS installed into your computer (if it's not already there).
- Open the SD card directory. It might have a different name, usually boot, but it should still be the Raspberry Pi system.
- Create a new text file and call it ssh. No file extensions, just ssh (remove the .txt extension).
- Create a new text file and call it wpa_supplicant.conf. Make sure you change the .txt extension to .conf. You may be prompted to confirm.
- Now we will set up WiFi access to the Raspberry Pi. If you are connecting the Raspberry Pi to an ethernet cable, you can skip to the next step of the instructable.
- Open your newly created wpa_supplicant.conf file with whatever text editor you want. Copy and paste in it:
country=<YourCountryISO> ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 network={ ssid="<YourWiFiNetwork>" psk="<YourWiFiPassword>" key_mgmt=WPA-PSK }
- Replace with your country's Alpha-2 ISO code.
- Replace with your network name, and with your WiFi password.
Your wpa_supplicant.conf file should look something like:
country=GB ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 network={ ssid="MyHomeNetwork" psk="S3cretPa55word" key_mgmt=WPA-PSK }
What will happen now is that the Raspberry Pi will search for the ssh file in boot. If it finds it (no matter what the file contains), it will enable SSH. It will also look for the wpa_supplicant.conf file to set up its WiFi. This is why these files are needed.
Accessing the Raspberry Pi (Part 1) - Network Access
Now that we have configured for SSH and setup the WiFi, it's time to switch on the Raspberry Pi. Safely remove the SD card from your computer, and plug it into the Raspberry Pi's SD card slot. Plug the power supply in, and power it up. During this time, the Raspberry Pi will boot up and connect to your WiFi network.
To SSH into your Raspberry Pi, you will need its local IP address. The local IP address allows the router to identify which machine is accessing the network. You can get your Raspberry Pi's IP address by either going into your router and browsing through all the connected devices, or by using some IP scanning software (like Angry IP Scanner). I will use Angry IP Scanner, as it is really easy to use.
What this software does is scan through all the IP addresses available on the network, and finds all devices that are connected to your router. The software name is a bit misleading, in that it doesn't get angry, nor will it do anything malicious to your computer or router.
If you use Angry IP Scanner:
- Install the software on your Windows/Mac machine, and launch it.
- Set your IP range. My local network runs on 192.168.1.xxx, so I set my range to be 192.168.1.0 to 192.168.1.255. To help you determine what range to use, have a read through this article.
- Press the Start button, and let Angry IP Scanner scan through the IP addresses.
- Once Angry IP Scanner has completed, look for the IP address whose hostname is something like raspberrypi.local.
- Congratulations, you have your Raspberry Pi's IP address!
Note: if you have multiple Raspberry Pis on your network, you might need to do a scan with your new Raspberry Pi powered off, then scan again with the Raspberry Pi switched on. You'll need to compare which new connection has been made on the network.
Accessing the Raspberry Pi (Part 2) - SSH Access
Now that you have your Raspberry Pi's IP address, you can SSH into it. As a Windows user, you will need an SSH client like PuTTY.
- Launch PuTTY.
- Under the Category section on the left side of the window, make sure you have Session selected.
- On the right side of the window, under Hostname (or IP address), type in your Raspberry Pi's IP address you found in the previous step. In my case, it was 192.168.1.77.
- Under Port, make sure it is set to 22.
- Under Connection Type, make sure SSH is selected.
- [OPTIONAL] You can save these settings by typing your session name under Saved Sessions, then pressing the Save button.
- Press the Open button. If you're connecting for the first time, PuTTY will prompt you about allowing fingerprint keys. Allow them to continue SSH-ing into your Raspberry Pi.
- The console should ask you for a username login. Type in pi and press Enter.
- You'll then be asked for a password. Type in raspberry and press enter.
That's it!
Setting Up the Raspberry Pi (Part 2) - Update the Raspberry Pi
It is always good practice to keep the Raspberry Pi OS up to date. Once you have SSH-ed into your Raspberry Pi, type the command sudo apt update and press the Enter key. This will update packages used within the OS. If if asks you for confirmation, press y. You will see lots of text and percentages, but that's ok!
Once it has completed, you will need to update the OS itself. Run the command sudo apt full-upgrade. This will download and update the OS, and remove old dependencies that it no longer needs. You will see more text and percentages, and that is also ok!
Once that has completed, reboot the Raspberry Pi by running the command sudo reboot. This will close the SSH connection and reboot the Raspberry Pi. Wait for a couple of minutes for the Raspberry Pi to boot up again and connect to your network. Then repeat Step 4 above to SSH into the Raspberry Pi again.
Setting Up the RaspberryPi (Part 3) - Easy Hostname
Now, we need to enable easy access to the Raspberry Pi's file system so that we can access the Raspberry Pi's files and folders. To do so we will need to change the Hostname and install some additional packages.
- In the Raspberry Pi's console, type in sudo raspi-config and hit Enter.
- Navigate with your arrow keys to Network Options and hit Enter.
- Select Hostname.
- Set your new host name. For example, I set mine to MyLocalPi. Then press Enter.
- Keep select Finish, and return to the main screen. Exit the RaspiConfig utility.
- If it asks you to reboot, select yes. If not, in the console, type sudo reboot, and hit Enter.
You can check that you're hostname has changed by typing hostname in the console and pressing Enter. You should see your new hostname appear. You can also run Angry IP Scanner again, and you should see that the hostname of your Raspberry Pi will have changed.
Setting Up the RaspberryPi (Part 4) - Samba
Now we are going to set up file access using Samba. Samba is the package that gives machines access to the Raspberry Pi's file system. With it, you should be able to navigate the Raspberry Pi using Windows' explorer window or Mac's Finder. For Windows users, this is known as Windows File Share. It also allows Mac users to access the Raspberry Pi under the Shared folder.
- In the console, type sudo apt install -y samba. This will install the Samba package.
- Once installed, type in sudo nano /etc/samba/smb.conf. The will open the file smb.conf in the console, ready for editing.
- We need to configure Samba to grant us access to the file system on the Raspberry Pi. At the bottom of the smb.conf file, add the following lines:
[PiShare] comment=Pi Share path=/home/pi browseable=yes writeable=yes only guest=no create mask=0740 directory mask=0750 public=no
- Press Ctrl+X (control key and X key) to exit. When asked to save, press Yes.
- Now we need to create a Samba user for remote access. In the console type sudo smbpasswd -a pi. This will create the Samba user pi.
- Create a password for your Samba user. You will be prompted to re-enter that same password.
That's it!
Accessing the Raspberry Pi (Part 3) - File Access
Now that you have set up the Raspberry Pi, it's time to turn to your remote machine.
You can access your file system the following ways:
- Open the Run window by pressing WinKey+R, and typing in \\[HOSTNAME], replacing [HOSTNAME] with the hostname you typed in Step 6.
- Open an explorer windows (for example, Documents or My PC). In the address bar, type in \\[HOSTNAME], replacing [HOSTNAME] with the hostname you typed in Step 6.
- On the Desktop, right-click and select New->Shortcut. In the location text box, type in \\[HOSTNAME], replacing [HOSTNAME] with the host name you typed in Step 6. Press Next. You can then give the shortcut a name of your choice.
When you try to access the Raspberry Pi file system using any of the methods above, Windows will ask you for a username and password. Use the username and password you set up for the Samba profile (Step 7).
Final Notes
That's it! You can now navigate the Raspberry Pi file system through you standard machine's file navigation system. This makes it so much more convenient than having to plug mouse, keyboard, and monitor, or having to navigate through the directories with multiple ls commands.
For more confident and advanced users, you can also run git checkout commands directly onto the Raspberry Pi through Windows. This makes it a lot easier to handle checking out, modifying, and committing code to a repository.
A rather important note:
When you access the Raspberry Pi file system, you will be presented with two folders: pi and PiShare (or similar, depending on the Samba username and the configured name in the smb.conf file, both in Step 7). In this case, selecting either folders will give you access to the files and open them, BUT navigating through the pi folder means you lose editing priviledges, ie you will not be able to create and edit files. However, navigating through the PiShare folder gives you full access.
That's it! Now you don't even need mouse, keyboard, and monitor for you Raspberry Pi projects! How liberating is that?