Remote Access Deluxe

So here we are: you’ve diligently followed all my previous instructions and built yourself a nice environment on your Raspberry Pi that does not require a keyboard, mouse and monitor. That sounds awesome. Until you want to get some real work done on the tiny single-board computer. For example, some programming.

Developing software for your Pi directly on your Pi is ideal : you can run it directly on the hardware you’re targeting. This makes the “code-compile-run” loop tighter, allowing you to generate more bugs per hour. And hopefully correct them all.

As it stands, however, you are limited to a command line though an SSH connection, and remote desktop access with VNC. That means your development tool options are limited to :

  • Command line editors and compilation commands. That would definitely slow things down.
  • GUI tools used through a VNC client. Slightly better, but sadly too slow for most people’s tastes. If you hate lag in video games, you will hate coding through a remote desktop.

This page describes how to setup your Raspberry Pi for a couple of much better options that will open worlds of possibilities : Samba and X11 forwarding.

1.   Setting-Up Samba File Sharing

Whatever computer you’re using, it’s a given that it is significantly more powerful and user-friendly than a Raspberry Pi. It also supports more editors and programming tools that would definitely make your life easier and boost your productivity. It would be a perfect world if you could use them to program on your Pi directly.

That’s what Samba enables.

Samba is an open-source implementation of the SMB network protocol. It is mostly used by Windows operating systems as the protocol behind network drives or “shares”, for connecting to NAS servers and “personal clouds”. Need I remind you, I’m a lifelong (and traumatized) Windows PC user. That being said, nothing is stopping you from using SMB with any operating system.

We’re going to use this technology to share your Pi’s files over your local network so that you can use any modern editor you’re already using on your PC to edit the source files on your Pi. That includes Visual Studio Code (or its telemetry-free version VSCodium), which will be our example today.

Log into your Raspberry Pi with ssh and let’s get it done.

1.1.   Install Samba

SMB support is usually present by default on Linux, however it is not present on Raspberry Pi OS distributions. So we need to install it. Make sure your Pi is connected to the internet and, if you haven’t done so already, update and upgrade your package manager with those two commands :

sudo apt-get update

sudo apt-get upgrade

 

They may take a little while to execute, enough to go grab a coffee. Once they have completed, you can install Samba with :

sudo apt-get install samba samba-common-bin

At some point, you may be asked whether you want to add WINS settings. Unless you know you will need NetBIOS name server support, you can answer yes or no, it won’t affect file sharing. Answering “yes” simply installs additional software that you may end-up never using.

You may also get an error message that you will be told do disregard. It looks scary but it won’t affect you :

No for real, this is fine. Why do you think all that open-source software has licenses that contain words like “this program is provided as-is”, “without warranty of any kind” and “should the program prove defective, you assume the cost” ?

1.2.   Configure Samba

Samba starts immediately after installation and does not require rebooting the Raspberry Pi. It does, however, require configuration. You need to tell Samba important things such as which folder(s) you want to share, who with, under which name, and with what permissions. This is done by editing the smb.conf file, like so :

sudo nano /etc/samba/smb.conf

This command opens “smb.conf” in the “nano” command line editor. I chose nano because it is by far the simplest command line editor pre-installed on your Raspberry Pi’s OS. Some of its commands are still unintuitive but don’t worry : once you’ve edited this one file, you might never need to use it ever again. That’s the whole point of what we’re doing here.

Samba installs with a default “smb.conf” file that serves as an example and is otherwise useless. If you’re interested in learning a bit about Samba configuration, it’s a good read though. Once you’re done, return the cursor to the first line and delete the entire contents of the file by holding down “Control K”. Then paste this in :

[global]
 workgroup = WORKGROUP
 wins support = yes
[share]
 comment=Raspberry Pi Share
 path=/
 browseable=Yes
 writeable=Yes
 only guest=no
 create mask=0777
 directory mask=0777
 public=no

 

“WORKGROUP” is the default workgroup name for Windows networks. If you have changed yours, replace it with your actual workgroup name.

“[share]” is the name of a share as it will appear to other computers. Better give it a meaningful name. If you’re going to use several different Pi’s, it’s a good idea to give your share the same name as the Pi (hostname). You can also call it something like “SDCard”. This line starts a share block : the following lines will pertain to that particular share. You can specify multiple shares on the same Pi by appending multiple share blocks built on the same syntax.

“path” is the path to the local Raspberry Pi folder that you wish to share. In this example I used “/” (the root) which means the entire filesystem will be shared. This is not safe at all, but it is practical if you’re just messing around and don’t mind potentially breaking your Raspberry Pi’s OS.

The permission masks are set to read-write-execute but of course you can place restrictions depending on your needs.

The remaining parameters are pretty self-explanatory.

Save the file (Control O, then Enter) and quit nano (Control X).

Because we set our share as “not public”, we now need to tell Samba who is allowed to access it. This is a basic precaution you should always take. Use this command :

sudo smbpasswd -a username

Replace “username” with your own username. This command will ask for username’s password. This is not the same as your Linux user password : this is only the password for accessing the share. Good cybersecurity practice demands you use a different password… but you do you.

And that’s it for the Raspberry Pi side of things. Now you can map that share as a network drive on your PC.

1.3.   Mapping the Share

You’ll need to go into the “Computer” menu of the file explorer to find the relevant icon :

It will present you with this dialog box :

Choose the drive letter you’d like, as long as it’s unused.

Folder is the network path to the share you’re mapping. It’s a path that specifies both a computer and the share on that computer. This can be written in two ways :

  • If your Pi has a fixed IP address, it should look like : \\123.123.123.123\share
  • If your Pi uses DHCP to get its IP address, use its hostname instead : \\raspberrypi\share

Replace “raspberrypi” by the name (hostname) you gave your Raspberry Pi, and “share” by the name you gave your share.

Pro tip : if you don’t remember your share’s name or if you want to map only a subfolder, you can enter just the \\hostname and then click the “Browse…” button. This will show you every share on the Raspberry Pi and let you choose one, or one of their subfolders, to map as a drive in Windows.

You will then be asked for your credentials : that’s the username and password you gave to Samba after editing “smb.conf”. Tell Windows to remember that information. As soon as you hit Enter, you’ll have access to all the files on your Pi, straight from Windows :

You should already notice that navigating this share is a lot faster than using the command line or VNC.

1.4.   Remote Development in Action

Let’s finish with Samba with a quick demonstration to verify that all is in order. We’re going to create a “Hello World” program in Python and run it using Visual Studio Code. If you haven’t installed it already, I strongly suggest you do. You can get it here : https://code.visualstudio.com/. VS Code, unfortunately, may come with some Microsoft telemetry. If this bothers you, there’s a telemetry-free build that is absolutely identical, save for the name and icon : https://vscodium.com/.

VS Code is a very powerful editor aimed at developers and it has a huge number of features that can make your life easier. It’s basically a sort of modular IDE. That’s a vast topic, well beyond the scope of this page, so I’ll let you look into it on your own. All you need to know for now is that it’s a multi-document editor with support for extensions. Those extensions are mostly syntax-highlighting for different programming languages and file formats. Since we’re about to write a little Python program, you may want to install a Python extension but it is by no means necessary.

Upon launching Code, you should get to its Welcome page :

Create a new file by clicking “New File” or going into the File menu or just hitting “Control N”. The editor will offer you the option of choosing a programming language (it couldn’t guess since we haven’t named our file yet) :

Select Python and type this very simple bit of Python in your shiny new file :

Now save that file directly into your Raspberry Pi, since you can now access it just like any other drive on your PC. Let’s name it “hello.py” and store it somewhere sensible, in our home folder :

You can now run it on your Pi directly from Code, as Code features its own terminal : it’s right there in the main menu !

Simply SSH into your pi and run “hello.py” :

And here we are. This is, by far, the most comfortable way to program on Raspberry Pi as you get all the benefits of a modern editor with no overhead on your Pi’s very limited CPU resources.

2.   X11 Forwarding

What if I told you that instead of using mere terminals and command lines to operate your Pi remotely, you could just launch any of your Pi’s desktop applications and have their GUI appear on your computer as if they were Windows applications ? That’s what X11 forwarding does.

If you really like the tools already your Pi, like the Geany and Thonny IDE, now you will be able to use them from the comfort of your PC without having to suffer the lag and performance impact of VNC remote-desktopping. The best part is, it’s really easy to setup and it’s free.

You just need to run an X11 server on your PC. The easiest way I know is to install MobaXterm. MobaXterm isn’t just a terminal, it also supports a lot of features and one of them is an X11 server that works right out of the box.

There’s a portable version, so you don’t need to install it. Whichever approach you choose, launch MobaXterm, start a terminal in one of its tabs, ssh into your Raspberry Pi and enter the name of a desktop application you’d like to run from your PC. As an example, here’s what Thonny looks like on Windows 10 (just type thonny and hit Enter) :

This will not work if you ssh into your Pi using a normal terminal such as “cmd.exe” or the PowerShell. Instead of your friendly Python IDE you’ll get an error message as Thonny can’t find an X11 server to connect to.

Pro tip : if you want to launch multiple programs, end your command lines with an ampersand (&). This will start each application in a different thread so that your SSH session isn’t blocked. Otherwise, you could only run one application at a time from a given session.

You may be curious as to how this wizardry is possible.

If you’re not versed in the inner workings of UNIX graphical user interface systems, this requires a little bit of an introduction. I’ll try to keep it as short as possible.

UNIX-like operating systems like Linux support an excellent GUI framework that was invented four decades ago. You just know you’re dealing with awesome software when nothing has been able to replace it since 1984 !

That system is called X Windows, and the latest major release is 35 years old. It’s version 11, hence the name X11. It hasn’t needed an update for the last 10 years. Think about it next time Microsoft or Apple or Google force yet another update on you.

X11 has an interesting architecture : to keep it simple, it separates the rendering of the user interface (what you interact with) from the application code, in what is called the client-view model in some circles. That separation makes it possible to have your applications’ windows anywhere you want, not just on the machine that’s running the application.

In this model, the Raspberry Pi is a client and your computer is the server. The rationale is that your Pi’s programs request GUI operations from whichever machine can render a GUI for the user. It’s a bit unintuitive since it means the user is working on a server to control a client (normally it’s the opposite) but you’ll get used to it.

3.   Copying Files

Setting-up file-sharing on a Pi, as we’ve just seen, takes a little bit of work. There may be times when you need something simpler. That’s what the Linux scp command is for.


Nope, not that one

scp (not to be mistaken with the amazing SCP Foundation and all its Lovecraftian horrors) is the secure copy command. It uses the Secure Copy Protocol, based on SSH, to send files over networks and provides the same authentication and encryption mechanisms.

Since scp is a command, it does not require you to install any software on either your PC’s or your Raspberry Pi’s, saving you a lot of time. Of course, you pay for that with reduced ergonomics since you’re back to using the command line instead of a GUI. But on occasion, it’s very helpful.

Another way to copy files to your Pi that may be more comfortable brings us back to MobaXterm.

If you look closely at the screen capture in the previous section, you’ll notice there’s a file explorer on the left pane of MobaXterm. Here, I’ve zoomed in for you :

Through SFTP (Secure File Transfer Protocol) MobaXterm provides you with convenient access to your Pi’s file system.

Now you might be wondering : “why did I have to mess with Samba and network shares, then ?”

Well this is different. When you created your Samba share, you also created access permissions for it and even user accounts. On the other hand, this SFTP browser only gives you the access permissions of the user you logged in as.

It’s still quite convenient, especially since there’s no need to configure anything. It just works, as soon as you login. You can drag-and-drop file between this browser and your local file explorer.

4.   Bottom Line

There are many different ways to access and use a Raspberry Pi over a network. The default propositions available “out of the box” (SSH and VNC) aren’t even the best. I doubt I’ve found every possible approach but when I do, I’ll update this page.

What’s always worth remembering :

  • Avoid VNC as it’s very taxing especially on small Raspberry Pi (like the Zero). VNC should only ever be considered if you need to use an application that relies on your Pi’s GPU.
  • Use X11 forwarding instead of VNC when you need to run a Raspberry Pi desktop app remotely. This places the burden of UI rendering on your PC and drastically reduces the network bandwidth required.
  • Better yet, map your Pi’s SD card or folders as network derives using Samba, so you can work on your Pi’s files using PC-native applications.