首先我要说的是,网络不是我的强项,我正在尝试配置一些子接口,以便我正在运行的某些代码将有更多套接字可以本地绑定。到目前为止,我已经完成了以下操作,我可以在本地绑定到我的子接口 192.168.2.210,但我无法与其建立远程连接。
这是我的 ifconfig:
eth0 Link encap:Ethernet HWaddr 00:50:56:BB:00:0B
inet addr:10.6.19.18 Bcast:10.255.255.255 Mask:255.255.255.0
inet6 addr: fe80::250:56ff:febb:b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1056026 errors:0 dropped:0 overruns:0 frame:0
TX packets:638290 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:237037186 (226.0 MiB) TX bytes:126161362 (120.3 MiB)
eth0:1 Link encap:Ethernet HWaddr 00:50:56:BB:00:0B
inet addr:192.168.2.210 Bcast:192.168.2.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
以下是我尝试配置 eth0:1 的操作
ifconfig eth0:1 192.168.2.210/32 up
ip route add 192.168.2.210/32 via 10.6.19.18
这是我的 netstat -r 输出:
192.168.2.210 devserv. 255.255.255.255 UGH 0 0 0 eth0
192.168.2.0 * 255.255.255.0 U 0 0 0 eth0
10.6.19.0 * 255.255.255.0 U 0 0 0 eth0
link-local * 255.255.0.0 U 0 0 0 eth0
default 10.6.19.1 0.0.0.0 UG 0 0 0 eth0
有什么想法吗?谢谢观看
答案1
放弃 ifconfig,因为它正在被 iproute2 包逐步淘汰;
root@bensley-n2:~# ip address show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
link/ether 00:e0:4c:6d:0a:49 brd ff:ff:ff:ff:ff:ff
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 74:ea:3a:ad:2e:00 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.5/24 brd 192.168.0.255 scope global wlan0
inet6 fe80::76ea:3aff:fead:2e00/64 scope link
valid_lft forever preferred_lft forever
4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
link/ether ce:70:78:f8:10:75 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
root@bensley-n2:~# ip address add 1.1.1.1/24 dev wlan0
root@bensley-n2:~# ip address show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
link/ether 00:e0:4c:6d:0a:49 brd ff:ff:ff:ff:ff:ff
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 74:ea:3a:ad:2e:00 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.5/24 brd 192.168.0.255 scope global wlan0
inet 1.1.1.1/24 scope global wlan0
inet6 fe80::76ea:3aff:fead:2e00/64 scope link
valid_lft forever preferred_lft forever
4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
link/ether ce:70:78:f8:10:75 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
root@bensley-n2:~# ip route show
default via 192.168.0.1 dev wlan0
1.1.1.0/24 dev wlan0 proto kernel scope link src 1.1.1.1
192.168.0.0/24 dev wlan0 proto kernel scope link src 192.168.0.5
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1
(您可以缩写ip
命令:ip address add
可以一直缩写为ip a a
,ip address show
可以缩写为ip a s
。如果您不指定第二个参数,则是默认操作,因此如果您想非常简洁,show
您只需输入即可。)ip a
您不需要为本地子网添加路由。
答案2
首先,套接字实际上并未绑定到接口——它们绑定到 IP 地址。虚拟/别名接口已弃用,应避免使用——您应直接向添加多个 IP 地址eth0
。其次,如果您希望这些地址在本地网络上可路由,则需要使用该网络上的地址(即 10.6.19.0/24),而不是像 192.168.2.0/24 这样的机器本地子网(除非您可以让路由器或远程机器使用该其他子网重新配置)。要添加其他地址,请使用以下命令:
ip addr add 10.6.19.19/24 dev bond0
由于地址位于现有子网上,因此您不需要任何额外的路由配置。
答案3
与其添加更多地址(这会变得有点复杂),为什么你的套接字不使用不同的端口呢?