Loopback 接口

什么接口是loopback?先看一个linux下面的配置:

[root@ZhouTianzuo ~]# ifconfig lo
lo        Link encap:Local 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:2584 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2584 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:129200 (126.1 KiB)  TX bytes:129200 (126.1 KiB

从配置可以看出,lo的mask仅为8bit,也就是说,只要127开头,任何网络主机号都可以,也就是说环回接口可以理解为一个网络号。

验证一下,ping 127.0.0.1 127.0.1.1 127.1.1.1 126.1.1.1

linux:~ # ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.019 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.012 ms
^C
--- 127.0.0.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.012/0.015/0.019/0.005 ms
linux:~ # 
linux:~ # 
linux:~ # ping 127.0.1.1
PING 127.0.1.1 (127.0.1.1) 56(84) bytes of data.
64 bytes from 127.0.1.1: icmp_seq=1 ttl=64 time=0.015 ms
64 bytes from 127.0.1.1: icmp_seq=2 ttl=64 time=0.010 ms
^C
--- 127.0.1.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.010/0.012/0.015/0.004 ms
linux:~ # 
linux:~ # ping 127.1.1.1
PING 127.1.1.1 (127.1.1.1) 56(84) bytes of data.
64 bytes from 127.1.1.1: icmp_seq=1 ttl=64 time=0.014 ms
64 bytes from 127.1.1.1: icmp_seq=2 ttl=64 time=0.009 ms
64 bytes from 127.1.1.1: icmp_seq=3 ttl=64 time=0.010 ms
^C
--- 127.1.1.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1998ms
rtt min/avg/max/mdev = 0.009/0.011/0.014/0.002 ms
linux:~ # 
linux:~ # ping 126.1.1.1
PING 126.1.1.1 (126.1.1.1) 56(84) bytes of data.
^C
--- 126.1.1.1 ping statistics ---
31 packets transmitted, 0 received, 100% packet loss, time 30238ms

linux:~ # 

可以,127这网络号下面的主机编址都是loopback。
继续这个,我们修改一下lo的地址,也是可以的:

[root@ZhouTianzuo ~]# ifconfig lo 130.0.0.1

[root@ZhouTianzuo ~]# ifconfig lo
lo        Link encap:Local Loopback  
          inet addr:130.0.0.1  Mask:255.255.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:3363 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3363 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:169619 (165.6 KiB)  TX bytes:169619 (165.6 KiB)

[root@ZhouTianzuo ~]# 

这个时候,默认的掩码变成了16bits,也就是网络130.0的下面的所有的主机都是loopback了,再ping之前的127.0.0.1也就不可能通了:

[root@ZhouTianzuo ~]# ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.

无法通,这里就不抓包了。

再看配置为32bits的掩码,会有什么情况:

[root@ZhouTianzuo ~]# ifconfig lo 130.0.0.1 netmask 255.255.255.255
[root@ZhouTianzuo ~]# 
[root@ZhouTianzuo ~]# ifconfig lo
lo        Link encap:Local Loopback  
          inet addr:130.0.0.1  Mask:255.255.255.255
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:3363 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3363 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:169619 (165.6 KiB)  TX bytes:169619 (165.6 KiB)

[root@ZhouTianzuo ~]# 

[root@ZhouTianzuo ~]# ping 130.0.0.1
PING 130.0.0.1 (130.0.0.1) 56(84) bytes of data.
64 bytes from 130.0.0.1: icmp_seq=1 ttl=64 time=0.186 ms
^C
--- 130.0.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 925ms
rtt min/avg/max/mdev = 0.186/0.186/0.186/0.000 ms
[root@ZhouTianzuo ~]# ping 130.0.0.2
PING 130.0.0.2 (130.0.0.2) 56(84) bytes of data.

明显ping 130.0.0.2就没有这个ip了。

总结:环回接口可以配置,而且是一个网络号,并非主机号,除非把掩码配置为32bits。在tcp ip详解里面有提及,就是,net/2的实现,会把到不是完全匹配ip的到lo的报文转发,比如lo的配置为127.0.0.1,到lo 127.0.0.2的报文就会转发,如此就不对了。

 

你可能感兴趣的:(Loopback 接口)