如何将每个服务器的 NIC 绑定到单独的 IP?

sja*_*sja 5 networking

我的问题是“如何将每个服务器的 NIC 绑定到单独的 IP?”

我的 Ubuntu 12.04 服务器有两个网卡——都连接到我的家庭局域网。

NIC 的设备名称在 /etc/udev/rules.d/70-persistent-net.rules 中设置。我已经调用了 lan0 和 lan1。(请参阅下面我的 70-persistent-net.rules)。

每个 NIC 通过 DHCP 从我的路由器正确获取其 IP 设置。IP 的分配是根据 dhcp 请求的 MAC 完成的。lan0 分配的 ip 为 172.23.80.1,而 lan1 分配的 ip 为 172.23.80.2。(请参阅下面的 ifconfig 输出和我的 /etc/network/interfaces)。

我的问题是,当我从单独的计算机通过 ssh 连接时,无论我在 ssh 调用中使用的 IP 是什么,我总是从同一服务器 MAC 接收连接。

让我描述以下显示我的问题的实验。我在我的 Ubuntu 桌面计算机上使用三个终端(term1、term2 和 term3)

第一学期:

arp -n
Address                  HWtype  HWaddress           Flags Mask            Iface
172.16.0.1               ether   00:50:7f:9d:2d:30   C                     wlan0
172.23.80.1                      (incomplete)                              wlan0
Run Code Online (Sandbox Code Playgroud)

学期2:

ssh me@172.23.80.1
me@172.23.80.1's password: 
Welcome to Ubuntu 12.04.1 LTS (GNU/Linux 3.2.0-29-generic-pae i686)

* Documentation:  https://help.ubuntu.com/
Last login: Thu Aug 16 12:37:48 2012 from 172.23.128.11
me@FERMI:~$ 
Run Code Online (Sandbox Code Playgroud)

第一学期:

arp -n
Address                  HWtype  HWaddress           Flags Mask            Iface
172.16.0.1               ether   00:50:7f:9d:2d:30   C                     wlan0
172.23.80.1              ether   00:0d:61:22:3d:d6   C                     wlan0
Run Code Online (Sandbox Code Playgroud)

第三学期:

ssh me@172.23.80.2
me@172.23.80.2's password: 
Welcome to Ubuntu 12.04.1 LTS (GNU/Linux 3.2.0-29-generic-pae i686)

* Documentation:  https://help.ubuntu.com/
Last login: Thu Aug 16 13:37:12 2012 from 172.23.128.11
me@FERMI:~$ 
Run Code Online (Sandbox Code Playgroud)

第一学期:

arp -n
Address                  HWtype  HWaddress           Flags Mask            Iface
172.23.80.2              ether   00:0d:61:22:3d:d6   C                     wlan0
172.16.0.1               ether   00:50:7f:9d:2d:30   C                     wlan0
172.23.80.1              ether   00:0d:61:22:3d:d6   C                     wlan0
Run Code Online (Sandbox Code Playgroud)

正如您从最后一个 arp 中看到的,即使两个 ip 已分配给不同的 NIC,两个 ssh 连接都由服务器上的单个 NIC 提供服务。这是我的问题。

这是我的服务器配置:

me@FERMI:~$ ifconfig
lan0      Link encap:Ethernet  HWaddr 00:0d:61:22:3d:d6  
          inet addr:172.23.80.1  Bcast:172.31.255.255  Mask:255.240.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1452 errors:0 dropped:0 overruns:0 frame:0
          TX packets:496 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:113691 (113.6 KB)  TX bytes:72934 (72.9 KB)

lan1      Link encap:Ethernet  HWaddr 00:19:5b:68:93:57  
          inet addr:172.23.80.2  Bcast:172.31.255.255  Mask:255.240.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1103 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:85747 (85.7 KB)  TX bytes:684 (684.0 B)
          Interrupt:22 Base address:0x9000 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:14 errors:0 dropped:0 overruns:0 frame:0
          TX packets:14 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:700 (700.0 B)  TX bytes:700 (700.0 B)


me@FERMI:~$ cat /etc/network/interfaces 
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto lan0 
iface lan0 inet dhcp

auto lan1 
iface lan1 inet dhcp


me@FERMI:~$ cat /etc/udev/rules.d/70-persistent-net.rules 
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.

# PCI device 0x1186:/sys/devices/pci0000:00/0000:00:1e.0/0000:03:02.0 (sundance)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:19:5b:68:93:57", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="lan1"

# PCI device 0x8086:/sys/devices/pci0000:00/0000:00:03.0/0000:02:01.0 (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0d:61:22:3d:d6", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="lan0"


me@FERMI:~$ route 
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         OHM.UNIVERSE    0.0.0.0         UG    100    0        0 lan0
172.16.0.0      *               255.240.0.0     U     0      0        0 lan0
172.16.0.0      *               255.240.0.0     U     0      0        0 lan1


me@FERMI:~$ cat /proc/sys/net/ipv4/ip_forward
0


me@FERMI:~$ arp -n
Address                  HWtype  HWaddress           Flags Mask            Iface
172.23.128.11            ether   00:18:de:02:f0:5c   C                     lan0
172.16.0.1               ether   00:50:7f:9d:2d:30   C                     lan0


me@FERMI:~$ sudo ufw status
Status: inactive
Run Code Online (Sandbox Code Playgroud)

小智 6

您的 NIC 绑定到单独的 IP。问题是默认路由。的输出route显示默认路由通过 lan0 NIC。

要解决您的索赔,只需使用ip route del default或删除默认路由route del default

但是,在那之后,您将无法路由到您的网络之外。如果需要,创建两个子网(例如 172.23.80.0 和 172.23.81.0)并分别使用 viaip route 172.23.80.0/24 via 172.23.80.1 dev lan0和路由会很有帮助ip route 172.23.81.0/24 via 172.23.81.1 dev lan1。例如

# /etc/network/interfaces
# 主网络接口
auto lan1
iface lan1 inet dhcp
post-up route del default
post-up ip route 172.23.81.0/24 via 172.23.81.1 dev lan1

也可能但未经测试

route del default
ip route add 172.23.80.1/32 via 172.23.80.1 dev lan0
ip route add 172.23.80.2/32 via 172.23.80.2 dev lan1