3

1我需要在Linux的接口上使用几个IP,并切换它,但它不起作用。
例如:

# ifconfig
eth0      Link encap:Ethernet  HWaddr 90:2b:34:33:80:65  
          inet addr:192.168.1.1  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::922b:34ff:fe33:8065/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:13220652 errors:0 dropped:32864 overruns:0 frame:0
          TX packets:8296620 errors:0 dropped:0 overruns:0 carrier:1
          collisions:0 txqueuelen:1000 
          RX bytes:16166162509 (16.1 GB)  TX bytes:2186645852 (2.1 GB)
          Interrupt:48 

eth0:1    Link encap:Ethernet  HWaddr 90:2b:34:33:80:65  
          inet addr:192.168.1.99  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              Interrupt:48 

lo        Link encap:Локальная петля (Loopback)  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:276685 errors:0 dropped:0 overruns:0 frame:0
          TX packets:276685 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:21387439 (21.3 MB)  TX bytes:21387439 (21.3 MB)

# ip route
169.254.0.0/16 dev eth0  scope link  metric 1000 
192.168.1.0/24 dev eth0  scope link  src 192.168.1.99 

# ping 192.168.1.50
PING 192.168.1.50 (192.168.1.50) 56(84) bytes of data.
64 bytes from 192.168.1.50: icmp_req=1 ttl=255 time=1.21 ms
64 bytes from 192.168.1.50: icmp_req=2 ttl=255 time=1.07 ms
64 bytes from 192.168.1.50: icmp_req=3 ttl=255 time=1.05 ms

然后我使用卷曲

# curl 192.168.1.50

和 tcpdump:

13:24:02.009094 IP 192.168.1.1 > 192.168.1.50: ICMP echo request, id 8919, seq 347, length 64
E..T..@.@..%.......2...s"..[...P....q#...................... !"#$%&'()*+,-./01234567
13:24:02.010087 IP 192.168.1.50 > 192.168.1.1: ICMP echo reply, id 8919, seq 347, length 64
E..T...........2.......s"..[...P....q#...................... !"#$%&'()*+,-./01234567
13:43:40.006264 IP 192.168.1.1.48275 > 192.168.1.50.80: Flags [S], seq 3496592766, win 14600, options [mss 1460,sackOK,TS val 15698502 ecr 0,nop,wscale 7], length 0
E..<O8@.@.h........2...P.i.~......9............
...F........
13:43:40.007663 IP 192.168.1.50.80 > 192.168.1.1.48275: Flags [S.], seq 3006420619, ack 3496592767, win 5792, options [mss 1460,sackOK,TS val 151247914 ecr 15698502,nop,wscale 0], length 0
E..<..@.@..8...2.....P...2V..i.................
    ..*...F....

并且源IP仍然是192.168.1.1,我做错了什么?

UPD:顺便说一句,刚刚在带有 2.6.32 内核的 Ubuntu 10.04 上尝试了同样的事情,一切正常,你甚至不需要在“ping”命令中添加“-I”,它在我的内核(3.2.0)中接缝有人坏了此功能。

4

2 回答 2

2

src选项仅影响其源地址由操作系统选择的数据包。ping程序选择自己的源地址。如果您想影响其选择,请使用-Ior-B选项。如果您想查看您的src选项是否有效,请建立 TCP 连接或发送 UDP 数据报。

于 2012-12-27T09:41:38.493 回答
1

尝试添加如下特定路线:

ip route add 192.168.1.50/32 dev eth0 proto static src 192.168.1.99

这应该会导致发送到 192.168.1.50 的任何流量都使用 src-ip 192.168.1.99...而不是默认的 192.168.1.1

于 2021-05-03T12:55:17.680 回答