如果你是Linux 的老鸟了,那么你可以直接略过,Linux(Ubuntu)菜鸟可以一起看看,避免我踩过的坑。
在树莓派3B上安装Ubuntu Server 19.10,因为在Ubuntu18开始就是使用了Netplan工具来管理网络,说实话我之前一直没有用过这个工具,所以为了弄好这个我大致花了1.5个工作日才搞定固定IP地址的配置。在树莓派上的有线网卡(eth0)和无线网卡(wlan0)都配置了固定IP地址。
下面直接贴出我的配置文件内容,并加以了注释说明,重要的是看下我贴出的图片的效果,注意里面的绿色的英文,只有在正确的情况才会是这样,如果不是就表示你在某个地方错了。
ubuntu@ubuntu:~$ sudo vim /etc/netplan/50-cloud-init.yaml #*.50-cloud-init.yaml文件有可能你不是这个名字,但是请记住.yaml结尾
# This file is generated from information provided by # the datasource. Changes to it will not persist across an instance. # To disable cloud-init's network configuration capabilities, write a file # /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following: # network: {config: disabled} network: version: 2 renderer: networkd ethernets: eth0: addresses: - 172.16.1.253/24#IP地址,这里不要使用类似于“255.255.255.0”的方法而是直接指定掩码位数24为(/24) gateway4: 172.16.1.1 nameservers: addresses: [172.16.1.1,8.8.8.8] #DNS服务器地址,有多个的情况请使用英文的逗号隔开 wifis: wlan0: dhcp4: no #可以使用true 就不用配置下面的address、gateway4、nameservers了 dhcp6: no #可以使用true addresses: [172.16.1.252/24] #IP地址,这里不要使用类似于“255.255.255.0”的方法而是直接指定掩码位数24为(/24) gateway4: 172.16.1.1 nameservers: addresses: [172.16.1.1,8.8.8.8]#DNS服务器地址,有多个的情况请使用英文的逗号隔开 access-points: "zczl": #无线网络的SSID password: "123456" #无线网络的密码
ubuntu@ubuntu:~$ sudo netplan --debug apply #netplan检查配置文件情况,如果没有问题就会输入下面的结果,如果有问题就会报错。 ** (generate:16726): DEBUG: 12:53:29.344: Processing input file /etc/netplan/50-cloud-init.yaml.. ** (generate:16726): DEBUG: 12:53:29.345: starting new processing pass ** (generate:16726): DEBUG: 12:53:29.346: wlan0: adding wifi AP 'zczl' ** (generate:16726): DEBUG: 12:53:29.346: wlan0: setting default backend to 1 ** (generate:16726): DEBUG: 12:53:29.346: Configuration is valid ** (generate:16726): DEBUG: 12:53:29.347: eth0: setting default backend to 1** (generate:16726): DEBUG: 12:53:29.347: Configuration is valid** (generate:16726): DEBUG: 12:53:29.348: Generating output files.. ** (generate:16726): DEBUG: 12:53:29.349: NetworkManager: definition eth0 is not for us (backend 1) ** (generate:16726): DEBUG: 12:53:29.349: wlan0: Creating wpa_supplicant configuration file run/netplan/wpa-wlan0.conf ** (generate:16726): DEBUG: 12:53:29.350: Creating wpa_supplicant service enablement link /run/systemd/system/systemd-networkd.service.wants/[email protected] ** (generate:16726): DEBUG: 12:53:29.351: NetworkManager: definition wlan0 is not for us (backend 1)(generate:16726): GLib-DEBUG: 12:53:29.351: posix_spawn avoided (fd close requested) DEBUG:netplan generated networkd configuration changed, restarting networkd DEBUG:no netplan generated NM configuration existsDEBUG:eth0 not found in {} DEBUG:wlan0 not found in {} DEBUG:Merged config: network: bonds: {} bridges: {} ethernets: eth0: addresses: - 172.16.1.253/24 gateway4: 172.16.1.1 nameservers: addresses: - 172.16.1.1 - 8.8.8.8 vlans: {} wifis: wlan0: access-points: zczl: password: hz123456 addresses: - 172.16.1.252/24 dhcp4: false dhcp6: false gateway4: 172.16.1.1 nameservers: addresses: - 172.16.1.1 - 8.8.8.8 DEBUG:Skipping non-physical interface: lo DEBUG:device eth0 operstate is up, not changing DEBUG:device wlan0 operstate is dormant, not changingDEBUG:{} DEBUG:netplan triggering .link rules for loDEBUG:netplan triggering .link rules for eth0 DEBUG:netplan triggering .link rules for wlan0 sudo netplan apply #应用配置