Last week someone created an AWS EC2 instance for us using AMI type
Amazon Linux 2 AMI 2.0.20190313 x86_64 HVM gp2. Let's call that Instance1. Yesterday we found a problem with our software that may have been caused by that version of Linux, so we created a new EC2 instance using an older AMI type
Amazon Linux AMI 2018.03.0.20190514 x86_64 HVM. We used the exact same instance type (m5.large), VPC, subnet, tags, and security groups as Instance1.
After we started up the second instance (Instance2), our software generated an IP address error. Then we noticed that the command
hostname -i returned
hostname: Unknown host.
hostname command by itself returns the correct hostname, and the
ifconfig command shows
eth0 having the correct IP address.
hostname -i on Instance1 returned the correct IP address.
We compared the 2 instances in the Description tab in the AWS console and did not see any differences. We asked the guy who created Instance1 about it and all he could suggest was checking that
eth0 existed and making sure the security groups were the same (which they are).
Does anyone know why
hostname -i is returning
Unknown host instead of the IP address?
We ended up performing a workaround:
/etc/hosts file on both Instance1 and Instance2 contained:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost6 localhost6.localdomain6
We edited the file on Instance2 to contain:
<ip address> localhost <hostname> ::1 localhost6 localhost6.localdomain6
hostname -i returned the IP address. But we would rather not have to make that change to
/etc/hosts on Instance2, since we didn't have to make that change on Instance1.