RaspberryPi:AdvancedConnectivity

From UCT EE Wiki
Revision as of 09:42, 18 August 2020 by CRNKEE002 (talk | contribs)
Jump to navigation Jump to search

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

Static IPs

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

[Show/hide]

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:

- IP Address: 192.168.137.1
- 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.
The IPv4 configuration screen in Windows 10

Ubuntu

[Show/hide]

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:

- IP Address: 192.168.137.1
- Subnet Mask: 255.255.255.0

  • You can leave Gateway and DNS blank
The IPv4 configuration screen in Ubuntu 18.10

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

[Show/hide]

  1. Insert the SD card into your computer and navigate to the BOOT partition

  2. Open “cmdline.txt” and append the following to the line (don’t create a new line)

     ip=192.168.137.15 

    This tells the Raspberry Pi to configure the Ethernet port to use the IP address 192.168.137.15

  3. Enable SSH as per Section [sec:SSH].

  4. 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]

In dhcpcd

[Show/hide]

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:

# 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

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

  1. Ensure the Pi is unplugged
  2. Right click on your network option in Windows taskbar
  3. Select “Open Network & Internet Settings”, on the lower right hand side of the screen.
  4. Select “Change Adapted Options”
  5. Right click on your WiFi network and select “Properties”
  6. 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>
  7. 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)
Using WiFi to Ethernet passthrough in Windows

Ubuntu

  1. Ensure the Pi is unplugged and turned off.
  2. Open a terminal and run nm-connection-editor
  3. Select the wired connection you’d like to share your WiFi to
  4. Select the IPv4 settings tab
  5. Under Method, select “Shared to other computers”
  6. under the IP addresses, click “Add” and enter in an address of 192.168.137.1, and a netmask of 24
  7. Select “Save”, and close the windows
  8. Plug in the Pi, start an SSH session and see if you can ping google.co.za
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 here.
Unfortunately, when you use bleeding edge technology, you may end up cutting yourself. Raspbian Buster updates wpa_supplicant to a version that doesn’t have support for the authentication method used by Eduroam. So we need to roll back wpa_supplicant to an older version. Ensure your Pi has internet access through a method such as Ethernet passthrough, and run the following:

$ sudo apt-get remove wpasupplicant

Edit /etc/apt/sources.list :

$ sudo nano /etc/apt/sources.list

And change

deb http://raspbian.raspberrypi.org/raspbian/ buster main contrib non-free rpi

to

deb http://raspbian.raspberrypi.org/raspbian/ stretch main contrib non-free rpi

Run

$ sudo apt-get update
$ sudo apt-get install wpasupplicant

This will install the correct version of wpa_supplicant. Check that version 2.4 is installed by running

$ wpa_supplicant -v

Now, change the sources file back.

$ sudo nano /etc/apt/sources.list

And edit the contents to contain

deb http://raspbian.raspberrypi.org/raspbian/ buster main contrib non-free rpi

Finally, run

$ sudo apt-get update

To update sources The correct version of wpa_supplicant 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.

  1. SSH into your Pi.

  2. Generate a hash for your password. Take note of it as it is needed in a later step

    $ echo -n your_uct_password_here | iconv -t utf16le | openssl md4
    
  3. Open /etc/wpa_supplicant/wpa_supplicant.conf

    $ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
  4. Edit it so it looks as follows:

        ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
        update_config=1
        country=ZA
    
        network={
        ssid="eduroam"
        key_mgmt=WPA-EAP
        identity="studentnumber@wf.uct.ac.za"
        password=hash:generated_hash_from_earlier
        }
        
  5. Save the file

  6. Open /etc/dhcpcd.conf

     $ sudo nano /etc/dhcpcd.conf
  7. Make sure the following lines are in the document:

    interface wlan0
    metric 200
    
  8. Reboot your Pi

  9. WiFi Generally takes a little longer to initialize than Ethernet, so give it time. You can see if it’s ready by running

    $ ifconfig 

    And seeing if an IPv4 address (“inet”) has been given.

  10. Test your configuration by pinging from the WiFi interface:

    $ ping google.com -I wlan0

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/.

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.

  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

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)

console=serial0,115200 

config.txt

uart_enable=1
dtoverlay=pi3-disable-bt