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
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:   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

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 and  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.

| improve this answer | |
  • 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 localhost but i still be able to check my host's IP. – The One May 17 '16 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). – G-Man Says 'Reinstate Monica' May 17 '16 at 2:14
  • In the CentOS system, the bind and bind-utils was not installed. So i'm really confused. – The One May 17 '16 at 2:21
  • @TheOne Ubuntu treats hostname differently that RH based systems..put ubuntu14-graphite in the /etc/hosts file and check..note, its, not (which you can preserver anyway just for localhost, as is loopback block you can use any address although some systems have predefined restricted addresses) – heemayl May 17 '16 at 2:27
  • @heemayl So by putting ubuntu14-graphite on /etc/hosts then check the IP i didn't get the right IP. That doesn't make sense,right? – The One May 17 '16 at 2:49

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.

| improve this answer | |

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.

| improve this answer | |

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

| improve this answer | |
  • 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. – Stephen Kitt Apr 5 '19 at 22:21

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:    ubuntu    ubuntu.ubuntu-domain    ubuntu
| improve this answer | |
  • 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. – Julie Pelletier Aug 9 '16 at 6:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.