sd卡上config.txt中末行添加
dtoverlay=dwc2
cmdline.txt文件中rootwait后添加
modules-load=dwc2,g_ether
登录树莓派
ssh root@ssh pi@raspberrypi.local
Linux raspberrypi 4.14.34+ #1110 Mon Apr 16 14:51:42 BST 2018 armv6l
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Mon Jun 25 08:42:47 2018 from *****%usb0
SSH is enabled and the default password for the 'pi' user has not been changed.
This is a security risk - please login as the 'pi' user and type 'passwd' to set a new password.
pi@raspberrypi:~ $
初次登录密码是
raspberry
现在我们来配置网卡用静态ip使得树莓派通过usb共享电脑端的网络
(类似于使用USB网卡来共享网络)首先我们来为树莓派设置网络参数:
vi /etc/network/interfaces末行添加
allow-hotplug usb0
iface usb0 inet static
address 192.168.2.2
netmask 255.255.255.0
network 192.168.2.0
broadcast 192.168.2.255
gateway 192.168.2.1
这里我们设置的是一个2的网段,树莓派主机的ip为192.168.2.2。当树莓派通过usb连接到电脑上时我们可以在Mac的网络设置中看到一个RNDIS/Ethernet Gadget的网卡我们设置其参数为
注意这里的ip为192.168.2.1相当于小型局域网中的网关主机,此处也确实如此,树莓派以此ip为网关。此时直接reboot树莓派, 有很多人使用
serveice networking restart
再此我觉得是有点欠妥的因为ssh也会挂掉倒不如直接reboot。重启之后最重要的一步,我们点击mac的设置->共享,选择将本机网络共享给刚刚设置的RNDIS/Ethernet Gadget
至此登录树莓派基本可以上网了,可以使用
curl https://www.baidu.com
检验一下。少部分时候可能会失败,大部分原因是网关和dns比如说dns,我们通过
vi /etc/resolv.conf
查看树莓派上的dns服务器, 其默认nameserver是8.8.8.8但其实这个Google的域名解析服务在国内很容易被墙,所以如果你的路由器可以转发dns请求,那么最好是
namesever "your router ip"
或者找国内的dns sever 比如202.102.192.68。再有就是网关的设置,RNDIS/Ethernet Gadget的网关一定要设置成电脑所在局域网中能够转发数据包的主机!这样也就基本没什么问题了。
原文链接:https://blog.csdn.net/u011031257/article/details/80804907
叨叨几句... NOTHING