Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Is it possible to add a list of hosts that are only specific to a certain user? Perhaps a user-specific hosts file?

This mechanism should also complement the entries in the /etc/hosts file.

share|improve this question
well, you might instead run own nameservers, and have the user use different nameservers per user-specific resolv.conf - except creating user-specific resolv.conf appears to be exactly as difficult as making user-specific /etc/hosts. – SF. Dec 10 '13 at 11:07
You might want to unaccept the currently accepted answer, apparently things have changed since then. – terdon Mar 7 '14 at 3:40

7 Answers 7

up vote 21 down vote accepted

As far as I know there isn't. The easiest way to do what I think you want is to write an LD_PRELOAD library that overrides gethostbyname() and related functions.

share|improve this answer
Hard to believe there isn't such library yet. – Piotr Findeisen Aug 21 '12 at 9:08
It's an unusual use case, and potentially causes confusion - for example, if you run a setuid program, it will not (must not!) pick up the user's hosts file. Main use I can think of is web browser adblockers, which should be done internally to the browser. – pjc50 Dec 10 '13 at 14:09
Just found out about the HOSTALIASES environment variable. As pjc50 already suggested, this feature is automatically cleared for setuid programs. – pwuertz Dec 10 '13 at 17:49
See for a working implementation of this solution. Works like charm! – Boris Bukh Sep 9 '14 at 18:16

AFAIK, There is nothing like that, and there is no reason to have something like that. There is a design problem if an admin think that he need a hosts file per user. Think about it: You can't hide a host just because you don't have it in /etc/hosts, you're reading more files when the system is initialized and maybe the difference between 2 users will be up to 5 hosts.

The right question is: Is there a good reason to want something like that?

Maybe the answer will say what kind of utility you really want

EDIT - Maybe this is a good conclusion: if you use your own domain as a domain name server (in DNS), you have just 1 table to map domains in the server. DNS does not work at user-level.

share|improve this answer
You're right, I suppose I should explain the problem and this may present a better solution. – redspike Apr 1 '11 at 14:32
@redspike: It's not clear from the question whether it is meant that the admin should override the hosts for a specific user, or whether a user should be able to extend the host aliases without any help from the admin. I thought rather that redspike is asking about the latter situation; this answer is about the first situation I describe... – imz -- Ivan Zakharyaschev Apr 1 '11 at 14:43
There are various tool-specific configuration mechanisms to create shortcuts for hostnames, see e.g. ssh_config(5) or git-push(1) for [url "<actual url base>"] in Git configuration. – imz -- Ivan Zakharyaschev Apr 1 '11 at 14:47
@redspike, then you should chage the question to fit the answer you really need – D4RIO Apr 1 '11 at 16:18
-1 for telling the OP what question he has to post – miracle173 Dec 5 '12 at 5:20

Not sure if this would help you, but I came here looking for a way to add saved "hosts" somewhere that was easily accessible to only my user.

I basically needed to be able to ssh into certain boxes on our work network, which only has one entry point.

What I did was add alias's to my .bashrc file.

For example, if you added:

alias jrfbox 'ssh jason@1912.168.6.6'

at the bottom of the ~/.bashrc (~ is your home directory), then after you logout and login again, you can type "jrfbox" hit enter, and it will connect.

share|improve this answer
If you're interesting specifically in the SSH case, you should see man ssh_config. – Nick May 6 '13 at 18:08

Beside the LD_PRELOAD tricks. A simple alternative that may work on a few systems would be to binary-edit a copy of the system library that handles hostname resolution to replace /etc/hosts with a path of your own.

For instance, on Linux:

If you're not using nscd, copy to some location of your own like:

mkdir -p -- ~/lib &&
cp /lib/x86_64-linux-gnu/ ~/lib

Binary-edit the copy to replace /etc/hosts in there to something the same length like /tmp/hosts.

perl -pi -e 's:/etc/hosts:/tmp/hosts:g' ~/lib/

Edit /tmp/hosts to add the entry you want. And use

export LD_LIBRARY_PATH=~/lib

for nss_files to look in /tmp/hosts instead of /etc/hosts.

Instead of /tmp/hosts, you could also make it /dev/fd//3, and do

exec 3< ~/hosts

For instance which would allow different commands to use different hosts files.

If nscd is installed and running, you can bypass it by doing the same trick, but this time for and replace the path to the nscd socket (something like /var/run/nscd/socket) with some nonexistent path.

share|improve this answer
+1 for audacity, -1 for the shock value – fche Sep 27 '13 at 12:37
+1 for binary patching, -1 for security implications – Parthian Shot Jun 26 at 20:28
@ParthianShot, what security implications? – Stéphane Chazelas Jun 27 at 11:05
@StéphaneChazelas Changing LD_LIBRARY_PATH to point to a directory owned by the user means any other process run by the user can use that directory to co-opt any new processes spawned by replacing libraries. And updates to through the package manager (including security updates) won't be reflected in the patched version. Modifying LD_LIBRARY_PATH is generally a bad thing to recommend for other reasons, but it's also unwise because of those issues. – Parthian Shot Jun 28 at 3:26
@ParthianShot, your point about missing updates is a fair point. However, for your other point, if a rogue software is running in your name, it having write access to an area in $LD_LIBRARY_PATH would be the least of your worries as it's already got write access to a lot worse and more reliable areas like your .bash*, crontab, .forward, and all configuration files by all the software you use (where it can for instance set LD_{PRELOAD,LIBRARY_PATH} but would typically do a lot worse) – Stéphane Chazelas Jun 28 at 7:09

The functionality you are looking for seems to be implemented in glibc. You can define a custom hosts file using the HOSTALIASES environment variable.

Here is a modified example from that works on my system (Ubuntu 13.10):

$ echo 'g' >> ~/.hosts
$ export HOSTALIASES=~/.hosts
$ wget g -O /dev/null

The linked blog post also mentions some limitations:

  • HOSTALIASES only works for applications using getaddrinfo(3) or gethostbyname(3)
  • When setuid is used libc sanitizes the environment, which means that the HOSTALIASES setting is lost. ping is setuid root (because it needs to listen for ICMP packets), so HOSTALIASES will not work with ping unless you're already root before you call ping.
share|improve this answer
This should be the answer. Thank you so much!! – gatoatigrado Jan 31 '14 at 21:21
Note that it doesn't work if you're using nscd and is limited to hostnames without a dot. – Stéphane Chazelas May 29 '14 at 12:45

One solution is to have each user in a separate chroot, so they can each have a separate /etc/hosts to themselves.

share|improve this answer
This could be an answer, but as stated with little explanation it is more suited as a comment – Anthon Dec 10 '13 at 10:49
Well... yeah, it's doable. Although chrooting is a pretty heavy-duty solution for this kind of thing. And brings with it its own set of issues. – Parthian Shot Jun 26 at 20:31

Private mountspaces created with the unshare command can be used to provide a private /etc/hosts file to a shell process and any subsequent child processes started from that shell.

# Start by creating your custom /etc/hosts file
[user] cd ~
[user] cat >my_hosts <<EOF localhost localhost.localdomain localhost4 localhost4.localdomain4

[user] sudo unshare --mount
# We're now running as root in a private mountspace. 
# Any filesystem mounts performed in this private mountspace
# are private to this shell process and its children

# Use a bind mount to install our custom hosts file over /etc/hosts
[root] mount my_hosts /etc/hosts --bind

[root] cat /etc/hosts localhost localhost.localdomain localhost4 localhost4.localdomain4

[root] exec su - appuser

[appuser] # Run your app here that needs a custom /etc/hosts file

[appuser] ping
PING ( 56(84) bytes of data.
64 bytes from localhost ( icmp_seq=1 ttl=64 time=0.062 ms
64 bytes from localhost ( icmp_seq=2 ttl=64 time=0.026 ms
--- ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.026/0.044/0.062/0.018 ms
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.