hostname -i prints the IP address of the host, after resolving the hostname. The trap: it uses the system resolver (which is /etc/hosts first, then DNS), so on a misconfigured box it can return 127.0.1.1 (the Debian default) or a wrong address. The capital -I flag (capital i) is the modern alternative - it lists all addresses assigned to the host’s interfaces, no DNS lookup.
Table of contents
- hostname -i (lowercase)
- hostname -I (capital I) - the modern alternative
- Other hostname flags worth knowing
- The /etc/hosts trap
- How this fits the rest of the stack
- FAQ
hostname -i (lowercase)
Output: a single IP address, or a space-separated list if the host has multiple A records.
$ hostname -i
192.168.1.42
What just happened: hostname called gethostbyname() (or its modern equivalent) on the host’s own name. The resolver checked /etc/hosts, then DNS, then returned the first address. The team that has /etc/hosts with the hostname mapped to 127.0.1.1 (the Debian default for laptops) sees the loopback address here - a confusing result that breaks a lot of scripts.
hostname -I (capital I) - the modern alternative
$ hostname -I
192.168.1.42 10.0.0.5 fe80::1
Output: every IP address assigned to any network interface, no DNS resolution. The team that runs scripts that need the host’s actual routable address uses this. The capital I does not go through the resolver at all - it reads the kernel’s interface table directly.
The trade-off: hostname -I returns all addresses. If the host has a public and a private interface, both show up. The team that needs one address picks it with hostname -I | awk '{print $1}' or similar.
Other hostname flags worth knowing
hostname -f # FQDN
hostname -s # short hostname
hostname -d # DNS domain
hostname -A # all FQDNs (similar to -I but via resolver)
The team that writes a startup script that needs the FQDN uses hostname -f. The team that needs the short name uses -s.
The /etc/hosts trap
The Debian installer (and Ubuntu’s) writes a line like this in /etc/hosts:
127.0.1.1 my-laptop
This is gethostbyname(my-laptop) returning 127.0.1.1 - the loopback. The team that runs hostname -i on a fresh Debian install and gets 127.0.0.1 is hitting this exact pattern.
The fix: either remove the 127.0.1.1 line (let the resolver use DNS or DHCP), or use hostname -I instead. The team that has scripts that depend on the real IP swaps to -I.
FAQ
Why does hostname -i return 127.0.1.1 on Ubuntu?
The Debian/Ubuntu installer writes a 127.0.1.1 <hostname> line in /etc/hosts. This is intentional for laptops (so the hostname resolves even without a network), but it makes hostname -i return the loopback. Use hostname -I (capital I) to get the real interface addresses.
What is the difference between hostname -i and hostname -I?
Lowercase -i calls the resolver on the hostname - it goes through /etc/hosts and DNS. Capital -I reads the kernel’s interface table directly and returns all addresses. Capital -I is what you want for scripts.
Can I change the host’s IP with hostname?
No. hostname reads the IP, it does not set it. To change the IP, edit /etc/hosts (for the resolver path) or the network interface config (for the actual interface). The team that scripts a host rename updates both.
Why does hostname -I return multiple addresses?
The host has multiple network interfaces - common on cloud VMs with a public and a private NIC, or on servers with multiple NICs for redundancy. Each interface has its own address.
Does hostname -i work on Windows?
No - hostname is a Linux/Unix tool. On Windows, the equivalent is ipconfig or Get-NetIPAddress in PowerShell. The team that scripts cross-platform host detection branches on the OS.
How this fits the rest of the stack
For a sense of what the full project costs before it commits, the RunxBuild hosting calculator shows the line items together. The API, the database, the storage, the worker, the bandwidth - each one is a separate number, and the team’s mental model for the platform is the sum of those numbers.
Useful related references: