Difference between revisions of "RaspberryPi:AdvancedConnectivity"

From UCT EE Wiki
Jump to navigation Jump to search
Line 4: Line 4:
  
 
For issues with connectivity, visit [[RaspberryPi:DebuggingConnectivity]]
 
For issues with connectivity, visit [[RaspberryPi:DebuggingConnectivity]]
 
= Static IPs =
 
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 wiki, we assign the Pi an IP address of 192.168.137.15. The reason for this is that when using network sharing 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.
 
 
== Setting a static IP on your Desktop ==
 
=== Windows ===
 
<span class="mw-customtoggle-WindowsIP" style="font-size:small; display:inline-block; "><span class="mw-customtoggletext" data-expandtext="Illuminate" data-collapsetext="Deluminate">[Show/hide]</span></span>
 
<div  id="mw-customcollapsible-WindowsIP" class="mw-collapsible mw-collapsed">
 
To change the IP of your Ethernet port on Windows 10, complete the following steps:
 
 
* Right click on your network option in Windows taskbar
 
* Select“Open Network &amp; Internet Settings”, on the lower right hand side of the screen.
 
* Select “Change Adapted Options”
 
* Right click on the Ethernet Connection and select “Properties”
 
* Select “Internet Protocol Version 4 (TCP/IPv4) and click ”Properties&quot;
 
* Select “Use the following IP address:” and enter in the following options:<br />
 
- IP Address: 192.168.137.1<br />
 
- Subnet Mask: 255.255.255.0
 
* You have successfully changed the IP of the Ethernet card on your computer. It is suggested that you now ensure connectivity by attempting to ping the Pi.
 
 
[[File:windowsIPConfig.png|thumb|none|alt=|The IPv4 configuration screen in Windows 10]]
 
</div>
 
=== Ubuntu ===
 
<span class="mw-customtoggle-UbuntuIP" style="font-size:small; display:inline-block; "><span class="mw-customtoggletext" data-expandtext="Illuminate" data-collapsetext="Deluminate">[Show/hide]</span></span>
 
<div  id="mw-customcollapsible-UbuntuIP" class="mw-collapsible mw-collapsed">
 
To change the IP of your Ethernet port on Ubuntu, complete the following steps:
 
 
* Click the network interface icon on the status bar and select Wired Settings
 
* Click the gear button of the interface you’d like to change
 
* Select the IPv4 Tab, and change the IPv4 method to Manual
 
* Under “Addresses” enter in the following:<br />
 
- IP Address: 192.168.137.1<br />
 
- Subnet Mask: 255.255.255.0
 
* You can leave Gateway and DNS blank
 
 
[[File:Ubuntustaticip.png|thumb|none|alt=|The IPv4 configuration screen in Ubuntu 18.10]]
 
</div>
 
 
== Setting a static IP on the Pi ==
 
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.
 
=== From the boot partition ===
 
<span class="mw-customtoggle-PiStaticBoot" style="font-size:small; display:inline-block; "><span class="mw-customtoggletext" data-expandtext="Illuminate" data-collapsetext="Deluminate">[Show/hide]</span></span>
 
<div  id="mw-customcollapsible-PiStaticBoot" class="mw-collapsible mw-collapsed">
 
<ol>
 
<li><p>Insert the SD card into your computer and navigate to the BOOT partition</p></li>
 
<li><p>Open “cmdline.txt” and append the following to the line (don’t create a new line)</p>
 
<pre> ip=192.168.137.15 </pre>
 
<p>This tells the Raspberry Pi to configure the Ethernet port to use the IP address 192.168.137.15</p></li>
 
<li><p>Enable SSH as per Section [sec:SSH].</p></li>
 
<li><p>You need to configure your PC to use the same subnet as the Pi. To do so, see the information below in the section on Assigning a static IP to the Pi.</p></li></ol>
 
</div>
 
=== In dhcpcd (while the Pi is running)===
 
<span class="mw-customtoggle-PiStaticDHCPCD" style="font-size:small; display:inline-block; "><span class="mw-customtoggletext" data-expandtext="Illuminate" data-collapsetext="Deluminate">[Show/hide]</span></span>
 
<div  id="mw-customcollapsible-PiStaticDHCPCD" class="mw-collapsible mw-collapsed">
 
Once you’ve successfully SSH’d into your Pi, it’s a good idea to configure the networking options in the config files directly. You will need to set a static IP on the computer first if you are SSH'ing through an Ethernet connection.
 
 
Use a text editor such as nano to open /etc/dhcpcd.conf as sudo user, and edit it to the following:
 
 
<pre># Static IP profile for eth0
 
profile static_eth0
 
static ip_address=192.168.137.15/24
 
static routers=192.168.137.1
 
static domain_name_servers=192.168.137.1 8.8.8.8
 
 
# Ethernet interface configuration
 
interface eth0
 
fallback static_eth0
 
 
# Wireless configuration
 
interface wlan0
 
metric 200</pre>
 
</div>
 
  
 
= Providing your Pi with wireless Internet Access =
 
= Providing your Pi with wireless Internet Access =
Line 81: Line 9:
 
There are three possible methods of this that will be presented, each with it’s own advantages and disadvantages.
 
There are three possible methods of this that will be presented, each with it’s own advantages and disadvantages.
  
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.
 
  
 
The third involves connecting to a wireless network. We provide examples for private networks, and Eduroam. Note that if connected using Eduroam, some services may be limited based on ICTS policy, but, for the most part, everything should accessible  
 
The third involves connecting to a wireless network. We provide examples for private networks, and Eduroam. Note that if connected using Eduroam, some services may be limited based on ICTS policy, but, for the most part, everything should accessible  
  
== Using WiFi to Ethernet passthrough to give your Pi internet access ==
 
 
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 ===
 
<span class="mw-customtoggle-PassthroughWindows" style="font-size:small; display:inline-block; "><span class="mw-customtoggletext" data-expandtext="Illuminate" data-collapsetext="Deluminate">[Show/hide]</span></span>
 
<div  id="mw-customcollapsible-PassthroughWindows" class="mw-collapsible mw-collapsed">
 
# Ensure the Pi is unplugged
 
# Right click on your network option in Windows taskbar
 
# Select “Open Network &amp; Internet Settings”, on the lower right hand side of the screen.
 
# Select “Change Adapted Options”
 
# Right click on your WiFi network and select “Properties”
 
# Click the “Sharing” tab, and enable the first checkbox <ref>This setting is what forces us to have to use the subnet 192.168.137.x.</ref>
 
# Select the Ethernet connection that your Pi will be using in the drop down box (Usually just “Ethernet”, but may be different if there are multiple Ethernet posts on your system)
 
 
[[File:Windowspassthrough.png|thumb|none|alt=|Using WiFi to Ethernet passthrough in Windows]]
 
</div>
 
=== Ubuntu ===
 
<span class="mw-customtoggle-PassthroughUbuntu" style="font-size:small; display:inline-block; "><span class="mw-customtoggletext" data-expandtext="Illuminate" data-collapsetext="Deluminate">[Show/hide]</span></span>
 
<div  id="mw-customcollapsible-PassthroughUbuntu" class="mw-collapsible mw-collapsed">
 
# Ensure the Pi is unplugged and turned off.
 
# Open a terminal and run <code>nm-connection-editor</code>
 
# Select the wired connection you’d like to share your WiFi to
 
# Select the IPv4 settings tab
 
# Under Method, select “Shared to other computers”
 
# under the IP addresses, click “Add” and enter in an address of 192.168.137.1, and a netmask of 24
 
# Select “Save”, and close the windows
 
# Plug in the Pi, start an SSH session and see if you can ping google.co.za
 
 
[[File:Nm-connection-editor.png|thumb|none|alt=|nm-connection-editor in Ubuntu 18.10]]
 
</div>
 
== WiFi ==
 
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 <code>wpa_cupplicant.conf</code> file on the boot partition, or while the pi is running, either by editing <code>/etc/wpa/wpa_supplicant.conf</code> directly, or using <code>$ sudo raspi-config</code>. Note that the raspi-config method will not work for Eduroam.
 
 
=== Simple Wifi Connections ===
 
If the Pi is up and running and you have access to it, you can run <code>$ sudo raspi-config</code> and enable WiFi using that menu, or editing <code>/etc/wpa/wpa_supplicant.conf</code> directly using an editor such as nano. However if you don't have access, you can create a  a <code>wpa_supplicant.conf</code> 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:
 
<pre>
 
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>"
 
}
 
</pre>
 
 
=== Connecting to Eduroam ===
 
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.
 
 
<span class="mw-customtoggle-Eduroam" style="font-size:small; display:inline-block; "><span class="mw-customtoggletext" data-expandtext="Illuminate" data-collapsetext="Deluminate">[Show/hide]</span></span>
 
<div  id="mw-customcollapsible-Eduroam" class="mw-collapsible mw-collapsed">
 
==== Raspbian Buster ====
 
'''Note:''' There have been updates to the Buster code base, and roll back of wpasupplicant might not be required. However this is currently unable to be tested due to the lockdown. If someone has the chance to test, please update this page accordingly.
 
These instructions come from [https://www.raspberrypi.org/forums/viewtopic.php?p=1501822#p1501822 here].<br />
 
Unfortunately, when you use bleeding edge technology, you may end up cutting yourself. Raspbian Buster updates <code>wpa_supplicant</code> to a version that doesn’t have support for the authentication method used by Eduroam. So we need to roll back <code>wpa_supplicant</code> to an older version. Ensure your Pi has internet access through a method such as Ethernet passthrough, and run the following:
 
 
<pre>$ sudo apt-get remove wpasupplicant</pre>
 
Edit /etc/apt/sources.list :
 
 
<pre>$ sudo nano /etc/apt/sources.list</pre>
 
And change
 
 
<pre>deb http://raspbian.raspberrypi.org/raspbian/ buster main contrib non-free rpi</pre>
 
to
 
 
<pre>deb http://raspbian.raspberrypi.org/raspbian/ stretch main contrib non-free rpi</pre>
 
Run
 
 
<pre>$ sudo apt-get update
 
$ sudo apt-get install wpasupplicant</pre>
 
This will install the correct version of <code>wpa_supplicant</code>. Check that '''version 2.4''' is installed by running
 
 
<pre>$ wpa_supplicant -v</pre>
 
Now, change the sources file back.
 
 
<pre>$ sudo nano /etc/apt/sources.list</pre>
 
And edit the contents to contain
 
 
<pre>deb http://raspbian.raspberrypi.org/raspbian/ buster main contrib non-free rpi</pre>
 
Finally, run
 
 
<pre>$ sudo apt-get update</pre>
 
To update sources The correct version of <code>wpa_supplicant</code> should now be installed, and you can configure your Pi for WiFi as you would if you were using Raspbian Stretch as explained in below.
 
 
==== Raspbian Stretch ====
 
 
This section provides the instructions on how to configure the Pi to use Eduroam. It is possible to do using the GUI through VNC, but we will not cover that here.
 
 
<ol>
 
<li><p>SSH into your Pi.</p></li>
 
<li><p>Generate a hash for your password. Take note of it as it is needed in a later step</p>
 
<pre>
 
$ echo -n your_uct_password_here | iconv -t utf16le | openssl md4
 
</pre></li>
 
<li><p>Open /etc/wpa_supplicant/wpa_supplicant.conf</p>
 
<pre>$ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf</pre></li>
 
<li><p>Edit it so it looks as follows:</p>
 
<pre>
 
    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
 
    update_config=1
 
    country=ZA
 
 
    network={
 
    ssid=&quot;eduroam&quot;
 
    key_mgmt=WPA-EAP
 
    identity=&quot;studentnumber@wf.uct.ac.za&quot;
 
    password=hash:generated_hash_from_earlier
 
    }
 
    </pre></li>
 
<li><p>Save the file</p></li>
 
<li><p>Open <code>/etc/dhcpcd.conf</code></p>
 
<pre> $ sudo nano /etc/dhcpcd.conf</pre></li>
 
<li><p>Make sure the following lines are in the document:</p>
 
<pre>interface wlan0
 
metric 200
 
</pre></li>
 
<li><p>Reboot your Pi</p></li>
 
<li><p>WiFi Generally takes a little longer to initialize than Ethernet, so give it time. You can see if it’s ready by running</p>
 
<pre>$ ifconfig </pre>
 
<p>And seeing if an IPv4 address (“inet”) has been given.</p></li>
 
<li><p>Test your configuration by pinging from the WiFi interface:</p>
 
<pre>$ ping google.com -I wlan0</pre></li></ol>
 
</div>
 
  
 
= Configuring the Pi to Act as an Access Point =
 
= Configuring the Pi to Act as an Access Point =
Line 291: Line 91:
 
<pre>iptables-restore &lt; /etc/iptables.ipv4.nat</pre></li></ol>
 
<pre>iptables-restore &lt; /etc/iptables.ipv4.nat</pre></li></ol>
 
</div>
 
</div>
 
= TTL over USB =
 
 
'''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)
 
 
<pre>console=serial0,115200 </pre>
 
config.txt
 
 
<pre>uart_enable=1
 
dtoverlay=pi3-disable-bt</pre>
 

Revision as of 12:20, 18 August 2020

Overview

This page covers advanced connectivity options that you can use on the Raspberry Pi. While not always required, they can simplify and streamline the way you work with the Raspberry Pi.

For issues with connectivity, visit RaspberryPi:DebuggingConnectivity

Providing your Pi with wireless Internet Access

There are three possible methods of this that will be presented, each with it’s own advantages and disadvantages.


The third involves connecting to a wireless network. We provide examples for private networks, and Eduroam. Note that if connected using Eduroam, some services may be limited based on ICTS policy, but, for the most part, everything should accessible


Configuring the Pi to Act as an Access Point

If you are hosting a server on the Raspberry Pi, or perhaps want to create a WiFi network for guests to connect to, the Pi can act as an access point. This guide comes from https://www.raspberrypi.org/documentation/configuration/wireless/access-point.md and https://frillip.com/using-your-raspberry-pi-3-as-a-wifi-access-point-with-hostapd/ and will work with a Pi that has an Ethernet Port (though it it possible to mimic an Ethernet port on a Pi 0W over USB. A Pi 0 doesn't have WiFi so this wouldn't serve any purpose).

Note that this WiFi connection will not provide internet access by bridging the Ethernet port (that’s something else entirely), but it works well for hosting services on the Pi, such as a Node-Red server.

[Show/hide]

  1. SSH into the Pi, update, and reboot to ensure updates have taken place

    $ sudo apt-get update
    $ sudo apt-get dist-upgrade
    $ sudo reboot
  2. Wait for the Pi to reboot, and reconnect via SSH. Install hostapd and dnsmasq

    $ sudo apt-get install hostapd dnsmasq
  3. Configure a static IP in dhcpcd

    $ sudo nano /etc/dhcpcd.conf

    Adjust the contents so that the wireless interface is described as follows:

    interface wlan0
    static ip_address=192.168.4.1/24
    nohook wpa_supplicant

    Save and close that file, and restart the dhcp service

    $ sudo systemctl restart dhcpcd
  4. Run the following commands to create save the original dnsmasq, and create a new file which will be edited:

    $ sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
    $ sudo nano /etc/dnsmasq.conf

    Place the following in the now open file:

    interface=wlan0
    listen-address=192.168.4.1
    dhcp-range=192.168.4.2,192.168.4.180,255.255.255.0,24h
    server=8.8.8.8
    domain-needed
    bogus-priv

    Save and close the file

  5. Restart the dnsmasq service

    $ sudo systemctl reload dnsmasq
  6. Configure hostapd. Open up the configuration file:

    sudo nano /etc/hostapd/hostapd.conf

    Place the following configuration in the file. Some assumptions are made about the technical aspects of it, but these are beyond the scope of this course. Note that network name and password do not have quotes around them.

    interface=wlan0
    driver=nl80211
    ssid=TestNetwork
    hw_mode=g
    channel=7
    ieee80211n=1
    wmm_enabled=1
    macaddr_acl=0
    auth_algs=1
    ignore_broadcast_ssid=0
    wpa=2
    wpa_passphrase=TestNetwork
    wpa_key_mgmt=WPA-PSK
    wpa_pairwise=TKIP
    rsn_pairwise=CCMP

    Save and close the file

  7. Next, the system needs to know where to find this configuration file. Open the configuration file:

    $ sudo nano /etc/default/hostapd

    Find the line with “#DAEMON_CONF” and replace it with:

    DAEMON_CONF="/etc/hostapd/hostapd.conf"

    Save and close this file.

  8. Now enable and start hostapd:

    $ sudo systemctl unmask hostapd
    $ sudo systemctl enable hostapd
    $ sudo systemctl start hostapd
  9. Add routing and masquerade by opening sysctl:

    $ sudo nano /etc/sysctl.conf

    And uncomment this line by removing the preceding # symbol:

    net.ipv4.ip_forward=1

    Save and close the file

  10. Add a masquerade, and save the iptables rule:

    $ sudo iptables -t nat -A  POSTROUTING -o eth0 -j MASQUERADE
    $ sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
  11. Edit /etc/rc.local and add this just above “exit 0” to install these rules on boot.

    $ sudo nano /etc/rc.local

    Place the following just above “exit 0” to install these rules on boot

    iptables-restore < /etc/iptables.ipv4.nat