官术网_书友最值得收藏!

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

  1. 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 use 127.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 the 192.168.x.x standard.

  2. 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
    
  3. 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 to dhcp to specifically tell the interface to work as a DHCP client.

  4. 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.
    
  5. 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 between 192.168.2.1 and 192.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 be 192.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.

主站蜘蛛池模板: 城步| 循化| 如东县| 甘谷县| 土默特右旗| 扶沟县| 墨脱县| 仪征市| 玉门市| 织金县| 泽库县| 蛟河市| 霍州市| 尖扎县| 伊宁市| 灵璧县| 潮安县| 上林县| 屯昌县| 安岳县| 涿鹿县| 离岛区| 宝丰县| 鄂伦春自治旗| 尼木县| 扶风县| 大足县| 平潭县| 阜宁县| 南城县| 盖州市| 渭源县| 马尔康县| 盘山县| 平舆县| 枝江市| 启东市| 江门市| 铅山县| 城固县| 青神县|