Java InetAddress.getLocalHost().getHostAddress() 返回 127.0.1.1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16444753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:56:41  来源:igfitidea点击:

InetAddress.getLocalHost().getHostAddress() is returning 127.0.1.1

java

提问by Ravi Joshi

My question is similar to thisquestion . I want to get the real IP of my machine (not 127.0.0.1) but strange, the below code in my Ubuntu is returning 127.0.1.1

我的问题类似于这个问题。我想获取我机器的真实 IP(不是 127.0.0.1)但奇怪的是,我的 Ubuntu 中的以下代码返回 127.0.1.1

InetAddress.getLocalHost().getHostAddress()

Below is my complete code, originally posted in SO at here

下面是我的完整代码,最初发布在这里

public String getMachineIP() {
    try {
        String hostIP = InetAddress.getLocalHost().getHostAddress();
        if (!hostIP.equals("127.0.0.1")) {
            return hostIP;
        }

        /*
         * Above method often returns "127.0.0.1", In this case we need to
         * check all the available network interfaces
         */
        Enumeration<NetworkInterface> nInterfaces = NetworkInterface
                .getNetworkInterfaces();
        while (nInterfaces.hasMoreElements()) {
            Enumeration<InetAddress> inetAddresses = nInterfaces
                    .nextElement().getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                String address = inetAddresses.nextElement()
                        .getHostAddress();
                if (!address.equals("127.0.0.1")) {
                    return address;
                }
            }
        }
    } catch (UnknownHostException e1) {
        System.err.println("Error = " + e1.getMessage());
    } catch (SocketException e1) {
        System.err.println("Error = " + e1.getMessage());
    }
    return null;
}

The above code is returning 127.0.1.1 whereas ifconfigon my Ubuntu machine is giving below output

上面的代码返回 127.0.1.1 而ifconfig在我的 Ubuntu 机器上给出以下输出

root@dell:~# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:21:70:b7:30:cd  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
          Interrupt:28 Base address:0x6000 

eth1      Link encap:Ethernet  HWaddr 00:22:68:d3:02:b5  
          inet addr:192.168.2.112  Bcast:192.168.2.255  Mask:255.255.255.0
          inet6 addr: fe80::222:68ff:fed3:2b5/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:23827 errors:0 dropped:0 overruns:0 frame:32515
          TX packets:23200 errors:46 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:22027719 (22.0 MB)  TX bytes:3778268 (3.7 MB)
          Interrupt:19 

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:402 errors:0 dropped:0 overruns:0 frame:0
          TX packets:402 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:29197 (29.1 KB)  TX bytes:29197 (29.1 KB)

I found 127.0.1.1 entry in host file (Strange to me, since I never updated this file)

我在主机文件中找到了 127.0.1.1 条目(对我来说很奇怪,因为我从未更新过这个文件)

root@dell:~# cat /etc/hosts
127.0.0.1   localhost
127.0.1.1   dell

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

How to get the real IP of my machine (not 127.0.0.1)? I am looking ONLY for IPv4 address excluding 127.0.0.0/8 subnet

如何获取我机器的真实IP(不是127.0.0.1)?我只寻找 IPv4 地址,不包括127.0.0.0/8 subnet

采纳答案by Adrian

You'll need to use NetworkInterfaceto enumerate network interfaces; InetAddress.getLocalHost()always returns loopback. This doesn't explain why you get 127.0.1.1 instead of 127.0.0.1, but since that method doesn't do what you're trying to do, it doesn't seem especially pertinent. See: http://docs.oracle.com/javase/6/docs/api/java/net/NetworkInterface.html#getInetAddresses()

您需要使用NetworkInterface来枚举网络接口;InetAddress.getLocalHost()总是返回环回。这并不能解释为什么您会得到 127.0.1.1 而不是 127.0.0.1,但由于该方法无法执行您想要执行的操作,因此它似乎并不是特别相关。请参阅:http: //docs.oracle.com/javase/6/docs/api/java/net/NetworkInterface.html#getInetAddresses()

回答by maloney

Try this code and paste what you get:

试试这个代码并粘贴你得到的:

Enumeration en = NetworkInterface.getNetworkInterfaces();
while(en.hasMoreElements()){
    NetworkInterface ni=(NetworkInterface) en.nextElement();
    Enumeration ee = ni.getInetAddresses();
    while(ee.hasMoreElements()) {
        InetAddress ia= (InetAddress) ee.nextElement();
        System.out.println(ia.getHostAddress());
    }
 }

This will loop over all of the IP addresses bounded to your host

这将遍历绑定到您的主机的所有 IP 地址

回答by Tobi042

The whole 127.0.0.0/8 subnet is reserved for loopback devices (Reserved IP addresses) Just ignore any IP beginning with 127 :)

整个 127.0.0.0/8 子网是为环回设备保留的保留 IP 地址)只需忽略以 127 开头的任何 IP :)