The hostname command outputs two IP addresses when one is expected

# hostname --help | grep ip
    -i, --ip-address       addresses for the host name
    -I, --all-ip-addresses all addresses for the host

According to the above, -i should output just one IP address, while -I should output all. In my case, it works the other way around.

# hostname -i
127.0.1.1 123.123.123.123
# hostname -I
123.123.123.123

Why is that?

Here’s my /etc/hosts file in case it matters:

127.0.0.1       localhost
::1     localhost       ip6-localhost   ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

127.0.1.1       tmp-1   tmp-1

123.123.123.123 tmp-1

(Where 123.123.123.123 is my real actual IP v4 address.)

Asked By: ᴍᴇʜᴏᴠ

||

The hostname(1) manpage describes these options in full, which should clarify the confusion:

-i, --ip-address

Display the network address(es) of the host name. Note that this works only if the host name can be resolved. Avoid using this option; use hostname --all-ip-addresses instead.

-I, --all-ip-addresses

Display all network addresses of the host. This option enumerates all configured addresses on all network interfaces. The loopback interface and IPv6 link-local addresses are omitted. Contrary to option -i, this option does not depend on name resolution. Do not make any assumptions about the order of the output.

(In any case, the help text for -i says "addresses", so multiple addresses can be expected there.)

Answered By: muru
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.