18

I'm running Ubuntu 14.04 on my company's cloud service (my instance has elastic IP).  I want to check out the IP address of my instance I'm running by command line, but it didn't work.

root@ubuntu14-graphite:~# hostname
ubuntu14-graphite
root@ubuntu14-graphite:~# hostname -i
hostname: Name or service not known

I don't understand the reason why. 

I tried to ping to ubuntu14-graphite but it said:

unknown host ubuntu14-graphite.

Does that mean my host doesn't even exist?

I have another server (not cloud instance but physical server).  In this server I installed CentOS 6.7.  I checked the hostname's IP and it worked even though there was nothing special in /etc/hosts.

Here's the output of cat /etc/hosts:

127.0.0.1   localhost

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

5 Answers 5

18

It means that "the system" (I'm using that term in a broad, general, ambiguous sense) doesn't know that the name ubuntu14-graphite corresponds to your host and its IP address.  (The fact that is is your host's name isn't enough to make this happen.)  This almost surely means that you don't have a line for ubuntu14-graphite in your /etc/hosts file, and that it's not in your naming service (e.g., DNS or NIS).  Consequently, a way to fix it is to add ubuntu14-graphite (and its IP address) to your /etc/hosts.  If you're using a more sophisticated (distributed) naming system such as DNS or NIS (or maybe ActiveDirectory?), the ultimate solution is to add your host to that service.
_____________
† ... and I see from your post of your hosts file that this is the case.

There is some debate over the question of what IP address you should associate with your host name.  Everyone agrees that it should be an address that "works" for your host; i.e., commands like ping A.B.C.D should succeed.  Some people recommend using a loopback address.  Loopback addresses are always of the form 127.B.C.D; common values are 127.0.0.1 and 127.0.1.1.  Other people recommend using a LAN address, which might look like 192.168.C.D (but it can be almost anything) — but you should not put a dynamically assigned address into your hosts file (and, if you're using DHCP with DNS, then the dynamically assigned address(es) should be entered into your DNS records automatically).  Use commands like ifconfig and hostname -I to see what addresses are in use.

5
  • Thanks your your answer. I edited my question because i noticed one more case related to my CentOS server which doesn't have anything but 127.0.0.1 localhost but i still be able to check my host's IP.
    – The One
    Commented May 17, 2016 at 2:07
  • Well, maybe the name and address of the CentOS server are entered into the DNS system (or whatever name service is in use). Commented May 17, 2016 at 2:14
  • In the CentOS system, the bind and bind-utils was not installed. So i'm really confused.
    – The One
    Commented May 17, 2016 at 2:21
  • @TheOne Ubuntu treats hostname differently that RH based systems..put 127.0.1.1 ubuntu14-graphite in the /etc/hosts file and check..note, its 127.0.1.1, not 127.0.0.1 (which you can preserver anyway just for localhost, as 127.0.0.0/8 is loopback block you can use any address although some systems have predefined restricted addresses)
    – heemayl
    Commented May 17, 2016 at 2:27
  • @heemayl So by putting 127.0.1.1 ubuntu14-graphite on /etc/hosts then check the IP i didn't get the right IP. That doesn't make sense,right?
    – The One
    Commented May 17, 2016 at 2:49
1

The actual way to check a machine's IP address is by using the ifconfig command.

Note that depending on the actual cloud setup, the server instance may not be aware of the public IP and only know a VLAN IP. For details on this, you need to refer to your provider's documentation.

1
1

Add the myhostname entry to the hosts service in /etc/nsswitch.conf.

In other words, change this line in /etc/nsswitch.conf:

hosts:      files dns

to this:

hosts:      files dns myhostname

Where usually hostnames are resolved using the /etc/hosts file and dns, the hostname of the system can also be resolved by telling the Name Service Switch to do so (as this configuration change will do).

This would explain why you didn't see anything special about the /etc/hosts file on the system that you could resolve the local hostname.

More information on the myhostname flag is here.

1
  • But note that the document you link to says, “It is recommended to place myhostname after file and before dns.”. Commented May 13 at 10:08
0

In my opinion it depends on version of hostname command (net-tools package).

See: https://bugzilla.redhat.com/show_bug.cgi?id=319981

  • example on "openSuse Leap 42.3":
    # hostname -V
    net-tools 1.60
    hostname 1.100 (2001-04-14)

    # hostname -i
    hostname: Name or service not known
  • on Lubuntu 17.10
  $ hostname -V
  hostname 3.18

hostname -i - gives me information about IPv6 and IPv4

1
  • That bug report is about hostname -s, not hostname -i. This isn’t related to versions of hostname, it’s related to whether the hostname given by hostname is resolvable. Commented Apr 5, 2019 at 22:21
-3

Go to etc/hosts.save check your hostname there. And then edit /etc/hosts to write there your hostname and the hostname in hosts.save. Also check the IP address. Example:

127.0.0.1    ubuntu   
127.0.1.1    ubuntu.ubuntu-domain    ubuntu
1
  • 2
    Setting a localhost IP in /etc/hosts will not give them the actual IP address. I also fail to see why you're also looking at /etc/hosts.save which is a technicality and very irrelevant. Commented Aug 9, 2016 at 6:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .