I wanted to demo a Raspberry Pi 3 project at a location without a 120-volt outlet. No problem, just plug the Pi into a USB cell-phone brick and we’re good to go, right? The thing is, I also had to use several desktop applications for the demo. That’s normally handled with a monitor and wireless keyboard.
How can you run a desktop program without a monitor?
It’s common to log into a Raspberry Pi over Wi-Fi or a wired connection to the router and execute applications from another network-connected machine using ssh. What if there is no Wi-Fi, no network and no router? The good news is that you can connect a Pi to your Linux laptop or even another Pi with a tiny LCD display, using a spare Ethernet cable. It’s called peer-to-peer networking and that’s what we’ll discuss today. We’ll also be headless.
The Basic Setup
You’ll actually want to do the initial peer-to-peer network setup on your target Raspberry Pi using a monitor and keyboard/mousepad. Once everything is working you can run the Pi headless (no monitor or keyboard) and access all the programs on an external machine. The peer-to-peer network technique works on any Raspberry Pi with an RJ-45 Ethernet port.
The Pi 3, in particular, also has a built-in Wi-Fi radio. Our modded setup won’t affect the existing Wi-Fi configuration, so when you are back within range of a friendly AP, just ssh over Wi-Fi for remote logins.
For convenience, I leave the wired connection configured for peer-to-peer since I don’t usually need it for Internet connectivity when connected to a Wi-Fi access point.
Boot up the Pi and ensure that you are connected to your Wi-Fi access point (AP). Check the little Wi-Fi indicator at the top-right on the toolbar. It should show the familiar arcing signal lines. You can also click on the symbol and there be should check mark next to your AP name. If not, connect to your local AP.
Next, use the curses-based NetworkManager front-end program to configure the peer-to-peer network.
What Is Curses-Based?
Linux has several modes for user interfacing. One is the desktop. Applications appear as icons and display various windows when they run. The desktop might have a taskbar at the top and color decorations to control the windows.
There is also the command line. The command line runs in a terminal. It could be in a terminal window on the desktop or on a serial terminal, without a desktop environment. You type in commands and use Enter to execute them.
Another user interface is called curses or more recently new curses (ncurses). A curses application creates a text-menu driven way of entering data. You use the arrow and tab keys to move around through the menus. Curses works with or without a desktop environment, but it’s easier to use than the command line. You can even use it over ssh. Since it’s text-based, you don’t need to use the “-X” option with ssh, as you normally would for running remote desktop applications.
In this case we use a program called nmtui to change the network parameters for peer-to-peer networking over an Ethernet cable. Nmtui is part of the NetworkManager package so you should be good to go with a recent standard version of Raspbian.
Begin by opening a terminal on the Pi and run nmtui from the command line. The typical curses-style display will appear on the monitor. All curses applications look like this:
Arrow to the “<Edit a connection>” menu item and hit Enter. A new window will appear and the “Wired connection 1” item should be highlighted in red. Tab over to the “<Edit…>” menu item and again hit Enter.
The next screen shows the Profile and Device names and various other parameters. Arrow down to the “<= IPv4 CONFIGURATION>” selection and press Enter.
A new list will pop up.
Arrow down to “<Shared>” and press Enter. The “IPv4 configuration” item will now show “<Shared>”. Tab over to “OK”, at the right and press Enter. Tab down to “<Back>” and press Enter.
You should now be back at the main Network Manager TUI window. Arrow down to “Quit” and hit Enter one last time.
That’s pretty much it. If you want to return to using regular wired Ethernet, say for Internet access, just return the IPv4 Configuration value, back to “Automatic.”
You should see the wired Ethernet device at the top, a lo device second and the Wi-Fi (wlan0) device at the bottom. My Pi’s wired connection appeared as a 10.42.0.1 IP address. The Wi-Fi device connected as 192.168.1.107. These are fairly standard IP addresses.
Your network interfaces should resemble the following screenshot.
Wired and Headless
Plug one end of the Ethernet cable into the port on the Raspberry Pi and the other end into your Linux notebook. The notebook should immediately connect to your new wired peer-to-peer network and change the NetworkManager icon, on the taskbar, to an up/down arrow.
Slide over to the Linux notebook and open a terminal. Run ifconfig on this machine to check the network interfaces. Notice that the wired Ethernet address now has a similar IP address as the Pi. The Pi address will be something like 10.42.0.1 and the Linux notebook will be something like 10.42.0.224. Think of the Pi as the server, so it will have a 1 at the end.
This is great because when you are out in the field, you don’t have to go searching around for the Raspberry Pi 3’s IP address like you would over Wi-Fi.
Also, modern wired Ethernet ports can use a standard patch cable with an RJ-45 connector at each end. There’s no need for a “cross-over” cable, like in the old days.
Next, log into the Pi from the notebook using ssh. For example:
drtorq-notebook% ssh -X email@example.com
Enter the password and we should now be live on the headless Pi. When you get out in the field the WiFi connection won’t work because it’s not hooked up to a known AP. Just ignore it.
After connecting to the Pi, we’re free to run desktop applications to our heart’s content. I needed guvcview and luvcview for my project. They both worked great. They also were very fast over the wired connection between the machines compared to WiFi.
Give this technique a try for your next ultra-portable Raspberry Pi project. I think you’ll find it very useful.