I would like to add a user specific host file on my Mac (El Capitan 10.11.5) to a users home directory and have this be checked in addition to /etc/hosts when resolving domain names.

I found a similar answer here. This answer was a flat no because that individual did not have superuser privileges. This is not the case for me because I do in fact have superuser privileges.

I found another answer here, but that doesn't seem to work on my machine. This answer involves setting the HOSTALIASES environment variable to ~/.hosts, but no addition to the ~/.hosts file ever had any implications in my web browser, which is the goal of me wanting to create a user specific host file.

NOTE: After reading through a bit of the man pages of bash, the correct environment variable on mac that the second answer needs is HOSTFILE. However, this still didn't affect anything. Thoughts on this?

EDIT: This is for a single user environment as MrWonderful suggested. However, I would like for the hosts setting of one user to be unaffected but another user.


  • 2
    I'm still curious as to what benefit this might provide that couldn't be realized through programatic means. Commented Jun 20, 2016 at 20:15
  • 1
    HOSTALIASES=$HOME/hosts sudo -E dtruss ping foobar shows the only file opened is /etc/hosts. HOSTFILE=$HOME/hosts has the same result. The hosts(5) man page states only /etc/hosts is read. The hostname(7) man page states HOSTALIASES is used. Ugh, more broken Apple software.
    – user83961
    Commented Oct 30, 2017 at 23:53
  • 1
    unix.stackexchange.com/questions/60549/… Commented Jun 16, 2018 at 0:20
  • unix.stackexchange.com/questions/10438/… Commented Jun 16, 2018 at 0:23

1 Answer 1


If you are managing a single-user environment, such as a shared MacBook, you could potentially have each user's .bashrc copy a base hosts file, then append their custom one to the end of it. For example:

sudo cp /etc/common_hosts /etc/hosts  
sudo cat ~/my_hosts >> /etc/hosts

This would effectively give every user their own custom additions to the hosts file. You can make it so a password isn't needed for these sudo commands by using the sudo visudo command and adding them to the allowed commands. Here are some examples from the bottom of my sudoers file:

# Samples
# %users  ALL=/sbin/mount /cdrom,/sbin/umount /cdrom
# %users  localhost=/sbin/shutdown -h now  

Under OS X, the /etc/hosts file is apparently dynamically read, so the net services would not need to be restarted.

mbp-c17189:~ root# ping goog
ping: cannot resolve goog: Unknown host
mbp-c17189:~ root# ping google.com
PING google.com ( 56 data bytes
64 bytes from icmp_seq=0 ttl=49 time=4306.507 ms
--- google.com ping statistics ---
2 packets transmitted, 1 packets received, 50.0% packet loss
round-trip min/avg/max/stddev = 4306.507/4306.507/4306.507/0.000 ms
mbp-c17189:~ root# echo     goog>>/etc/hosts
mbp-c17189:~ root# ping goog
PING goog ( 56 data bytes
64 bytes from icmp_seq=0 ttl=49 time=43.639 ms
--- goog ping statistics ---
2 packets transmitted, 2 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 43.639/43.914/44.189/0.275 ms
mbp-c17189:~ root# 
  • 1
    So, how do you handle it when two people are logged in simultaneously?
    – Allan
    Commented Jun 20, 2016 at 20:09
  • My answer would only apply to a single-user-at-a-time scenario, like on a Macbook, for instance. In a shared environment, this would not work well / at all. Commented Jun 20, 2016 at 20:10
  • 1
    @Allan - What is your real goal? Perhaps if you gave a more concrete example it would facilitate a deeper understanding of the problem domain. Especially why different users would need different names or IP addresses for some entries. Commented Jun 20, 2016 at 20:12
  • 1
    This is close to what I was after. I really wanted to leave the main hosts file alone so that the hosts setting for each user would apply only to them, but this is helpful nonetheless.
    – K. Shores
    Commented Jun 20, 2016 at 20:15
  • 1
    Not my goal...I just don't see this as a viable solution given that you would have to give sudo access to users potentially with no password. What's going to prompt them for the password every time the host file is to be edited?
    – Allan
    Commented Jun 20, 2016 at 20:19

You must log in to answer this question.

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