# 1.5 Network Configuration

So far, we are **NOT** able to surf the Internet yet. In this chapter, we are going to enable our Internet by configuring network settings, and then set up a static IP address for your Raspberry Pi.

## 1.5.1 Connect to Internet

Command

```
ifconfig
```

shows the current network configuration.

![Image](https://625702022-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LBY-O3EwoOkuXt6PKFT%2F-LBY-TAnIw7CAUhJHWPK%2F-LBY-UEdcUL1CRTR7yx2%2Fnetwork_1_ifconfig.jpg?generation=1525300729317559\&alt=media)

Obviously, there is **NO** Internet yet, as indicated on the top-right corner.

![Image](https://625702022-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LBY-O3EwoOkuXt6PKFT%2F-LBY-TAnIw7CAUhJHWPK%2F-LBY-UFdn12gxdttjGj9%2Fnetwork_2_internet_not_connected.jpg?generation=1525300730866926\&alt=media)

And, by clicking on the **Wifi** icon, it will open a list of detected **Wifi** networks:

![Image](https://625702022-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LBY-O3EwoOkuXt6PKFT%2F-LBY-TAnIw7CAUhJHWPK%2F-LBY-UH0rVZQiRS8ybya%2Fnetwork_3_internet_listing.jpg?generation=1525300728541846\&alt=media)

You will notice that Raspberry Pi 3B **ONLY** detects the 2.4G Wifi network, which can be confirmed from its [official specification](https://www.raspberrypi.org/products/raspberry-pi-3-model-b/). Clearly, [BCM43438](http://www.cypress.com/file/298076/download) is the chip with

> a 2.4 GHz WLAN IEEE 802.11 b/g/n MAC/baseband/radio, Bluetooth 4.1 support, and an FM receiver.

Click on your network, the **Pre Shared Key** dialog will be popped up for you to input the password, as follows:

![Image](https://625702022-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LBY-O3EwoOkuXt6PKFT%2F-LBY-TAnIw7CAUhJHWPK%2F-LBY-UJuEYk4CrOy5B8d%2Fnetwork_4_internet_pre_sharedkey.jpg?generation=1525300733671843\&alt=media)

After inputing the correct password to your **Wifi**, you will see the Internet is NOW connected:

![Image](https://625702022-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LBY-O3EwoOkuXt6PKFT%2F-LBY-TAnIw7CAUhJHWPK%2F-LBY-UL7BfbvyyJlgyf3%2Fnetwork_5_internet_connected_1.jpg?generation=1525300727068857\&alt=media)

Move your mouse to the **Wifi** icon, you will see the network information about **wlan0**.

![Image](https://625702022-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LBY-O3EwoOkuXt6PKFT%2F-LBY-TAnIw7CAUhJHWPK%2F-LBY-UMIaVyn1JD94h7B%2Fnetwork_6_internet_connected_2.jpg?generation=1525300726490004\&alt=media)

Now, let's have a look at the network configuration by command

```
ifconfig
```

again.

![Image](https://625702022-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LBY-O3EwoOkuXt6PKFT%2F-LBY-TAnIw7CAUhJHWPK%2F-LBY-UNLZ68ACmKZtH8b%2Fnetwork_7_ifconfig.jpg?generation=1525300727109464\&alt=media)

As you can see, the IP address is NOW automatically allocated as **192.168.0.105**.

## 1.5.2 Set Static IP (Optional but Strongly Recommended)

In order to be more easily and accurately locate which device is of which IP address, without shifting to a **NEW** IP address while reconnecting to the **SAME** network, users would like to set static IPs for frequently-used devices.

There are at least three ways to set up a static IP for Raspberry Pi 3B.

**I. Router**

Specifically for ther author's Cisco router, there is a place to specify **Pre-assigned DHCP IP Address**.

![Image](https://625702022-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LBY-O3EwoOkuXt6PKFT%2F-LBY-TAnIw7CAUhJHWPK%2F-LBY-UPuVUbMaVFNazgj%2Frouter_configure_1.jpg?generation=1525300733738368\&alt=media)

After this **Pre-assigned DHCP IP Address** dialog is opened, we **Add Static IP**, then **Save**. Finally, we will be able to see the **Success** dialog.

![Image](https://625702022-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LBY-O3EwoOkuXt6PKFT%2F-LBY-TAnIw7CAUhJHWPK%2F-LBY-USJZPA4zINiof_t%2Frouter_configure_2.jpg?generation=1525300731533896\&alt=media)

![Image](https://625702022-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LBY-O3EwoOkuXt6PKFT%2F-LBY-TAnIw7CAUhJHWPK%2F-LBY-UTTxAP-sry76aVK%2Frouter_configure_3.jpg?generation=1525300728552065\&alt=media)

![Image](https://625702022-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LBY-O3EwoOkuXt6PKFT%2F-LBY-TAnIw7CAUhJHWPK%2F-LBY-UVVsBbN3064uRit%2Frouter_configure_4.jpg?generation=1525300734044817\&alt=media)

After the static IP configuration from within the router, we reconnect(disconnect first and then connect again) Raspberry Pi 3B to Internet, we'll be able to see the IP address has already changed accordingly.

However, to think of one additional issue? If the router is rebooted for some reason, all pre-assigned IP will be forgot by the router, what are you going to do?

Yes. The answer is: do some configuration on the Raspberry Pi 3B, instead of router.

**II. Configure /etc/dhcpcd.conf**

Refer to **ModMyPi** online blog [How to give your Raspberry Pi a Static IP Address - UPDATE](https://www.modmypi.com/blog/how-to-give-your-raspberry-pi-a-static-ip-address-update)

**/etc/dhcpcd.conf** is supposed to be configured as:

```
interface eth0
static ip_address=192.168.0.31/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1 8.8.8.8

interface wlan0
static ip_address=192.168.0.30/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1 8.8.8.8
```

**III. Configure /etc/network/interfaces and /etc/wpa\_supplicant/wpa\_supplicant.conf**

Refer to **ModMyPi** online tutorial [Tutorial - How to give your Raspberry Pi a Static IP Address](https://www.modmypi.com/blog/tutorial-how-to-give-your-raspberry-pi-a-static-ip-address)

For **/etc/network/interfaces**, the following snippet of code is to be added at the bottom:

```
auto eth0
iface eth0 inet static
    address 192.168.0.31
    netmask 255.255.255.0
    gateway 192.168.0.1

allow-hotplug wlan0
iface wlan0 inet static
    address 192.168.0.30
    netmask 255.255.255.0
    gateway 192.168.0.1
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
```

For **/etc/wpa\_supplicant/wpa\_supplicant.conf**, the default network is needed to configure as follows:

```
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=CA

network={
    ssid="Wifi Network SSID"
    psk="Password Phrase"
    key_mgmt=WPA-PSK
}
```

respectively.

A lot more discussion can be found in Raspberry Pi Official Forum, topic title [Raspberry Pi 3 Wifi Static IP](https://www.raspberrypi.org/forums/viewtopic.php?f=91\&t=157250).

Here in our case, **/etc/network/interfaces** and **/etc/wpa\_supplicant/wpa\_supplicant.conf** are modified accordingly, while **/etc/dhcpcd.conf** is left as it is.

After the network is configured, `ifconfig` brings a **NEW** IP address: **192.168.0.30**.

![Image](https://625702022-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LBY-O3EwoOkuXt6PKFT%2F-LBY-TAnIw7CAUhJHWPK%2F-LBY-UtVhbpMGu249Oa4%2Fnetwork_8_ifconfig.jpg?generation=1525300732403258\&alt=media)

**What's WEIRD**

**Two weird things** are: 1. If **/etc/dhcpcd.conf** is configured, **Raspberry Pi 3B** with **Raspbian Stretch** **CANNOT** connect to the Internet any longer.

1. If **/etc/network/interfaces** and **/etc/wpa\_supplicant/wpa\_supplicant.conf** are configured, **Raspberry Pi 3B** with **Raspbian Stretch** is **ABLE** to connect to the Internet, but with the top right corner **Wifi sign** as **Unconnected** or **Disconnected**:

![Image](https://625702022-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LBY-O3EwoOkuXt6PKFT%2F-LBY-TAnIw7CAUhJHWPK%2F-LBY-V43TG4J1gyp8GZu%2Fnetwork_9_ping_unconnected.jpg?generation=1525300730351652\&alt=media)
