Difference between revisions of "RaspberryPi:AdvancedConnectivity"

From UCT EE Wiki
Jump to navigation Jump to search
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
[[Category:RaspberryPi]]
 
[[Category:RaspberryPi]]
 
= Overview =
 
= 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]]
+
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.  
  
= Static IPs =
+
For issues with connectivity, visit [[RaspberryPi:DebuggingConnectivity]]
Setting a static IP makes it easy to access the Pi. 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 ===
 
 
 
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 & 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"
 
* 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]]
 
 
 
=== Ubuntu ===
 
 
 
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]]
 
 
 
== Setting a static IP on the Pi ==
 
=== From the boot partition ===
 
<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 Section [sec:Connectivity-ChangeComputerIP]</p></li></ol>
 
 
 
=== In dhcpcd ===
 
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>
 
 
 
= Providing your Pi with wireless Internet Access =
 
 
 
There are two possible methods of this that will be presented, each with it’s own advantages and disadvantages.
 
 
 
The first 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 second involves connecting to a wireless network. In the example we give you, we're only going to add a connection to Eduroam. While you could host other services on the Pi when using WiFi connectivity, it would require some access to port forwarding and other things that ICTS unfortunately won’t allow.
 
 
 
== Using WiFi to Ethernet passthrough to give your Pi internet access ==
 
 
 
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. If you’re connected to Windows, you can use network sharing. Complete the following to enable network sharing:
 
 
 
=== Windows ===
 
# 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]]
 
 
 
=== Ubuntu ===
 
# 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]]
 
 
 
== Eduroam Connectivity ==
 
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.
 
=== Raspbian Buster (Raspbian Site) ===
 
'''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 acoordingly.
 
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 (Vula) ===
 
 
 
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>
 
  
 
= Configuring the Pi to Act as an Access Point =
 
= 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/.
+
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.
 
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.
  
 +
<span class="mw-customtoggle-AP" 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-AP" class="mw-collapsible mw-collapsed">
 
<ol>
 
<ol>
 
<li><p>SSH into the Pi, update, and reboot to ensure updates have taken place</p>
 
<li><p>SSH into the Pi, update, and reboot to ensure updates have taken place</p>
Line 249: Line 81:
 
<p>Place the following just above “exit 0” to install these rules on boot</p>
 
<p>Place the following just above “exit 0” to install these rules on boot</p>
 
<pre>iptables-restore &lt; /etc/iptables.ipv4.nat</pre></li></ol>
 
<pre>iptables-restore &lt; /etc/iptables.ipv4.nat</pre></li></ol>
 
+
</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>
 

Latest revision as of 12:20, 18 August 2020

Overview[edit]

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

Configuring the Pi to Act as an Access Point[edit]

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