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
g www.google.com
export HOSTALIASES=$PWD/.hosts

and now

curl g #works 
curl #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) ?

  • It works for me. How does it say with curl -v work and getent hosts work? – yaegashi Aug 29 '15 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 – PSkocik Aug 29 '15 at 11:11
  • Got it. I'm on GNOME desktop and using NetworkManager and dnsmasq. It returns for A record query for domain name, as if inet_aton() applied. BIND returns NXDOMAIN for such queries. You can see it by host <nameserver address> – yaegashi Aug 29 '15 at 11:50
  • So I suppose it regards the aliased string as a domain name as-is, simply queries to libnss resolver modules with it. – yaegashi Aug 29 '15 at 11:57
  • 1
    Btw you can utilize a public service like xip.io to get arbitrary IPv4 address using domain names: work – yaegashi Aug 29 '15 at 12:28

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 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 to query for FQDN However, other servers including BIND just return NXDOMAIN for such queries. So you cannot rely on this to define work 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 like this:

  • Thanks. I've installed and started dnsmasq and the ip line in the HOSTALIASES file works now. – PSkocik Aug 31 '15 at 9:49

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.