- Raspberry Pi Zero Cookbook
- Edward Snajder
- 1329字
- 2021-07-09 19:12:11
Setting Up dynamic and static IP address for RPZ
Now that we have our Raspberry Pi Zero on the network, let's look a little closer at what we can do with the networking. Everything should have connected to your Wi-Fi network thanks to DHCP (Dynamic Host Allocation Protocol). DHCP takes incoming requests from devices to be added to the network. It has a pool of IP addresses on the network that it will "lease" to the device for a period of time. After the lease expires, the device may renew (and, depending on the DHCP server rules, will get a new or the same address), or the address will be returned to the available pool. This makes it easy to add machines to a network without keeping track of inpidual addresses assigned to devices. When your Raspberry Pi Zero connected to your Wi-Fi network, by default, it would have been assigned at least one IP address: an IPv4 address (four sets of numbers between 0 and 255, as in 192.168.17.250
), and/or a newer IPv6 address (8 sets of hexadecimal numbers between 0 and FFFF, as in fda5:eec5:fae1: fda5:eec5:fae1:ffff
).
Note
An IP address works very much like a phone number-any device on your home network or any device that can be seen from the Internet will have an IP address associated with it. On the Internet, the device will have a unique IP, much like a phone number. Just like every friend you have has a different phone number, every device has its own IP address. Internal, or private, networks work like phone number extensions inside an office. There may be one number you dial to get to the office, but inside, each employee might get their own internal number.
The DNS (domain name system) is much like an Internet phone book. It ties an address, such as www.yahoo.com , to an IP address, such as 50.51.200.222
. This way, humans can remember names of places instead of inpidual IP addresses.
Sometimes, however, it is ideal for a device to have a known, permanent IP address, and on a home network, IPv4 is a lot easier to remember. Most home networks have an address in the form of 192.168.x.x
, known as a private address, which gives you plenty of room to assign permanent, or static, IP addresses to some devices while letting others get assigned automatically. In this recipe, we will explore the options for both.
Note
One important note: In this recipe, we will look at how to set up DHCP and static IP from the command line (the hard way). If you plan on using your Raspberry Pi Zero frequently with X Windows or a VNC server, we will look at a way to set this up from the Raspbian desktop as well, which has been documented to override the command-line settings.
Getting ready
Whether you've soldered your Wi-Fi adapter to the Pi or have it connected via a USB OTG dongle, you are ready to go. You should keep your serial connection available in case you make a change that results in the loss of your network connection. We can run through all of this configuration using the console connection established in the first recipe of this chapter.
How to do it...
- First, let's take another look at your network configuration using
ifconfig
:pi@rpz14101:~$ ifconfig lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:1992 errors:0 dropped:0 overruns:0 frame:0 TX packets:1992 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1 RX bytes:161808 (158.0 KiB) TX bytes:161808 (158.0 KiB) wlan0 Link encap:Ethernet HWaddr 00:13:ef:80:0b:41 inet addr:192.168.2.119 Bcast:192.168.2.255 Mask:255.255.255.0 inet6 addr: fe80::a577:b1b7:a7a7:8a60/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:14761 errors:0 dropped:17582 overruns:0 frame:0 TX packets:826 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:5874864 (5.6 MiB) TX bytes:228064 (222.7 KiB)
The first entry,
lo
, should always come up when this command is run. This is called the loopback adapter and is the home of the device. If I am on the device and no other adapter or address is present, I can always use127.0.0.1
to have the Pi Zero talk to itself. This isn't something you'll need to worry about, but it is something you will see on any Linux computer you work with.The second entry,
wlan0
, is the Wi-Fi adapter. The second line is the IPv4 address, and the third is the IPv6 one. As you can see, my IPv4 address follows the192.168.x.x
standard. - Next, we will look at how and where DHCP or static addressing are configured. You'll find that file in
/etc/network/interfaces
:pi@rpz14101:~$ cat /etc/network/interfaces # interfaces(5) file used by ifup(8) and ifdown(8) # Please note that this file is written to be used with dhcpcd # For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf' # Include files from /etc/network/interfaces.d: source-directory /etc/network/interfaces.d auto lo iface lo inet loopback iface eth0 inet manual allow-hotplug wlan0 iface wlan0 inet manual wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
- We want to look closely at the lines starting with
iface
here. Our Wi-Fi adapter has this entry:iface wlan0 inet manual
The part of the command we want to focus on here is
manual
. This setting means manual configuration, which means that something else takes care of the configuration, or it is handled manually by the user. On the Raspbian OS, it doesn't seem to make much difference once you have things configured to this point, but you can change the setting todhcp
to specifically tell the interface to work as a DHCP client. - Change this line in your
/etc/network/interfaces
file:iface wlan0 inet manual
Make it this:
iface wlan0 inet dhcp
Restarting your network adapter with
sudo ifdown wlan0; sudo ifup wlan0
will return messages that a message is being sent to the DHCP server:pi@rpz14101:~$ sudo ifdown wlan0; sudo ifup wlan0 Internet Systems Consortium DHCP Client 4.3.1 Copyright 2004-2014 Internet Systems Consortium. All rights reserved. For info, please visit https://www.isc.org/software/dhcp/ Listening on LPF/wlan0/00:13:ef:80:0b:41 Sending on LPF/wlan0/00:13:ef:80:0b:41 Sending on Socket/fallback DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 6 DHCPREQUEST on wlan0 to 255.255.255.255 port 67 DHCPOFFER from 192.168.2.1 DHCPACK from 192.168.2.1 bound to 192.168.2.119 -- renewal in 36084 seconds.
- Now, let's say you want to assign your Raspberry Pi Zero a permanent address so you will always know what the address is. Generally speaking, you will want to keep the first three octets in your network address the same as before. So, my DHCP address is
192.168.2.119
, but I want to give it a static address. The address I should choose should be anything available between192.168.2.1
and192.168.2.254
, and it can't already be in use by another device on the network. To do this, I have to enter a lot more information: DHCP figures out a lot of things for you. Let's say I want my IP address to always be192.168.2.42
. My/etc/network/interfaces
file is currently as follows:allow-hotplug wlan0 iface wlan0 inet dhcp wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
It will change to this:
allow-hotplug wlan0 iface wlan0 inet static address 192.168.2.42 netmask 255.255.255.0 gateway 192.168.2.1 wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
Restarting with
sudo ifdown wlan0;sudo ifup wlan0
will restart the interface with the new static IP address:wlan0 Link encap:Ethernet HWaddr 00:13:ef:80:0b:41 inet addr:192.168.2.42 Bcast:192.168.2.255 Mask:255.255.255.0 inet6 addr: fe80::a577:b1b7:a7a7:8a60/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:20 errors:0 dropped:97297 overruns:0 frame:0 TX packets:42 errors:0 dropped:1 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:37298989 (35.5 MiB) TX bytes:14776826 (14.0 MiB)
Now you know how to set up your Raspberry Pi Zero with a dedicated IP address or leverage your home network's DHCP service to take care of the details for you.
Note
There are a lot of reports about the Raspberry Pi tools available in the GUI overriding your network settings that you have modified in the /etc/network/interfaces
file. I didn't run into an issues with it myself, but the best approach is to stick to either the command-line tool or the GUI tool, but if you make edits in both, you are bound to run into some service conflicts.
- Clojure Data Analysis Cookbook
- Instant Raspberry Pi Gaming
- 腦動力:C語言函數速查效率手冊
- ETL with Azure Cookbook
- 精通Windows Vista必讀
- Visual C# 2008開發技術實例詳解
- Creo Parametric 1.0中文版從入門到精通
- Learning C for Arduino
- 內模控制及其應用
- 教育機器人的風口:全球發展現狀及趨勢
- Bayesian Analysis with Python
- Learning ServiceNow
- 網頁設計與制作
- Mastering Machine Learning with R
- 深度學習之模型優化:核心算法與案例實踐