案例场景;树莓派Linuxv9.1 连接到一个网络通过 RJ45:
luis@Fresoncio:~$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.18.25.248 netmask 255.255.255.0 broadcast 172.18.25.255
inet6 fe80::5c59:95e9:c658:8ad2 prefixlen 64 scopeid 0x20<link>
ether b8:27:eb:92:fe:b3 txqueuelen 1000 (Ethernet)
RX packets 24160 bytes 1673645 (1.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1968 bytes 196942 (192.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 4 bytes 156 (156.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4 bytes 156 (156.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
请注意 DHCP 获取的 IP: 172.18.25.248 。
有时我需要连接到其他网络,所以我添加几个网卡设备,例如:
luis@Fresoncio:~$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.18.25.248 netmask 255.255.255.0 broadcast 172.18.25.255
inet6 fe80::5c59:95e9:c658:8ad2 prefixlen 64 scopeid 0x20<link>
ether b8:27:eb:92:fe:b3 txqueuelen 1000 (Ethernet)
RX packets 24160 bytes 1673645 (1.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1968 bytes 196942 (192.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.35.101 netmask 255.255.255.0 broadcast 192.168.35.255
inet6 fe80::2064:deee:d534:1a83 prefixlen 64 scopeid 0x20<link>
ether a0:ce:c8:09:f8:84 txqueuelen 1000 (Ethernet)
RX packets 2378 bytes 177246 (173.0 KiB)
RX errors 0 dropped 4 overruns 0 frame 0
TX packets 54 bytes 9354 (9.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.33 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::a170:6c36:802a:c19 prefixlen 64 scopeid 0x20<link>
ether 3c:18:a0:07:63:8b txqueuelen 1000 (Ethernet)
RX packets 797 bytes 61728 (60.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 37 bytes 4845 (4.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
……等等,
到现在为止一切都还好。
这问题当我想来的时候放(无论是 DHCP 还是静态)IP配置任何 NIC 的,例如,对于主 NIC(始终eth0
):
luis@Fresoncio:~$ cat /etc/network/interfaces
# 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'
auto eth0
iface eth0 inet dhcp
# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d
重新启动(或重新启动网络)后,eth0
适配器设置正常,但任何其他 NIC 似乎停止工作,因为它们不再显示:
luis@Fresoncio:~$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.18.25.248 netmask 255.255.255.0 broadcast 172.18.25.255
inet6 fe80::5c59:95e9:c658:8ad2 prefixlen 64 scopeid 0x20<link>
ether b8:27:eb:92:fe:b3 txqueuelen 1000 (Ethernet)
RX packets 24160 bytes 1673645 (1.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1968 bytes 196942 (192.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 4 bytes 156 (156.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4 bytes 156 (156.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
即使他们还在公认的:
luis@Fresoncio:~$ ifconfig -a
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.18.25.248 netmask 255.255.255.0 broadcast 172.18.25.255
inet6 fe80::ba27:ebff:fe92:feb3 prefixlen 64 scopeid 0x20<link>
ether b8:27:eb:92:fe:b3 txqueuelen 1000 (Ethernet)
RX packets 1430 bytes 109681 (107.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 194 bytes 33418 (32.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth1: flags=4098<BROADCAST,MULTICAST> mtu 1500
ether a0:ce:c8:09:f8:84 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth2: flags=4098<BROADCAST,MULTICAST> mtu 1500
ether 00:0e:c6:d4:10:b6 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth3: flags=4098<BROADCAST,MULTICAST> mtu 1500
ether 3c:18:a0:07:63:8b txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 4 bytes 156 (156.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4 bytes 156 (156.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
当然,我可以/etc/network/interfaces
再次去设置eth1
,eth2
...等等。但是不是有一个更实用方法?
这是:如何为特定网卡设置 IP 配置在/etc/network/interfaces
不改变行为其他人的?
更多数据:
- 与运行 Ubuntu v16.04 的桌面 PC 的结果相同。
编辑01:
在普通的 Linux 安装中,这是/etc/network/interfaces
我常用的查找方式:
luis@Fresoncio:~$ cat /etc/network/interfaces
# 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
请注意,在这个原始的未修改/etc/network/interfaces
文件中,所有网卡都会正确启动(在我的例子中为 DHCP)并进行配置。
手动配置eth1
,eth2
...等,可能(对于某些人来说)确实被认为是解决问题的一种方法,但这不是问题所询问的方式。事实是:似乎您插入的任何 NIC(eth9
、wlan3
、enp2s0
... 等)都会在未触及的/etc/network/interfaces
文件中自动启动和配置(作为 DHCP),我想保留这种行为。
答案1
它ifconfig -a
向您显示所有已被识别并具有适当驱动程序的设备。
Plainifconfig
只显示已启动并已配置的设备。
你的/etc/network/interfaces
文件只是配置lo
适配器和eth0
适配器。您没有eth1
在启动时配置其他设备( 等),因此当它们在那里时,没有任何东西使它们上线并激活它们。 (除非您在 下的文件中做了一些事情/etc/network/interfaces.d
?)
/etc/network/interfaces.d
假设您无法/etc/network/interfaces
像这样进行设置 - eth0 仍将执行 DHCP 并提供默认网关,其他设备将位于其子网上,没有关联的网关。
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
auto eth1
ifact eth1 inet static
address 192.168.35.101
netmask 255.255.255.0
auto eth2
ifact eth2 inet static
address 192.168.1.33
netmask 255.255.255.0
请注意,您可以仅使用 1 个 NIC 并使用虚拟接口来执行相同的操作
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
auto eth0:1
ifact eth0:1 inet static
address 192.168.35.101
netmask 255.255.255.0
auto eth0:2
ifact eth0:2 inet static
address 192.168.1.33
netmask 255.255.255.0