# MacOS – User specific host file

hostsmacosNetworksudoterminal

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.

Thoughts?

• 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
PING google.com (172.217.4.238): 56 data bytes
64 bytes from 172.217.4.238: icmp_seq=0 ttl=49 time=4306.507 ms
^C