-
环境介绍:
- linux开发板是mx6ull。virtualbox虚拟机是ubuntu18.04。网卡是usb网卡。虚拟机的网卡设置:a.NAT-enp0s3。b.桥接电脑的wifi网卡-enp0s8。c.桥接usb网卡-enp0s9。
- ubuntu网络设置:a. usb网卡手动分配ip地址:192.168.2.2,网关是192.168.2.2。b.桥接wifi网卡是路由器自动分配ip地址。c.NAT网卡不用管。
- 开发板网络设置:a.手动分配ip:192.168.2.3,网关是192.168.2.2,DNS:8.8.8.8。
-
打开虚拟机转发:
-
虚拟机开启ip转发: # 临时开启
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
永久生效
sudo sh -c "echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf"
sudo sysctl -p
-
虚拟机配置NAT转发:
规则sudo iptables -t nat -A POSTROUTING -o enp0s3 -j MASQUERADE
sudo iptables -A FORWARD -i enp0s9 -o enp0s3 -j ACCEPT
sudo iptables -A FORWARD -i enp0s3 -o enp0s9 -j ACCEPT
-
开发板配置网关和DNS:
配置IP
ifconfig eth0 192.168.2.3 netmask 255.255.255.0
配置默认网关(指向虚拟机)
route add default gw 192.168.2.2
配置DNS
echo "nameserver 8.8.8.8" > /etc/resolv.conf
-
测试
开发板上ping虚拟机
ping 192.168.2.2
开发板上ping外网
ping 8.8.8.8
开发板上ping域名
ping www.baidu.com
-
-
调试方法:
-
确认虚拟机IP转发已开启:cat /proc/sys/net/ipv4/ip_forward。结果1是开启。0是没有。
-
确认iptables规则存在:
sudo iptables -t nat -L -n。
正确的显示:Chain POSTROUTING (policy ACCEPT 20 packets, 1391 bytes)
pkts bytes target prot opt in out source destination
8 597 MASQUERADE all -- * enp0s3 0.0.0.0/0 0.0.0.0/0
sudo iptables -L FORWARD -n
正确的显示:Chain FORWARD (policy ACCEPT 6 packets, 454 bytes)
pkts bytes target prot opt in out source destination
0 0 ACCEPT all -- enp0s9 enp0s3 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT all -- enp0s3 enp0s9 0.0.0.0/0 0.0.0.0/0
-
在虚拟机上通过enp0s3网卡:ping -I enp0s3 8.8.8.8
-
虚拟机监听内网口:tcpdump -i enp0s9 icmp(在内网ping 8.8.8.8)
正确的显示:tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on enp0s9, link-type EN10MB (Ethernet), capture size 262144 bytes
21:29:55.849889 IP 192.168.2.3 > dns.google: ICMP echo request, id 21248, seq 0, length 64
21:29:56.387531 IP dns.google > 192.168.2.3: ICMP echo reply, id 21248, seq 0, length 64
-
监听外网口:tcpdump -i enp0s3 icmp(在内网ping 8.8.8.8)
正确的显示:tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on enp0s3, link-type EN10MB (Ethernet), capture size 262144 bytes
^C21:36:53.569286 IP yang-ubuntu18 > dns.google: ICMP echo request, id 21760, seq 0, length 64
-
linux开发板连接virtualbox虚拟机ubuntu的usb网卡,访问外网方法。
bush42026-05-20 13:01
相关推荐
SkyWalking中文站18 小时前
认识 Horizon UI · 5/17:3D 基础设施地图tntxia1 天前
linux curl命令详解_curl详解扛枪的书生2 天前
Linux 网络管理器用法速查SkyWalking中文站2 天前
认识 Horizon UI · 1/17:SkyWalking 新一代可观测性控制台顺风尿一寸2 天前
Java Socket 内核之旅:从 SocketChannel.read() 到 tcp_recvmsg 与 epoll 的完整调用链路雪梨酱QAQ2 天前
Kubeneters HA Cluster部署江华森2 天前
Spring Cloud 微服务全栈实战:从 Eureka 到 Docker Compose 一文贯通江华森2 天前
Matplotlib 数据绘图基础入门XIAOHEZIcode2 天前
Ubuntu 终端美化全栈指南:Bash 到 Kitty 踩坑实录江华森2 天前
NumPy 数值计算基础入门