Lab 3: Working with your Pi (Pi 3/4)
The ultimate goal of this lab is to set up your Pi so you can
- Control your Pi, i.e., run command line on Pi. It doesn't matter if you use ssh to access remotely or directly operate on Pi through mouse/keyboard and monitor.
- Connect your Pi to the Internet, either via Ethernet cable or WiFi.
The instructions assume you can connect to campus WiFi (i.e., eduroam, RHIT-open), and you have your institute laptop with an Ethernet cable if needed. If you want to further explore the setup for a different WiFi environment, feel free to refer to the tutorial HERE.
You'll work on this lab individually (but it's ok to ask other students for help). In this lab, you'll set up Linux on your Pi and get some experience with command-line tools.
When you're done with this lab you will have done the following things:
- Set up Raspbian on your Raspberry Pi 3 B+ or Pi 4
- Log into your Pi from your laptop
- Install Git to obtain and save your lab work.
- Explore a variety of command-line tools
The equipment you need for this lab:
- Your Laptop (with Windows... if you use Linux or Mac, the instructions will be a little different)
- A Raspberry Pi 3 model B or Pi 4 project board.
- A micro SD card (at least 8GB)
- An adapter that will let you plug the micro SD card into your laptop.
- A 2.5A micro USB power adapter (or another way to get 2.5 amps into the pi).
- An Ethernet cable and Ethernet adapter for your laptop.
Gather this stuff, make sure you have it all before you start.
Part 1: Setting up the Pi
Prep the Disk
- During the prelab for lab 1, you downloaded a Raspbian image. If you deleted it, download it again from here onto your local machine. It's really big, so you may want to put it somewhere not backed up.
- Insert the SD card into your laptop. Be careful not to bend the SD card or adapter.
->
- Write the image onto your SD card. For this, follow the instructions on the raspberry pi site here or try these few steps:
- Obtain Etcher and install it.
- Find Etcher in your start menu and launch it.
- Select the file you downloaded in step 1 (Etcher may default to the wrong home directory, you might need to hunt for it.)
- Select the SD card you've inserted.
- Click the "Flash!" button to begin writing data to the SD card.
- Wait for the write to complete, then remove and reinsert the SD card.
- Once the SD card is written, you must modify Raspbian to enable SSH.
- In Windows File Explorer, navigate to the "Boot" drive (your newly-written SD card)
- Enable viewing "File Name Extensions" so you can see and remove file extensions. Here's what it looks like in Windows 10
- Create a new text file called "ssh" (be sure there is no
.txt
extension)
Find the file named
cmdline.txt
, add the following command at the end of the last line.
ip=192.168.137.2
In my case, there is only one line in that file and it is ended with text like this
plymouth.ignore-serial-consoles
. So simply append the above command after this string. (Don't forget to add a space before you type in the ip command)Remove the SD card from your laptop. To do this from windows, first you must "eject" it via "Safely remove hardware". Once Windows has finished with it, you can remove it.
Prepare the Network
Next, you need to set up a small local network so your computer can talk to the Pi! This section will have you (1) enable Internet Sharing and (2) disable IPv6 for easier connections to your Pi. Be sure to do all this before you turn on your Pi.
Windows 10 (Network Preparation)
(The following instructions are for windows 10. If you're using windows 7, these instructions will be a little different.)
Configure Ethernet Port with Static IP Address
Check an important parameter
Type
regedit
in the windows search bar to open the application Registry EditorOn the opened Registry Editor window, from the left pane, go to HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\SharedAccess\Parameters.
Pay attention to the two IP addresses labeled in red boxes. Ideally, the two IP addresses should be identical. Remember the first three numbers of the IP addresses which will be used in later steps. In my case, it is 192.168.137
Set Static IP
Plug in your Ethernet adapter (if it is not built-in to your computer).
Open "Network Connections" by holding the windows key on your keyboard and typing the "x" key ( Win-x ) and then selecting "Network Connections". Choose "Change Adapter Options".
Right-click your "Ethernet Connection", choose "Properties", then authenticate as an administrator if prompted.
Uncheck the box next to "Internet Protocol Version 6 (TCP/IPv6)".
In the same window, select "Internet Protocol Version 4 (TCP/IPv4)" and click on the "Properties" button.
Choose the option "Using the following IP address". If your observed IP address observed in the previous step is 192.168.137.1, then set the IP here as 192.168.137.4 as shown in the picture. If your value is different, change it accordingly by setting the last number to 4.
Click "OK" to close the window and also close the "Properties" window for Ethernet.
Configure to Share your Laptop's WiFi Internet
- In the same "Network Connections" window opened above, right-click your "Wi-Fi Connection", choose "Properties", then authenticate as an administrator if prompted.
- Uncheck the box next to "Internet Protocol Version 6 (TCP/IPv6)".
- Select the "Sharing" tab, and check the box to allow other network users to connect through your Internet connection. (If you have a choice, be sure "Ethernet Connection" is selected as the "Home networking connection".
- Click "OK" to close the window.
Once you've done this, skip to the next section.
Hook up the Pi!
- Pull the microSD card out of the adapter, and insert it into the Pi.
->
- Connect your Ethernet cable to your laptop and the Pi.
- Plug the AC adapter into a power outlet and into the Pi.
->
- Admire the lights on the circuit board. There should be lights in two places: on the top by the SD card and on the network port. Oooh. Pretty.
- After about 30 seconds, open up Command Prompt on your computer.
Check if the Pi is alive using the command
ping 192.168.137.2
. If you don't see something like the following, it's not connected. (To make it stop, type Control-C.)- Once you've verified the pi is alive, close your command prompt.
Take Control
Now that you know it's connected and alive, it's time to log in! Use SSH to log into the pi. You'll use the same program (SecureCRT, Putty or your favorite SSH client) that you used in Lab 1. To log in,
Choose user "pi" and the default password "raspberry".
Here's what it looks like in Git Bash:
Here's what the new connection screen looks like in SecureCRT:
And what it looks like when you're connected:
NOTE: if you have trouble connecting to your Pi, disconnect the power to your Pi, re-insert the SD card into your laptop, and try adding the "ssh" file again.
Change your Password
Once you're logged in, change your password! Pick a new password for the user "pi" that you will remember.
To do this, you need to use a command called raspi-config
.
pi@rasperrypi ~ $ sudo raspi-config
After typing the command, you'll get a screen with a menu. This is a tool that can do tons of things with your Pi. You may want to explore its options later, but for now we'll focus on just a few.
Choose the first option: "1 System Options", then choose "S3 Password". The system will hide the password as you type it in! This is for security; don't worry, when you press 'enter' it will have the password you typed in.
Once changed, raspi-config will return you to the main menu.
Change your Hostname
In raspi-config
, choose "1 System Options" and "S4 Hostname". You're going to change the name you call the Pi to <rhit username>-pi
. For example, if your Rose-Hulman username is stammsl, change the hostname to stammsl-pi
.
Once changed, raspi-config will return you to the main menu.
Set WiFi Country
In raspi-config
, choose "5 Localisation Options" and "L4 WLAN Country". In the menu, type "U" to quickly find "US" and select it by typing enter.
Once changed, raspi-config will return you to the main menu.
Enable SSH (always)
So you don't have to keep creating that "ssh" file, you want to turn on the SSH server permanently. To enable this, choose "2 Interface Options" from the main raspi-config menu. In the next menu, select "P2 SSH". Answer "Yes" when asked if you would like the SSH server to be enabled.
Once enabled, raspi-config will return you to the main menu.
Expand the Filesystem
The image you installed was intentionally small. If you have a small SD card, it will fit. It also fits big ones! In order to maximize the space on your card, you will again use raspi-config
.
In the main raspi-config menu, select the "6 Advanced Options" item. In the menu that pops up, select "A1 Expand Filesystem".
When set up completes, raspi-config will return you to the main menu. Now use left ("<-") and right ("->") arrow keys to select the "<Finish>" option at the bottom of the menu. Follow the instructions to reboot. After it reboots, log back in using SSH.
Reconnect to the Pi
Once the pi has rebooted, log back in. Use the following command line to log into your Pi.
ssh pi@192.168.137.2
Remember to type in the new password you just set in the previous step.
Register with EIT
We're going to put your Pi directly on the RHIT network soon. In preparation for this, you need to register it with EIT.
When logged in, get the hardware address of the WiFi adapter:
pi@raspberrypi ~ $ cat /sys/class/net/wlan0/address b8:27:eb:df:2f:b7
The output value is your Pi's hardware addresses for the wireless (wlan0) interface. You'll use this value below.
On your laptop, use a web browser to open https://clearpass.rose-hulman.edu/guest/guest_index.php. Log in, then select "Register New Device".
- Fill out the form: "Network Address" is the value you obtained for
wlan0
. In the comment field, write the hostname you set on your pi (e.g.,student-pi
). - Click "Register Device".
(optional) If you would like to be able to plug your Pi into the wired network (say in your room), you also need to register the wired interface
eth0
. Repeat steps 1-4 above, but obtain the hardware address for your ethernet adapter instead ofwlan0
. Your ethernet adapter is likely calledeth0
instead ofwlan0
, but it is possible that it will start withenx
and have a bunch of other characters after it:pi@raspberrypi ~ $ ls /sys/class/net/ eth0 lo wlan0 pi@raspberrypi ~ $ cat /sys/class/net/eth0/address b8:27:eb:3a:7b:e1
or
pi@raspberrypi ~ $ ls /sys/class/net/ enxb827eb3a7be1 lo wlan0 pi@raspberrypi ~ $ cat /sys/class/net/enxb827eb3a7be1/address b8:27:eb:3a:7b:e1
Connect your Pi to the WiFi network.
EIT will recognize your Pi on wireless, but you need to tell the Pi how to get on WiFi. We've created a script for you to use to speed this up. On your pi, type in the following commands:
pi@username-pi ~ $ sudo apt update ... pi@username-pi ~ $ sudo apt install ca-certificates firmware-brcm80211 ... pi@username-pi ~ $ wget tiny.cc/cssepi3-wifi ... pi@username-pi ~ $ sudo bash cssepi3-wifi ...
The script will prompt you for which network you want. Select RHIT-OPEN unless you have a reason to use eduroam. It will ask for your RHIT username (not email) and password (even if you don't select eduroam) so it can authenticate to eduroam if you want to in the future. Follow the instructions and when it's complete, restart your pi.
Once your Pi has restarted (this may take a while), verify it is accessible via wifi.
- Disconnect the network cable from your Pi.
- Open a command prompt or Git Bash.
Using
ping
, check if your Pi is connected to the wireless network. On the wireless network, your Pi's name is<hostname>.wlan.rose-hulman.edu
. For example, mine isstammsl-pi.wlan.rose-hulman.edu
. Use ping to see if you can access the pi using this name:C:\> ping joeschmoe-pi.wlan.rose-hulman.edu Pinging joeschmoe-pi.wlan.rose-hulman.edu (137.112.44.132) with 32 bytes of data: Reply from 137.112.44.132: bytes=32 time=0.087 ms TTL=64 Reply from 137.112.44.132: bytes=32 time=0.063 ms TTL=64 Reply from 137.112.44.132: bytes=32 time=0.093 ms TTL=64 Reply from 137.112.44.132: bytes=32 time=0.093 ms TTL=64 Ping statistics for 137.112.44.132: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 0.063ms, Maximum = 1ms, Average = 0.87ms
If the ping fails, your Pi is not connected to eduroam properly (or your laptop is not connected to the network).
Note: It may take a little while for EIT to finish registering your Pi's hardware address. Be a little patient with this part of the process. If you can't get on the WiFi right away, keep the Pi plugged into your laptop (using Internet Sharing) until the Pi is accessible on wifi (ping looks like above).
Part 2: Practice with the Pi
Now it's time to play around with the pi through a terminal. When the pi is waiting for you to give it instructions, it'll show you a prompt. The prompt looks like this:
pi@username-pi ~ $
This means you are logged in as pi
on a computer called raspberrypi
and you are in the ~
directory (your home directory). The dollar sign ($
) is a visual indication for you to type something.
For the rest of this lab, you're going to use a number of linux commands. Here's a quick reference for some of them:
cd <dir>
: change directory. For example,cd foo
is like opening the folder "foo" and going inside.cd ..
goes back up a directory.ls
: show a list of the files in the current directorycat <file>
: display the contents of a file.
Stage 1: Configure your Pi for on-campus updates
- In order to run things as an administrator (for example, to install programs), you need to use a command called
sudo
. This temporarily turns you into an admin called root. Try it!pi@username-pi ~ $ whoami pi pi@username-pi ~ $ sudo whoami root</li>
Stage 2: Install a command-line web browser
Since your sole access to the pi is via command line, lets install a browser that works in a text-only environment! To do this, we will use `apt`.It will ask you if it's ok to install stuff. Type "y" and hit enter. Once it's installed, launch it and see if you can view the course website! (Hint: hit the "ESC" key to show menus and "q" to quit).pi@username-pi ~ $ sudo apt install links
pi@username-pi ~ $ links http://www.rose-hulman.edu/class/csse/csse132/
Stage 3: Install Git
Next, you need to install the git client so you can get your lab and homework materials. Use apt to install it!*NOTE: your Pi may already have Git installed. If it does, running this command will result in a message such as:*pi@username-pi ~ $ sudo apt install git
git is already the newest version
Stage 4: Use Git
Clone your class repository
The command below will check out your repository. Be sure to replace `Once it's checked out, use the `cd` command to go into the directory, and the `ls` command to show your repository's contents.git clone ssh://<username>@gitter.csse.rose-hulman.edu:/srv/git_repos/2021c-csse132-<username>
pi@username-pi ~ $ cd 2021c-csse132-username/ pi@username-pi ~/2021c-csse132-username $ ls lab01 lab02 lab03 pi@username-pi ~/2021c-csse132-username $
Configure Git
Next, you will have to tell Git your username and email address. Use the `git config` command to do this.(replace "YOUR NAME" with your name)git config --global user.name "YOUR NAME"
(replace "youremail@rose-hulman.edu" with your Rose email address).git config --global user.email youremail@rose-hulman.edu
Create and push a new file.
Use the `cd` command to go into the lab03 folder. Once you're in there, create a new file called "itworks.txt":- type
nano itworks.txt
to create the file and edit it using nano. - Put your name in the file, and write a fact about yourself (for example, "I like cats").
- Save the file in nano (hold the Ctrl key and type the letter O)
- Exit nano (hold The Ctrl key and type the letter X)
Now that you've got the file, commit it to your git repository. First, pull any change from the server:
git pull
Then, you need to tell git about the file:
git add itworks.txt
Commit your changes:
git commit -m "added new file in lab 3"
Once it verifies your commit, it's time to upload your new file to the server.
git push
You've commited your first git file with your raspberry pi! Good job!
Stage 5: Solve the mystery
Use cd
to navigate into your lab03
directory.
Once you're in there, use the ls
command to list that directory's contents.
You'll notice a file that you should read!
Read it using the cat
command and follow its instructions.
When you've solved the mystery, you'll submit your solution to Gradescope (see below).
Finishing the Lab
Submit any electronic files you create to git (from your Pi, using
git add <file>
for each file, thengit commit
andgit push
.). (Probably you already did that for this lab)To submit the rest of this lab, please go to Gradescope and complete the questions. Here is a preview of questions you will be asked:
- Please upload a screenshot of your terminal connected to the Pi and running the CSSE132 webpage through the "links" text-based web browser.
- From the mystery problem, paste here the Linux command that you used to identify owners of a blue Honda with license plate beginning
L337
. - What is the name of the suspect?
Upgrade the software on your Pi using
apt update
andapt upgrade
. This will trigger download of software updates from the software mirror at Rose, and will be lots of downloads. You should save this step for OUTSIDE OF LAB so that the WiFi Access Point in the classroom does not get overloaded. Do this step at the end of lab or later this week. NOTE: only do this next step if you plan to use your pi on campus or on the Rose VPN. We want to use the CSSE update mirrors. This way you won't use your quota to install software. This is two commands: one to download the script, another to run it.pi@username-pi ~ $ wget tiny.cc/cssepi ... pi@username-pi ~ $ sudo bash cssepi ... pi@username-pi ~ $ sudo apt update pi@username-pi ~ $ sudo apt upgrade
Follow the instructions (if it gives you any).
Before you power down the Pi, be sure to tell it to shut down. To shut it down, type:
sudo shutdown now
...and hit enter.
Turn off Internet Sharing on your laptop. Sometimes Windows networking stops working and will force you to reboot your computer if you leave Internet Sharing on and put your computer to sleep.