嵌入式Linux dhcp自动配置usb虚拟网卡ip跟主机通信

发布时间 2023-08-12 19:31:32作者: IotaHydrae

dhcpd自动配置usb虚拟网卡ip,与PC机通信

配置

buildroot

image

勾选dhcp server

修改设备/etc/dhcp/dhcpd.conf

option domain-name "example.org";
option domain-name-servers ns1.example.org, ns2.example.org;

default-lease-time 600;
max-lease-time 7200;

ddns-update-style none;

subnet 192.168.6.0 netmask 255.255.255.0 {
       range 192.168.6.100 192.168.6.255;
       option domain-name-servers 192.168.6.128;
       option subnet-mask 255.255.255.0;
       option routers 192.168.6.3;
       option broadcast-address 255.255.255.0;
}

host pcnode {
        fixed-address 192.168.6.100;
}

运行

insmod /opt/g_ether.ko

ifconfig usb0 192.168.6.1

dhcpd usb0

主机这边的网络配置里,应该已经自动被分配了ip

10: enx1a80e5e527f2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UNKNOWN group default qlen 1000
    link/ether 1a:80:e5:e5:27:f2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.6.100/24 brd 192.168.6.255 scope global dynamic noprefixroute enx1a80e5e527f2
       valid_lft 456sec preferred_lft 456sec
    inet6 fe80::7000:4645:df3d:70be/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

尝试从主机端访问设备

❯ ping 192.168.6.1
PING 192.168.6.1 (192.168.6.1) 56(84) bytes of data.
64 bytes from 192.168.6.1: icmp_seq=1 ttl=64 time=0.368 ms
64 bytes from 192.168.6.1: icmp_seq=2 ttl=64 time=0.371 ms
64 bytes from 192.168.6.1: icmp_seq=3 ttl=64 time=0.340 ms
^C
--- 192.168.6.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2047ms
rtt min/avg/max/mdev = 0.340/0.359/0.371/0.013 ms

更多

转发设备wifi网络,共享给主机(未添加)