---
title: "hadoop和hbase使用时要特别注意/etc/hosts文件"
author: "kabike"
date: "2014-09-01T20:30:26Z"
tags: []
categories: ["开源软件"]
slug: "hadoop-hbase-etc-hosts"
draft: false
lastmod: "2014-09-01T20:31:29Z"
id: 2111791
url: "https://kabike.iteye.com/blog/2111791"
---

/etc/hosts文件对hbase有很重要的作用
比如在hbase的master启动的时候,需要在zookeeper里注册自己的hostname,master取得hostname的方法是

```java
 String hostname = Strings.domainNamePointerToHostName(DNS.getDefaultHost(
      conf.get("hbase.master.dns.interface", "default"),
      conf.get("hbase.master.dns.nameserver", "default")));
```


DNS.getDefaultHost方法为

```java
public static String getDefaultHost(String strInterface, String nameserver)
    throws UnknownHostException {
    if (strInterface.equals("default")) 
      return InetAddress.getLocalHost().getCanonicalHostName();

    if (nameserver != null && nameserver.equals("default"))
      return getDefaultHost(strInterface);

    String[] hosts = getHosts(strInterface, nameserver);
    return hosts[0];
  }
```


如果/etc/hosts文件中存在以下这样的条目,InetAddress.getLocalHost().getCanonicalHostName()就会返回localhost,那么zookeeper里的master的hostname就成了localhost,那么region server无论如何都不能访问master了

>  127.0.0.1   localhost dev02

 
 所以要写成

>   127.0.0.1   localhost
>   192.168.1.2 dev02


[这个问题](http://stackoverflow.com/questions/23049777/hbase-regionserver-error-telling-master-we-are-up)应该也是同样的原因