Skip to content

Internet Connection

Luis Rivera edited this page Feb 24, 2020 · 37 revisions

Internet Connection

This section covers how-to set up and connect to the internet on your Raspberry Pi.

Ethernet Connection

Requirements:

  • Internet access (router)
  • An Ethernet cable

Steps to connect:

  1. Make sure that your router is connected to the internet.
  2. Connect one (1) Ethernet cable end to your Raspberry Pi and the other end to your router.
  3. Access https://github.com/gitluis/rpi-cluster through the web browser.
  4. If page shows up, you have successfully connected your board to the internet.
  5. Done!

For troubleshooting possible Ethernet connectivity issues, refer to How to Fix Your Internet Connection: 15 Steps (with Pictures).

WiFi Connection

If you are using the command-line interface, refer to the next section for details on how to set up WiFi through the command line.

If you are using the Raspberry Pi Desktop or another Desktop Environment, you can set up wireless networking by going to the network icon at the right-hand end of the menu bar. See image reference here.

For troubleshooting possible wireless connectivity issues, refer to Raspberry Pi Foundation's wireless connectivity guide.

Setting WiFi Network via the Command Line Interface (CLI)

How-to configure WiFi Protected Access (WPA) based wireless network interfaces manually on Linux-based operating systems.

Using raspi-config

  1. Run sudo raspi-config
  2. Select the Network Options item from the menu
  3. Select Wi-fi option
  4. Set the network SSID and the passphrase for the network

If you do not know the SSID of the network, the next section details how to list all available networks prior to running raspi-config command.

Getting WiFi network details

To scan and list all available WiFi networks along with other useful information, run the following command in a terminal window

sudo iwlist wlan0 scan

Retrieve the following information from your WiFi Network:

  • 'ESSID:"testing" which is the name of the WiFi network
  • 'IE:IEEE 802.11i/WPA2 Version 1' which is the authentication method that such network uses
  • The password for the wireless network (commonly found in the back of the router)

Try using raspi-config after writing down the network details. If raspi-config fails to connect the Pi to the desired network, the next section covers how to manually add such network to the Pi and connect to it.

Adding network details to Raspberry Pi

Create a wpa-supplicant configuration file

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

Generate an encrypted pre-shared key (psk)

wpa_passphrase "<network ssid>" >> /etc/wpa_supplicant/wpa_supplicant.conf

Enter the network SSID and WPA password or keyphrase then hit Enter again.

Here is how wpa_supplicant.conf should look like so far:

network={
    ssid="network ssid"
    psk="encrypted pre-shared key"
    key_mgmt=WPA-PSK
}

Add the following information at the top of the wpa_supplicant.conf file

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=<country-code-here>

Refer to en.wikipedia.org/wiki/ISO_3166-1 for your specific country code.

Re-configure the network interface

sudo wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf
wpa_cli -i wlan0 reconfigure

Verify whether the Raspberry Pi has successfully connected to the internet using:

ifconfig wlan0

If inet addr displays an IP address beside it, the Raspberry PI is connected to the internet. Otherwise, please refer to the Raspberry Pi Foundation's guide on how to connect to WiFi via command line.

Network configuration using Netplan

As of Ubuntu 18.04 LTS, ubuntu has switched to Netplan for configuring network interfaces. Netplan is a YAML-based configuration system that makes the process of configuring a network simple and very straightforward.

Install Netplan

For those not using Ubuntu 18.04 LTS, you will have to manually install netplan

sudo apt update -y
sudo apt install netplan

By default, it may be disabled, thus we have to enable it

echo "ENABLED=1" | sudo tee /etc/default/netplan

For Netplan, we will use the NetworkManager renderer. If you are unsure whether network manager service is installed in your computer, try the following:

sudo apt install network-manager

Reboot the system

sudo reboot

How can I install Netplan without an internet connection?

Keep in mind that if you are not using Ubuntu 18.04 LTS which comes with Netplan installed by default then you will have to install Netplan first. However, if you are not or cannot connect to the internet via Ethernet, how will you download and install Netplan?

For such specific cases I would recommend attempting to configure your network with the previous guides (above). If none of them worked out for you, I would suggest to stick to Raspbian with Desktop in which network can be set up easily or switch to Ubuntu 18.04 LTS or a newer version.

Getting network details

List all network interfaces

ip a

You typically have two (2) or more network interfaces:

  • First one is always lo which stands for loopback interface. This is a network interface that identifies the device and points to the local IP address of your system, most well known as 127.0.0.0/8 and also called localhost.
  • The second is the network interface we're looking for to configure whose name may vary between eth0, ens33, among others depending on your operating system.

Set up network via DHCP

To set up a network interface named eth0 via Ethernet to get an automatically IP address assigned via DHCP, create a eth0-config.yaml configuration file with the following:

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: true

Now refer to Network configuration to apply the new configuration.

Set up a static network

To set up a network interface named eth0 via Ethernet to have a static IP address assigned to it, create a eth0-config.yaml configuration file with the following:

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      addresses:
        - 192.168.0.25/24
      gateway4: 192.168.0.1
      nameservers:
        addresses: [192.168.0.1, 8.8.8.8]

For your IP address mask value, e.g. your Subnet Mask is 255.255.255.0 then the IP address mask is xxx.xxx.xxx.xxx/24 as specified above, refer to this guide here for more details.

Now refer to Network configuration to apply the new configuration.

Set up an open wireless network (WiFi)

To set up a network interface named wlan0 and connect to a open wireless connection (public WiFi), create a wlan0-config.yaml configuration file with the following:

network:
  version: 2
  wifis:
    wlan0:
      access-points:
        opennetwork: {}
      dhcp4: yes

Now refer to Network configuration to apply the new configuration.

Set up a WPA-based wireless network (WiFi)

To set up a network interface named wlan0 and connect to a private wireless connection (WPA/WPA2 WiFi), create a wlan0-config.yaml configuration file with the following:

network:
  version: 2
  renderer: NetworkManager
  wifis:
    wlan0:
      dhcp4: no
      dhcp6: no
      addresses: [192.168.0.25/24]
      gateway4: 192.168.0.1
      nameservers:
        addresses: [192.168.0.1, 8.8.8.8]
      access-points:
        "network_ssid_name":
          password: "**********"

For your IP address mask value, e.g. your Subnet Mask is 255.255.255.0 then the IP address mask is xxx.xxx.xxx.xxx/24 as specified above, refer to this guide here for more details.

Don't forget to replace "network_ssid_name" with your network SSID and "**********" with your network's passphrase key or pre-shared key password.

Now refer to Network configuration to apply the new configuration.

Network configuration

Save your configuration files under /etc/netplan/ with the .yaml extension, e.g. /etc/netplan/eth0-config.yaml.

Apply the new configuration

sudo netplan apply

The configuration will be written to disk under /etc/netplan/ and will persist between reboots if accepted and properly configured.

Reboot the system

sudo reboot