17

The HOSTALIASES environment variable allows users to set their own host aliases instead of having to sudoedit /etc/hosts (more details, e.g., at http://blog.tremily.us/posts/HOSTALIASES/)

However, with /etc/hosts I can alias IP addresses to names and names to names, whereas HOSTALIASES only seems to work with name to name aliasing.

I tried:

cat > .hosts
work 10.10.0.1
g www.google.com
^D
export HOSTALIASES=$PWD/.hosts

and now

curl g #works 
curl 10.10.0.1 #works
curl work #doesn't work

Can I make curl work work without needing to edit a file I don't have write permissions to (/etc/hosts) ?

6
  • It works for me. How does it say with curl -v work and getent hosts work?
    – yaegashi
    Commented Aug 29, 2015 at 10:36
  • $? == 6: ... couldn't resolve host 'work' ... AND $? == 2: <no output>. getent hosts g gets me $? == 0: 2a00:1450:400c:c05::67 www.google.com Commented Aug 29, 2015 at 11:11
  • Got it. I'm on GNOME desktop and using NetworkManager and dnsmasq. It returns 10.10.0.1 for A record query for domain name 10.10.0.1., as if inet_aton() applied. BIND returns NXDOMAIN for such queries. You can see it by host 10.10.0.1. <nameserver address>
    – yaegashi
    Commented Aug 29, 2015 at 11:50
  • So I suppose it regards the aliased string 10.10.0.1 as a domain name as-is, simply queries to libnss resolver modules with it.
    – yaegashi
    Commented Aug 29, 2015 at 11:57
  • 1
    Btw you can utilize a public service like xip.io to get arbitrary IPv4 address using domain names: work 10.10.0.1.xip.io.
    – yaegashi
    Commented Aug 29, 2015 at 12:28

1 Answer 1

20

HOSTALIASES feature is provided by the resolver funtion gethostbyname() in glibc. In this function an alias look up result is passed as-is to subsequent libnss module calls specified by hosts: in /etc/nsswitch.conf, therefore if there's no module which can handle it, gethostbyname() will end up with failure.

Note that in most programs numerical address notation like 10.10.0.1 and 2a00:1450:400c:c05::67 is processed by inet_aton() inet_pton() getaddrinfo() before gethostbyname() is called.

Some DNS servers, including dnsmasq, return valid address records to queries with a numerical address string as if inet_aton() applied to it: e.g. they return A record 10.10.0.1 to query for FQDN 10.10.0.1.. However, other servers including BIND just return NXDOMAIN for such queries. So you cannot rely on this to define work 10.10.0.1 in your HOSTALIASES as an alternative of /etc/hosts.

One possible workaround is to utilize a public DNS service like xip.io to get resolvable FQDNs for arbitrary IPv4 addresses. For example you can define work for 10.10.0.1 like this:

work 10.10.0.1.xip.io
2
  • Thanks. I've installed and started dnsmasq and the ip line in the HOSTALIASES file works now. Commented Aug 31, 2015 at 9:49
  • 1
    xip.io resolves to NXDOMAIN as of 2021-12-30. nip.io seems to have replaced it. Commented Dec 30, 2021 at 11:57

You must log in to answer this question.

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