主要是解决两个问题:
- 同时连接两个网络时由于默认网卡不对导致不能上网;
- 开机的时候等n久都连不上 WiFi,但是一插上网线,WiFi 就通了。
WiFi 相关
查看和连接
查找附近的无线网络:
| 12
 
 | sudo iwlist wlan0 scansudo iwlist wlan0 scan | grep "ESSID"
 
 | 
查看当前的 WiFi 网络:
编辑连接:
| 1
 | sudo vim /etc/wpa_supplicant/wpa_supplicant.conf
 | 
| 12
 3
 4
 5
 6
 
 | network={ssid="abc"
 psk="12345678"
 key_mgmt=WPA-PSK
 priority=17
 }
 
 | 
更新配置:
| 1
 | wpa_cli -i wlan0 reconfigure
 | 
开关 WiFi
临时开关:
| 12
 
 | sudo ifconfig wlan0 downsudo ifconfig wlan0 up
 
 | 
永久关闭:
| 1
 | echo "dtoverlay=pi3-disable-wifi" | sudo tee -a /boot/config.txt
 | 
其他开关(蓝牙):
How
to disable onboard WiFi and Bluetooth on Raspberry Pi 3
静态 IP
第一次连接树莓派的时候为了确定 ip 可能会将 ip=xxxx 的信息写在
/boot/cmdline.txt 里面,不过后期要用静态 IP
的时候不建议这么写,否则树莓派在启动的时候为了连接有线网络会等待很长时间。如果仍要设置静态
IP ,这么配置比较好:
| 1
 | sudo vim /etc/dhcpcd.conf
 | 
在最后添加如下内容:
| 12
 3
 4
 5
 
 | interface eth0                                  #网络名static ip_address=172.31.31.35/26               #ipv4地址 / 网络前缀
 #static ip6_address=fd51:42f8:caae:d92e::ff/64  #ipv6
 static routers=172.31.0.1                       #网关
 tatic domain_name_servers=202.99.166.4 8.8.8.8  #DNS域名解析服务器
 
 | 
它的网络配置和其他的有些不同,dhcpcd 用的不是子网掩码,而是 CIDR
的斜线记法,两者之间需要稍微换算一下。比如 ip 地址
192.168.191.1,子网掩码 255.255.255.0,就可以记为
192.168.191.1/24。
为什么不去编辑/etc/network/interfaces呢?在我这个版本(2018-04-18-raspbian-stretch)中
interfaces 里面说静态 IP 得去 dhcpcd.conf
里面配置:
| 12
 3
 4
 5
 6
 7
 
 | # 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
 
 | 
raspbian
- Multiple IP addresses being assigned - Raspberry Pi Stack
Exchange
路由选择网卡上网
我的网线走的是联通的个人网,需要设置 Netkeeper
才能上网。所以为了给树莓派联网,我只能用手机或电脑开热点。但是在同时连接网线和WiFi的时候,树莓派上不了网,只有拔掉网线之后,树莓派才能打开网页。
不仅仅是树莓派,我装在电脑上的 Ubuntu
也是这样,每次上网总得拔掉网线,再连wifi,很麻烦。
上过组网课后今天再次想到这个问题,这应该和路由协议有关。查看路由表:
| 12
 3
 4
 5
 6
 7
 8
 
 | pi@raspberrypi:~ $ routeKernel IP routing table
 Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
 default         172.31.0.1      0.0.0.0         UG    202    0        0 eth0
 default         192.168.191.1   0.0.0.0         UG    303    0        0 wlan0
 172.31.0.1      0.0.0.0         255.255.255.255 UH    202    0        0 eth0
 172.31.31.0     0.0.0.0         255.255.255.192 U     202    0        0 eth0
 192.168.191.0   0.0.0.0         255.255.255.0   U     303    0        0 wlan0
 
 | 
发现默认路由 default
(有时候显示为0.0.0.0)转发的网关有两个,一个是
eth0 (网线)的 172.31.0.1 ,一个是
wlan0 (WiFi)的 192.168.191.1 。由于 eth0
排在 wlan0
的前面,发给因特网的请求会转发给网关,然后联通把数据包吞了。看下面的
ping 测试:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 
 | pi@raspberrypi:~ $ ping -c 4 baidu.comPING baidu.com (123.125.114.144) 56(84) bytes of data.
 From 172.31.31.35 icmp_seq=1 Destination Host Unreachable
 From 172.31.31.35 icmp_seq=2 Destination Host Unreachable
 From 172.31.31.35 icmp_seq=3 Destination Host Unreachable
 From 172.31.31.35 icmp_seq=4 Destination Host Unreachable
 
 --- baidu.com ping statistics ---
 4 packets transmitted, 0 received, +4 errors, 100% packet loss, time 23107ms
 pipe 4
 
 
 | 
既然 172.31.0.1 的路由转发记录无效,不妨把它删除。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 
 | pi@raspberrypi:~ $ sudo route del default gw 172.31.0.1 eth0
 pi@raspberrypi:~ $ route -n
 Kernel IP routing table
 Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
 0.0.0.0         192.168.191.1   0.0.0.0         UG    303    0        0 wlan0
 172.31.0.1      0.0.0.0         255.255.255.255 UH    202    0        0 eth0
 172.31.31.0     0.0.0.0         255.255.255.192 U     202    0        0 eth0
 192.168.191.0   0.0.0.0         255.255.255.0   U     303    0        0 wlan0
 
 pi@raspberrypi:~ $ ping -c 4 baidu.com
 PING baidu.com (220.181.57.216) 56(84) bytes of data.
 64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=1 ttl=53 time=13.7 ms
 64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=2 ttl=53 time=15.0 ms
 64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=3 ttl=53 time=17.6 ms
 64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=4 ttl=53 time=16.7 ms
 
 --- baidu.com ping statistics ---
 4 packets transmitted, 4 received, 0% packet loss, time 3004ms
 rtt min/avg/max/mdev = 13.730/15.800/17.656/1.515 ms
 
 | 
移除完这条记录,地址就能够正常解析了,百度也 ping
得通了。所以原因在于存在多个默认网关,并且那个首选的网关是不可到达的。重启系统或网络后路由表就会恢复,可以把它当作一个临时的做法。如果要永久生效的话也有办法的,我就没折腾了,文末的参考连接里面有。
回头一看,上一篇树莓派日记已经是快一年前的事情了……惭愧,或许叫吃灰日记比较好(
参考资料: