- Using the Shell (Command Prompt or Terminal)
- Simplifying access to the Pi with SSH
- Transferring Files with PuTTY
- Securing the Pi
- Enabling the root user account (Superuser)
- Remote Desktop (Accessing your Pi from a PC)
- Using a Serial Console
- Navigating in the Shell (command prompt)
- Next Steps
This blog post has a collection of steps or ‘things’ that can be done with the Pi in order to be able to access it flexibly.
These steps allow you to work with the Pi either using a keyboard/mouse/TV or using your desktop PC or laptop and remotely connecting to the Pi.
The key steps are listed and all the links to click on for more information on topics. Not everything here is essential; it is more a la carte so you can pick and choose.
Use this blog post as a quick reference to do these ‘things’:
- Using the shell (command prompt)
- Simplifying access to the Pi with SSH
- Transferring Files
- Securing the Pi
- Enabling the root user (superuser)
- Remote desktop (accessing the graphical display from a PC)
- Enabling the Serial Console connection
- Navigating in the Shell (Command Prompt)
Note: If you're completely starting from scratch, then you can configure and install a fresh micro SD card with the latest appropriate image on it, by following the information here: Before You Start Coding the Pi and then come back here!
Using the Shell (Command Prompt or Terminal)
When a PC runs Windows it isn’t just running an operating system, it is also running a graphical user interface. Daily interaction with the PC is through the graphical user interface but users can bring up a command prompt in Windows and just type instructions. A similar thing is possible with the Pi. The operating system happens to be Linux, and the graphical user interface is called PIXEL. The entire thing can be called Raspbian which is the name of the combined distribution of Linux, PIXEL and any other stuff bundled by the creators of the Raspbian release.
The shell can also be loosely referred to as a command prompt or a terminal window; they all mean the same thing. It can be invoked from the Pi’s desktop as shown here.
Simplifying access to the Pi with SSH
If you’re not able to get access to the TV always and wish to use your Pi from your PC, then something called SSH is very handy. It allows you to write and edit your code on a PC, and then transfer the code across to the Pi and run it remotely. To use this, first follow the steps to enable SSH capability.
You'll also need to know the IP address of the Pi. There are two easy ways:
- If you only have one Pi connected to your home network, then try going to a Windows command prompt (or a Linux or Mac terminal) and typing ping raspberrypi because often that will reveal it.
- Alternatively, navigate to your home router using a web browser and check within the pages there. The address of the home router is usually http://192.168.0.254 or http://192.168.1.254
Here's an example using the first method:
Next, ‘SSH client’ software is needed. The free option for Windows is called PuTTYhttp://www.putty.org/and it works well (for Mac and Linux PCs, SSH client software is built-in, it is accessed from the Mac/Linux command prompt by literally typing ssh). After PuTTY is installed, type in the IP address of the Pi (obtainable as described above) and type a name for your Pi and click Save.
From now on you can access the Pi by double-clicking the name, and it will bring up a window where it will prompt for the username/password (pi/raspberry by default).
The screenshots further below happen to use software called SecureCRT merely because I’m more familiar with it, but the same result can be achieved using PuTTY. I’ve customized my display so that the Pi appears in a blue screen. You can do similar things with PuTTY.
Transferring Files with PuTTY
Transferring files from the PC to the Pi, or vice versa, is possible with PuTTY too. It is achieved using the command line on your PC. For Windows, click on the start prompt and type cmd to bring this up.
For this example, a file called example.txt will be transferred from the PC to the Pi user’s default home folder or directory which is at /home/pi
C:\Users\bob>cd .. C:\Users>cd .. C:\>cd "Program Files" C:\Program Files>cd PuTTY C:\Program Files\PuTTY>pscp.exe c:\myprojects\example.txt email@example.com:/home/pi firstname.lastname@example.org's password: example.txt | 0 kB | 0.8 kB/s | ETA: 00:00:00 | 100%
It is fairly similar to pull files from the Pi to the PC. Here a file called example2.txt in the pi user’s home folder is transferred to the PC:
C:\Program Files\PuTTY>pscp.exe email@example.com:/home/pi/example2.txt c:\c_root\install firstname.lastname@example.org's password: example2.txt | 0 kB | 0.0 kB/s | ETA: 00:00:00 | 100%
There are other programs which achieve the same thing with a graphical user interface and they could be easier to use. PuTTY is a basic free option for Windows. For Mac and Linux users, a different software option is already built-in; from a Mac/Linux command prompt the program is called sftp (type man sftp for detailed instructions).
Securing the Pi
The steps here are quite important! Once Raspbian is up-and-running, it is advisable to change the password for the default ‘pi’ user. To do this, get to a command prompt and type passwd and it will prompt for the old password (raspberry by default) and then the new password. Simple passwords won’t be accepted.
Enabling the root user account (Superuser)
Some things are privileged and cannot be accessed by someone who has logged in as the ‘pi’ user. However, sometimes it is necessary to perform a privileged task. One method is to type sudo before entering the command for the privileged task.
Another method that some would find more preferable is to enable the root user account. It is easy to do using the sudo -i command. As pi user, do the following:
Now you have a Pi which you can log into as the pi user, or as the root user.
Remote Desktop (Accessing your Pi from a PC)
When the Pi is connected to a network there are many ways to connect to it. One method, SSH, has already been discussed. It provides a command line interface and the ability to transfer files to/from it. Occasionally the graphical capabilities of the Pi may be needed, and for that a TV can be plugged in using the HDMI connection. However, it is also possible to use the network to display the Pi’s desktop and any graphics on a PC. There are several ways to do this. One method called Windows Remote Desktop is described here. Another method is called VNC and there are online resources on it.
To enable the Pi to be able to send graphical display information, software called xrdp needs to be installed as shown here:
Next, go to the Windows start bar and type remote desktop and it should find the application. Click on it.
Type the IP address of the Pi and click on Connect. A large blank window will appear, and a prompt for the username/password for accessing the Pi.
Once the details are filled it, the usual Pi desktop will appear on the PC.
If you’re not using a Windows PC and are instead using a Mac or Linux PC then software needs to be installed (Windows already has the correct software installed). For a Mac, install Remote Desktop Client from the itunes store, and for Linux PCs install rdesktop.
Using a Serial Console
If you look around the back of most computers and computer-based products in business and industry, you’ll see a port marked ‘Console’. It offers a last-resort method of connection if more sophisticated methods like keyboard/mouse, wireless and Ethernet fail. The console on such computers can be connected to a PC using an RS232 serial cable, or can be connected to an old-school dial-up modem if the computer is at a remote site and no-one can get to it in any other way. The Pi does have a console connection, but it needs an adapter before it can be attached to a computer. It is known as a USB-to-UART adapter and it converts from a PC USB connection to a three-wire connection where one wire is ground, and the other two wires send data to/from the Pi using digital values of 3.3V and 0V to represent 1’s and 0’s, and groupings of these representing ASCII characters. The end result is a text-based connection much like with SSH, but using a relatively simple communication method compared to SSH over the network. When all else fails often the serial console connection continues to run and provides entry into the computer to examine the cause for failure of the other connection methods for example.
There are many suitable USB-to-UART adapters, but they come in different voltage versions. A 3.3V compatible adapter is required which most are (some older ones might only support 5V which will damage the Pi’s serial interface).
A suitable flying lead USB-to-UART adapter (the functionality is embedded within the USB connector housing) is and it has six wires. Only four need to be connected (two are 3.3V power and ground, and the other two are the transmit/receive wires for the digital data). You could even make your own USB-UART adapter on stripboard. The photo below shows a home-made one on a printed circuit board (PCB).
Setting up the console has a few implications due to some discrepancies between the Pi 3 and earlier Pi releases that won’t be discussed here, and a power user will probably want to read up to understand them. Anyway, to enable the serial interface on the Pi for now, as root user, go to the /boot folder and append the following at the end of the config.txt file:
# initialize the uart (mini-uart on pi 3, PL011 on earlier pi's) enable_uart=1
As an optional (but recommended) thing, in the same folder (still as root user) edit the cmdline.txt file and remove the text quiet there. By removing this text, the serial console will provide lots of useful debugging information when it starts up. The Pi must be rebooted for the changes to take effect. Still as root user, type reboot to do this.
On the PC, ‘serial terminal software’ is needed. On Windows PCs PuTTY can be used for this. Go to the Start button and then type device manager and click on it. Scroll down and expand the ‘Ports’ section. Plug the USB UART adapter in and you’ll see a new port appear. The name of the port (COM16 in the screenshot below) will need to be noted, and then in PuTTY select the ‘Serial’ connection type instead of the usual SSH, and type in a speed of 11500 and enter the name of the port to match the COM name from the device manager window. The settings can be saved and then the saved session can be started at any time by double-clicking on it.
When the session is run a blank window appear, press Enter and you should see the Pi’s usual login prompt. As mentioned earlier if the Pi is now rebooted then upon startup a whole load of information will appear.
Navigating in the Shell (command prompt)
The command prompt used so far actually has a formal name; it is called the bash shell. Other command prompts are possible too. The bash shell happens to be one of the most popular ones. There are things that are worth learning because it will speed up working in the command prompt.
One obvious thing that you may have already noticed is tab completion; if a command or folder name or file name is partially typed, pressing the tab button will auto-complete.
Another thing that is less obvious but is highly useful is that when typing in a long line, if you make a mistake and you need to quickly move to the beginning of the line, you don’t need to hold down the left-arrow to move the cursor one character at a time. You can just press Ctrl-A. To move back to the end of the line, press Ctrl-E.
If you’ve typed a long line but realized you don’t need it, instead of holding down the backspace key and deleting character-by-character, you can just press Ctrl-C. The text will stay on the screen but the line will be aborted and will not be actioned by the bash shell.
To repeat a command (or to recall it or edit it before executing the command again) press the up-arrow. You can navigate a chunk of historical commands by using the up/down arrow keys.
Another useful thing is to type notes or comments in the command line; these are useful when logging the entire session (this is an option in PuTTY for example) and you want to intersperse some notes to yourself in the logged file. To insert a note, just start the line with a hash (#) character.
Another really nice feature requires a bit of configuration. Edit the .bashrc file that is in your home folder (/home/pi). The file is normally hidden from view because the filename begins with a dot. At the end of the file place the text stty stop '' (that's not a double-quote, it is two single quotes) and then save the file. Now whenever a new command prompt is created, a history searching capability is enabled. If you know you’ve typed a command before with (say) the characters gcc in it, type Ctrl-R and then type gcc. The bash shell will automatically display the entire line that contained that text. If you’re fine with it, press the right arrow to edit the line and press enter to execute it. If you want to abort, press Ctrl-C. If the line found isn’t the one you need, press Ctrl-R again and it will continue to search the next occurrence in the command history. If you reach the end of the history and want to search in the other direction, use Ctrl-S.
Sometimes the simplest methods are the best, and another popular method is to just have a text editor open and copy-and-paste your important commands into there for future reference.
Here is a summary of the tasks and shortcuts:
|Move cursor to beginning or the end of a line||Press Ctrl-A or Ctrl-E|
|Abort a line||Press Ctrl-C|
|Repeat commands from the history||Use the cursor keys. Press the up-arrow repeatedly until you find what you need|
|Type notes or comments||Start the line with a hash (#) character|
|Search for commands from the history||Press Ctrl-R and then type the search term. Press Ctrl-R again to continue the search. Press Ctrl-S to search in the other direction (see the text earlier how to enable this feature). Once you’ve found your command line, press the right arrow to edit it or press Enter to execute it|