Difference between revisions of "RaspberryPi:Networking"
(4 intermediate revisions by the same user not shown) | |||
Line 105: | Line 105: | ||
Ethernet is the physical connection made using a CAT 5 or 6 cable - though there are ways that Ethernet can be mimicked over USB (see Details on the Pi 0 and 0W for more information). | Ethernet is the physical connection made using a CAT 5 or 6 cable - though there are ways that Ethernet can be mimicked over USB (see Details on the Pi 0 and 0W for more information). | ||
− | If you're on a private network, connecting your Pi will usually just assign it an IP address and internet connectivity automatically works. If you're on a linux-based system, like Ubuntu, you can use the hostname of the Pi to connect. By default this is "raspberrypi", so you could SSH in using "SSH pi@raspberrypi". However, in most cases, you'll want to know the IP address and assign a static IP address. | + | There are two sorts of Ethernet Connectivity we will consider. The first is just connecting the Pi to a known Ethernet network. It is simple as it requires no configuration, but it means you will need to use other methods to ensure the network allows SSH traffic and to ensure you can determine the IP of the Pi. |
+ | |||
+ | The second is using Ethernet passthrough from your computer to the Raspberry Pi. This leaves the WiFi free to host your own access point, and you can host services such as a Node-Red server or media center on the Pi. | ||
+ | |||
+ | If you're on a private network, connecting your Pi to a router will usually just assign it an IP address and internet connectivity automatically works. If you're on a linux-based system, like Ubuntu, you can use the hostname of the Pi to connect. By default this is "raspberrypi", so you could SSH in using "SSH pi@raspberrypi". However, in most cases, you'll want to know the IP address and assign a static IP address. | ||
== Static IPs == | == Static IPs == | ||
Line 234: | Line 238: | ||
== Using USB connectivity on the Pi 0/0W == | == Using USB connectivity on the Pi 0/0W == | ||
+ | In order to use this method on Windows you require [https://support.apple.com/kb/DL999?locale=en_US Apple Bonjour Print Services] as well as [https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html PuTTY]. | ||
+ | |||
+ | With the SD card plugged into your computer, open up the boot partition and make the following changes: | ||
+ | <ol> | ||
+ | <li>Be sure to enable SSH as described at [[RaspberryPi:Networking#Enabling_SSH]]</li> | ||
+ | <li>At the bottom of config.txt, add the following line | ||
+ | <pre>dtoverlay=dwc2</pre></li> | ||
+ | <li>On the '''same line''' in cmdline.txt, append the following: | ||
+ | <pre> modules-load=dwc2,g_ether</pre></li> | ||
+ | </ol> | ||
+ | |||
+ | Open up a terminal window (or PuTTY if you're on Windows). | ||
+ | After about 90 or so seconds, you should be able to SSH in (assuming you enabled SSH on the Pi) by connecting to <code>raspberrypi.local</code>. | ||
+ | You may (if you're using PuTTY) get a message about keys and hashes and hosts - just click "yes". | ||
+ | You can then log in with the username "pi" and the password "raspberry". | ||
+ | |||
+ | === Internet Access over USB === | ||
+ | If you don't have WiFi connectivity, you can send an internet connection from your computer over USB. | ||
+ | |||
+ | In order to provide the Pi with internet access, you need to do the following: | ||
+ | <ol> | ||
+ | <li> Share your computer's connection with the Pi as you have done in the "WiFi to Ethernet Passthrough" section above. You will be sharing your regular connection with an Ethernet connection, as that is how the Pi 0 has been set up. In the test I ran <li> | ||
+ | <li> On the Pi, you need to configure DHCPCD to have usb0 use a static IP of 192.168.137.x. The rest of this wiki has been written with x=15 | ||
+ | <li> SSH in using "raspberrypi.local" as the address to connect to </li> | ||
+ | <li> Run <code> sudo nano /etc/dhcpcd.conf</code>, scroll to the bottom, and add the following: | ||
+ | <pre> | ||
+ | interface usb0 | ||
+ | static ip_address=192.168.137.15 | ||
+ | static router = 192.168.137.1 | ||
+ | static domain_name_servers=192.168.137.1 8.8.8.8 | ||
+ | </pre></li> | ||
+ | <li> Save and close the file </li> | ||
+ | <li> Run the command <code> sudo route add default gw 192.168.137.1 </code> </li> | ||
+ | <li> Reboot the Pi </li> | ||
+ | <li> SSH in again. You should be able to ssh in using "192.168.137.15" now, as well as "raspberrypi.local" </li> | ||
+ | <li> To test connectivity, try pinging google.com. You should get a valid response back. </li> | ||
+ | </ol> | ||
= Ensuring Connectivity = | = Ensuring Connectivity = |
Latest revision as of 12:13, 4 September 2020
There are many ways to interface with the Pi. This page will cover types of network connectivity. For more comprehensive, advanced networking details, see RaspberryPi:AdvancedConnectivity.
Contents
A Brief Overview of Networks[edit]
It is useful to have a basic idea of how networks, IP addresses, and subnets work. For this, it it suggested you read this article from Microsoft.
Wireless Connectivity[edit]
WiFi connectivity is a useful way of connecting the Pi to the internet as it enables more portability. You can adjust WiFi Connections either before boot, by creating a wpa_cupplicant.conf
file on the boot partition, or while the pi is running, either by editing /etc/wpa/wpa_supplicant.conf
directly, or using $ sudo raspi-config
. Note that the raspi-config method will not work for Eduroam.
Simple Wifi Connections[edit]
If the Pi is up and running and you have access to it, you can run $ sudo raspi-config
and enable WiFi using that menu, or editing /etc/wpa/wpa_supplicant.conf
directly using an editor such as nano. However if you don't have access, you can create a a wpa_supplicant.conf
file on the SD card, on the boot partition while it is plugged into another system.
If editing the wpa_supplicant configuration file, the contents should look something like the following:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 country=ZA network={ ssid="<Name of your WiFi network>" psk="<Password for your WiFi network>" }
Connecting to Eduroam[edit]
Connecting to Eduroam requires a fair amount of configuration which depends on your operating system. Generally though, edits to wpa_supplicant.conf and dhcpcd.conf are sufficient. Note that there are new releases of Raspberry Pi OS, and it is unknown if the edits for Buster are still required.
[Show/hide]
Ethernet Connectivity[edit]
Ethernet is the physical connection made using a CAT 5 or 6 cable - though there are ways that Ethernet can be mimicked over USB (see Details on the Pi 0 and 0W for more information).
There are two sorts of Ethernet Connectivity we will consider. The first is just connecting the Pi to a known Ethernet network. It is simple as it requires no configuration, but it means you will need to use other methods to ensure the network allows SSH traffic and to ensure you can determine the IP of the Pi.
The second is using Ethernet passthrough from your computer to the Raspberry Pi. This leaves the WiFi free to host your own access point, and you can host services such as a Node-Red server or media center on the Pi.
If you're on a private network, connecting your Pi to a router will usually just assign it an IP address and internet connectivity automatically works. If you're on a linux-based system, like Ubuntu, you can use the hostname of the Pi to connect. By default this is "raspberrypi", so you could SSH in using "SSH pi@raspberrypi". However, in most cases, you'll want to know the IP address and assign a static IP address.
Static IPs[edit]
Setting a static IP makes it easy to access the Pi and ensures that the IP address of the Pi is always known. In this course, we assign the Pi an IP address of 192.168.137.15. The reason for this is that when using Ethernet Passthrough on Windows, Windows asserts a domain of 192.168.137.x, with the host PC being assigned 192.168.137.1. Keep in mind that all devices will need to be on the same subnet though, so if you're assigning a static IP to be used on your own Ethernet network, you need to know what the subnet of that network is.
If you're connecting your Pi through your desktop, you'll need to assign a static IP to the desktop, too.
Setting a static IP on your Desktop[edit]
If you're connecting your Pi through your desktop, you'll need to assign a static IP to the desktop, too. If you're setting a static IP on a Pi that's already connected directly to the network through a router or similar network switch, you can ignore setting a static IP on the computer.
Windows[edit]
[Show/hide]
Ubuntu[edit]
[Show/hide]
Setting a static IP on the Pi[edit]
To set a static IP on the Pi, you have multiple options. You can do it before first boot by addling a line to cmdline.txt on the boot partition of the SD card, or once the Pi is running by editing dhcpcd. Before setting the static IP, ensure you have the correct subdomain, and that you don't assign an IP that another device on the network is using. If you're assinging it a static IP and you have, for example, a router acting as a DHCP server, ensure the IP you assign is outside of DHCP range.
From the boot partition[edit]
[Show/hide]
In dhcpcd (while the Pi is running)[edit]
[Show/hide]
WiFi to Ethernet Passthrough[edit]
In a situation when you can't or don't want to connect to a WiFi network, but are connected to a computer through an Ethernet port There may be a situation in which you want your Pi to work as an access point rather than using the WiFi interface to provide the Pi with internet access. In this situation, you need to get internet access through the Ethernet port. Before starting, you need to ensure the computer you're using has a static IP assigned. When enabling network sharing on Windows, it automatically assigns 192.168.137.1. Once that's done, complete the following to enable network sharing:
Windows[edit]
[Show/hide]
Ubuntu[edit]
[Show/hide]
Other Connectivity Methods[edit]
There are ways other than WiFi and Ethernet as a means of connecting headlessly (without a keyboard and screen directly attached) to the Pi.
TTL over USB[edit]
Note: The Pi uses 3.3V logic levels. Using a 5V logic module will permanently damage the pins.
This option allows you to use a USB to UART converter such as a FT232R or CP2102. Begin by removing the SD card, and insert it into a computer. Make the following changes on the boot partition:
cmdline.txt: add the following (on the same line)
console=serial0,115200
config.txt
uart_enable=1 dtoverlay=pi3-disable-bt
Using USB connectivity on the Pi 0/0W[edit]
In order to use this method on Windows you require Apple Bonjour Print Services as well as PuTTY.
With the SD card plugged into your computer, open up the boot partition and make the following changes:
- Be sure to enable SSH as described at RaspberryPi:Networking#Enabling_SSH
- At the bottom of config.txt, add the following line
dtoverlay=dwc2
- On the same line in cmdline.txt, append the following:
modules-load=dwc2,g_ether
Open up a terminal window (or PuTTY if you're on Windows).
After about 90 or so seconds, you should be able to SSH in (assuming you enabled SSH on the Pi) by connecting to raspberrypi.local
.
You may (if you're using PuTTY) get a message about keys and hashes and hosts - just click "yes".
You can then log in with the username "pi" and the password "raspberry".
Internet Access over USB[edit]
If you don't have WiFi connectivity, you can send an internet connection from your computer over USB.
In order to provide the Pi with internet access, you need to do the following:
- Share your computer's connection with the Pi as you have done in the "WiFi to Ethernet Passthrough" section above. You will be sharing your regular connection with an Ethernet connection, as that is how the Pi 0 has been set up. In the test I ran
- On the Pi, you need to configure DHCPCD to have usb0 use a static IP of 192.168.137.x. The rest of this wiki has been written with x=15
- SSH in using "raspberrypi.local" as the address to connect to
- Run
sudo nano /etc/dhcpcd.conf
, scroll to the bottom, and add the following:interface usb0 static ip_address=192.168.137.15 static router = 192.168.137.1 static domain_name_servers=192.168.137.1 8.8.8.8
- Save and close the file
- Run the command
sudo route add default gw 192.168.137.1
- Reboot the Pi
- SSH in again. You should be able to ssh in using "192.168.137.15" now, as well as "raspberrypi.local"
- To test connectivity, try pinging google.com. You should get a valid response back.
Ensuring Connectivity[edit]
Sometimes you may want to debug your connection to the Pi. A fast way to do this is via the ping command. Ping sends a packet to a particular host (in this case the Pi), and measures the time taken for a response from that host.
To use the ping command, open a command prompt window or terminal and type the following:
$ ping 192.168.137.15
If that host is unreachable (the Pi hasn’t booted yet or is incorrectly configured), a message will show that the host is unreachable. If everything was correctly configured, you should get
Reply from 192.168.137.15: bytes=32 time<1ns TTL=64
This means your Pi and computer are both correctly configured. See section [sec:SSH] for configuring your Pi for SSH access.
NB: Don’t be surprised if you can’t ping a Windows machine from your Pi. Windows blocks the specific type of packet required for a ping in the firewall.
Networking Protocols[edit]
For a more detailed overview on useful networking protocols, see Network Protocols
SSH[edit]
Enabling SSH[edit]
If you have not connected to your Pi and configured it for SSH, you need to do so. SSH is disabled by default on new installations of Raspbian.
To enable SSH, add it as an enabled service in the raspi-config
menu. If you do not have access to the Pi as yet, do the following:
- Insert the SD card into a computer
- Navigate to the BOOT partition
- Create a file called “ssh”. Note there is no file extension!
- Your Pi will enable SSH upon next boot
Using SSH[edit]
To use SSH on your Pi, you need to connect to the computer to a network. See Section [sec:NetworkingOnThePi] on various ways that can be done (it is suggested to use Ethernet upon first connection).
Once your Pi is connected to the computer and you have ensured connection (see Section [sec:Connectivity-EnsuringConnectivity]), use can log in to your Pi via SSH. If you are on a Linux-based system, such as Ubuntu or Mac, you should be able to run the following. Note that the default username is “pi” and the default password is “raspberry”.
$ ssh <username>@192.168.137.15
If you are on Windows, you may need to use PuTTY. Some instances of windows have SSH in the command line, and you can run the command shown above. But if not, you will need to do the following:
- Open PuTTY
- In the “Hostname” field, enter in “192.168.137.15”
- Click “Open”. A terminal window will be opened. If it is the first time you’re SSH’ing into your Pi on this particular computer, you will be asked about the server fingerprint. Click “Yes” to continue.
- You will be asked for a username and password. The default username is “pi” and the password is “raspberry”.
- You should now successfully connected to your Raspberry Pi via SSH
VNC[edit]
To learn about how to install VNC, see
- Ensure the VNC service is enabled on the Pi
- Connect to the Pi through VNC.
- Set your preferred resolution for Raspbian
- In the desktop menu, go to Preferences - Raspberry Pi Configuration and click the “Set Resolution” button.
- Select a more appropriate resolution (1280*720 suggested)
- Select “Okay” and then “Okay”. You will be asked to reboot your Pi, do so.
- Set the VNC resolution
- Through editing /boot/config.txt, uncomment the following lines relating to framebuffer_width and framebuffer_height
- On the desktop on VNC, use the settings. Do this if you have already connected to VNC. This is a little more difficult as it required you to play with windows in order to see the buttons you need.