When most people buy a Raspberry PI, they think they need a computer screen. Don't waste your money on unnecessary computer monitors and keyboards. Don't waste your time moving keyboards and monitors between computers. Don't tie up a TV when it's not needed. You can easily use your primary computer to access it.
Running headless (without a display) Raspberry Pi or other Unix systems can be extremely useful. I've used xWindows for many years and it's great when you need graphical displays instead of command lines. In the case of robots, it's often not practical to use a monitor because it moves around or it is too small for a monitor. Raspberry PI's can be run off batteries and in locations that make it impractical to have a monitor. Using SSH allows line commands but there are graphical interfaces that are useful.
This instructable is not an xWindows tutorial. It gives you enough information to get started. Don't let the terminology and jargon intimidate you. It's actually quite easy to use basic xWindows.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Terminology That May Seem Confusing
You can probably skip this section. Refer to this section if I use words or concepts you want to understand. These basic concepts may interest you or may be helpful in discussing a problem you are having.
Don't be intimidated by the amount of information here
There is terminology and jargon that feels very confusing. Its not needed unless you need to ask for help so you can ask the right question. It's easy to setup and use. Don't memorize the terminology. Just get comfortable with the concepts.
What does "running headless" mean
Running headless is where a computer runs without a display, keyboard and mouse. Sometimes we will refer to this as a server. It is a computer that is used remotely instead of accessing it directly from it's keyboard and screen. Running a headless Microsoft Windows means you can't run any graphical programs. E.g. Microsoft windows requires a display so it requires a display. On the other hand, running Microsoft Word on a headless Unix system works fine. Using xWindows on Unix allows you to run graphical programs from remote computers.
Since Microsoft "Windows" is proprietary to Microsoft, Unix can't easily use this term. Instead, we have "xWindows" to provide the same functionality but it's not tied to a display on the same computer. Microsoft Windows desktop, Unix xWindows desktop and Apples desktop all have a desktop with a start menu and icons. Clicking an icon or the start menu will open a window.
xWindows runs on Unix
If you are not running Microsoft Windows or Apple, then you are probably running Unix. Android, Raspbian, Linux, Solaris, Noobs, Ubuntu, and many more are common flavors of Unix. Most of these Unix flavors come with xWindows installed. Android is the exception because it runs on computers so small that xWindows is too slow.
When you open a window (on any platform), it must be displayed on a desktop. For an xWindows, this desktop is called an xServer that has a keyboard, mouse and display. In fact, several users will each have an xServer (with display, keyboard and mouse) on computers in the network. They can use xWindows clients on any computer in that network.
Microsoft Windows and Apple have this same desktop / window concept. The big difference is the desktop must be on the same computer as the Window. They can only have 1 user who is accessing the programs through open windows.
Step 2: Enable SSH and Configure Networking
Installing the OS (e.g. Linux, Raspbian, ...) requires a monitor, keyboard and mouse. For a Raspberry PI, you can temporarily use your TV for this and the cheapest USB keyboard / mouse you can find (or borrow). Other computers may temporarily require specific hardware during installation. Once you've enabled SSH and configured networking, this equipment should no longer be needed.
For Raspbian and Noobs, this is easily done during installation. To make these changes after installation, you can open a line command terminal and run command "sudo raspi-config". Currently, option 2 (networking) allows you to configure networking. Option 5 (interfacing options) sub-option p2 SSH allows you to enable ssh. These may change with future releases.
For other Unix systems, refer to the manuals for these configuration options.
Once networking is enabled, you will need to save the IP address to be used later. From the line command terminal, issue command "sudo ifconfig". You are interested in Eth0 (wired connectioin) or wlan0 (wifi connection). The "inet" value will have 4 numbers seperated by periods (e.g. #.#.#.# - in my case 192.168.1.4).
In a home environment, this IP address will probably stay the same. However, there are situations where it could change. If you have this problem where it changes, you can either setup a static IP in your router (see router documentation), lookup the new IP address in the router (see router documentation) or attach a monitor /keyboard and issue the ifconfig command mentioned above.
Step 3: Install a Desktop (xServer) on Other Computers That Access XWindows Clients on the Headless Computer
For Windows, I prefer Mobaxterm which is free for non-business use. Don't get intimidated by all the options. It has a lot of functionality that you don't need.
For Unix systems, the desktop will typically be running on the computers local display. If so, you already have it installed.
Step 4: Setup the XServer
Microsoft Windows using Mobaxterm:
To create a desktop session, click on the session icon followed by a click on the SSH icon. In the remote host field, enter the IP address from above. click on the "specify username" to enable the userid field and enter the userid in the field next to it (e.g."pi" for Raspbian). Click on the "advanced SSH settings" tab and in the remote environment field, select the desktop environment installed on that machine (e.g. lxde desktop for Raspian). Click ok to save the changes. You can now click on this session to open the desktop for that computer. The first time, you will be prompted to save the password. After saving the password, you won't be prompted for the password again.
ssh pi@#.#.#.# "DISPLAY=$DISPLAY nohup xxxxx" or using sudo to get root access: ssh pi@#.#.#.# "DISPLAY=$DISPLAY nohup sudo -E xxxxx" where #.#.#.# is the IP address of the xWindows client computer. where pi is the userid. xxxxx is the command (e.g. lxterminal for a line command terminal).
I suggest that you look into ssh-keygen to eliminate the password prompting..
Step 5: Optional: Merging the XServer Desktop With the Microsoft Windows Desktop in Mobaxterm
The best option with Mobaxterm is integrating the xServer desktop into the Microsoft Windows desktop. Cut and paste between environments is easy. You can easily switch between all windows regardless of the xServer window (e.g. alt-tab). It has less overhead.
However, if you remove the xServer desktop, you will need to add the start menu from that desktop. For lxde (as is the case for Raspbian), this is lxpanel. For other desktops, you will need to investigate how they implement their start menu so that you can start programs on the remote computer.
Setup Mobaxterm multi-window mode Click on Settings -> configuration. Select the X11 tab and select "multiwindow mode" in the X11 server display mode. Click OK to save.
The xServer Desktop will continue working in a single window as long as you don't change the session definition.
Change the session definition to run the start menu instead of the desktop
Right click on the session and select edit session. Click on the Advanced SSH settings tab. Change remote environment to Interactive Shell instead of the desktop you selected. Set the execute command to run the start menu for the xServer desktop (e.g.specify "lxpanel" for Raspberry Pi because this is the start menu for lxde). Click OK to save the changes.
Move and resize the new start menu When your mouse goes over this new start menu, it will expand which is annoying when you are trying to do something with Microsoft Windows. Positioning it in a seldom used location is extremely important.
Here are the instructions for lxpanel (as in Raspbian).
For other desktops, you will need to determine how to make these changes.
Double click the session to start lxpanel. This will add a start bar for the remote xWindows system at the top of your screen. Right mouse on this start bar and click on "panel settings". In these settings, select "left" in edge and "center" in alignment. Change height to 20% and width to 20 pixels. Click on the advanced tab. Make sure minimize is checked and change the size when minimized to 2 pixels. Click close to apply the changes.
When minimized, the new start menu will be barely noticeable in the left center of the Windows desktop. It will be a very thin black indentation. Moving your mouse over this will make it expand. The applications menu is the top icon. You can use this to open new windows and remote application as needed.
Step 6: Don't Set a Root Password or Login As Root
Inexperienced users set the ROOT user password and use ROOT directly (usually all the time). If you don't have the experience to know how to use the alternatives, then you shouldn't log in a root. Root can be extremely dangerous and should only be used when it's truly needed.
I've never set the ROOT user password. Instead, I use one of the many alternatives and close them immediately when I no longer need ROOT authority.
Running 1 command with root authority
sudo xxxxx (e.g. sudo apt-get install gparted)
Running several commands with root authority in a line command terminal
Use exit command to return to normal user prompt.
Running graphical programs with root authority
sudo -E xxxxx (e.g. sudo -E gparted)
Raspberry Pi "root terminal" requires administrative password.
Don't set the ROOT password because of this.
Instead, use the "sudo -E su" command mentioned above. Alternatively, you can modify "root terminal" on the start bar. To change the command for application menu -> system tools -> root terminal. If you right mouse on "root terminal" and select properties, you can select the "desktop entry" tab. Replace the command with "lxterminal -e sudo -E su". This will open a line command prompt as root user and allow you to run graphical commands because the DISPLAY variable is retained.
"Raspberry Pi Configuration" requires administrative password
Don't set the password for root because of this.
Instead change the command for application menu -> preferences -> Raspberry Pi Configuration. If you right mouse on "Raspberry Pi Configuration" and select properties, you can select "desktop entry" tab. Verify the command should end with sudo -A rc_gui. If it does, then replace the entire command with "sudo -E rc_gui".
"Preferred applications" requires administrative password
Don't set the password for root because of this.
Instead change the command for application menu -> preferences -> Preferred applications. If you right mouse on "Preferred Applications" and select properties, you can select "desktop entry" tab. Verify the command should end with sudo -A rp-prefapps. If it does, then replace the entire command with "sudo -E rp-prefapps".
-E option for sudo
The -E forces sudo to pass all environment variables. The DISPLAY environment variable must be retained for graphical commands so they know how to communicate with the xServer. Warning: the other variables could conflict with the new root environment being built.Passing only the DISPLAY variable would be a far better solution.