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/hostswhen 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
HOSTALIASESenvironment variable to
~/.hosts, but no addition to the
~/.hostsfile 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.
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 (220.127.116.11): 56 data bytes 64 bytes from 18.104.22.168: icmp_seq=0 ttl=49 time=4306.507 ms ^C --- 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 22.214.171.124 goog>>/etc/hosts mbp-c17189:~ root# ping goog PING goog (126.96.36.199): 56 data bytes 64 bytes from 188.8.131.52: icmp_seq=0 ttl=49 time=43.639 ms ^C --- 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#