定制系统无法获取动态IP?

定制系统无法获取动态IP?

我正在使用 6.4.0 版本的内核自定义系统,并且文件系统是使用busybox.但是不知道为什么enp3s0无法获取ip。看起来usingudhcpc可以获取ip,但是useifconfig -a查看的时候还是不行。

# cat /etc/network/interfaces
auto enp3s0
iface enp3s0 inet dhcp

# dmesg |grep enp
[    1.140220] r8169 0000:02:00.0 enp2s0: renamed from eth0
[    1.166766] r8169 0000:03:00.0 enp3s0: renamed from eth1
[    1.985026] r8169 0000:02:00.0 enp2s0: Link is Down
[    2.206037] r8169 0000:03:00.0 enp3s0: Link is Down
[    4.969924] r8169 0000:03:00.0 enp3s0: Link is Up - 1Gbps/Full - flow control rx/tx
[    4.969958] IPv6: ADDRCONF(NETDEV_CHANGE): enp3s0: link becomes ready
[    5.202340] r8169 0000:02:00.0 enp2s0: Link is Up - 1Gbps/Full - flow control rx/tx
[    5.202375] IPv6: ADDRCONF(NETDEV_CHANGE): enp2s0: link becomes ready
# udhcpc -b -i enp3s0 -p /var/run/lan_udhcpc.pid -R
udhcpc: started, v1.36.1
udhcpc: broadcasting discover
udhcpc: broadcasting select for 192.168.50.76, server 192.168.50.254
udhcpc: lease of 192.168.50.76 obtained from 192.168.50.254, lease time 7200
# ifconfig -a
enp2s0    Link encap:Ethernet  HWaddr 00:E0:1A:D8:B0:E3
          inet addr:192.168.5.2  Bcast:192.168.5.255  Mask:255.255.255.0
          inet6 addr: fe80::2e0:1aff:fed8:b0e3/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:548 errors:0 dropped:0 overruns:0 frame:0
          TX packets:358 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:46441 (45.3 KiB)  TX bytes:61582 (60.1 KiB)

enp3s0    Link encap:Ethernet  HWaddr 00:E0:1A:D8:B0:E4
          inet6 addr: fe80::2e0:1aff:fed8:b0e4/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6740 errors:0 dropped:56 overruns:0 frame:0
          TX packets:26 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:594227 (580.2 KiB)  TX bytes:4228 (4.1 KiB)

lo        Link encap:Local Loopback
          LOOPBACK  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

答案1

我的问题通过添加暂时解决了default.script,即保存ip。但是,我不太明白为什么udhcpc需要这样的额外操作。我动态获取ip,肯定是为了保存和使用。

添加-s /etc/udhcpc/default.script

udhcpc -b -i enp3s0 -p /var/run/lan_udhcpc.pid -R -s /etc/udhcpc/default.script

默认脚本:

#!/bin/sh

[ -z "$1" ] && echo "Error: should be called from udhcpc" && exit 1

RESOLV_CONF="/etc/resolv.conf"
[ -e $RESOLV_CONF ] || touch $RESOLV_CONF
[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
[ -n "$subnet" ] && NETMASK="netmask $subnet"
# Handle stateful DHCPv6 like DHCPv4
[ -n "$ipv6" ] && ip="$ipv6/128"

if [ -z "${IF_WAIT_DELAY}" ]; then
        IF_WAIT_DELAY=10
fi

wait_for_ipv6_default_route() {
        printf "Waiting for IPv6 default route to appear"
        while [ $IF_WAIT_DELAY -gt 0 ]; do
                if [ -z "$(ip -6 route list | grep default)" ]; then
                        printf "\n"
                        return
                fi
                sleep 1
                printf "."
                : $((IF_WAIT_DELAY -= 1))
        done
        printf " timeout!\n"
}

case "$1" in
        deconfig)
                /sbin/ifconfig $interface up
                /sbin/ifconfig $interface 0.0.0.0

                # drop info from this interface
                # resolv.conf may be a symlink to /tmp/, so take care
                TMPFILE=$(mktemp)
                grep -vE "# $interface\$" $RESOLV_CONF > $TMPFILE
                cat $TMPFILE > $RESOLV_CONF
                rm -f $TMPFILE

                if [ -x /usr/sbin/avahi-autoipd ]; then
                        /usr/sbin/avahi-autoipd -k $interface
                fi
                ;;

        leasefail|nak)
                if [ -x /usr/sbin/avahi-autoipd ]; then
                        /usr/sbin/avahi-autoipd -wD $interface --no-chroot
                fi
                ;;

        renew|bound)
                if [ -x /usr/sbin/avahi-autoipd ]; then
                        /usr/sbin/avahi-autoipd -k $interface
                fi
                /sbin/ifconfig $interface $ip $BROADCAST $NETMASK
                if [ -n "$ipv6" ] ; then
                        wait_for_ipv6_default_route
                fi

                if [ -n "$router" ] ; then
                        echo "deleting routers"
                        while route del default gw 0.0.0.0 dev $interface 2> /dev/null; do
                                :
                        done

                        for i in $router ; do
                                route add default gw $i dev $interface
                        done
                fi

                # drop info from this interface
                # resolv.conf may be a symlink to /tmp/, so take care
                TMPFILE=$(mktemp)
                grep -vE "# $interface\$" $RESOLV_CONF > $TMPFILE
                cat $TMPFILE > $RESOLV_CONF
                rm -f $TMPFILE

                # prefer rfc3397 domain search list (option 119) if available
                if [ -n "$search" ]; then
                        search_list=$search
                elif [ -n "$domain" ]; then
                        search_list=$domain
                fi

                [ -n "$search_list" ] &&
                        echo "search $search_list # $interface" >> $RESOLV_CONF

                for i in $dns ; do
                        echo adding dns $i
                        echo "nameserver $i # $interface" >> $RESOLV_CONF
                done
                ;;
esac

HOOK_DIR="$0.d"
for hook in "${HOOK_DIR}/"*; do
    [ -f "${hook}" -a -x "${hook}" ] || continue
    "${hook}" "${@}"
done

exit 0

相关内容