Is there a way to set per user “/etc/hosts” file or some other mechanism to create a per user DNS resolver?

5

I am writing a script that is supposed to switch IP address for specific FQDN and while I can mess with /etc/hosts directly I'd like to leave it vanilla and implement this some other way, preferably only for the user that executes the script.

Any suggestion?

user130888

Posted 2019-10-31T20:05:23.933

Reputation: 51

Answers

1

It may be useful to know which OS you use.

I am not aware of any way to do this other than via the hosts file.

I have developed my own bash script to automatically update my hosts file from a popular source for the purpose of ad blocking etc.

It has been running with long host files for several years without any problems.

My script does this as root:

1 - copy routine content from original hosts file to a new file

2 - copy custom entries to new file

3 - filter/copy from published ad blocker site

4 - overwrite hosts file with new file

5 - flush DNS

It should not be too difficult to do this on a per user basis.

As long as you are happy to work as root don't be afraid of playing with hosts files but keep the original.

Neville Hillyer

Posted 2019-10-31T20:05:23.933

Reputation: 146

thanks for the answer! I am using macOS (I tagged it but didn't mention it in the post, apologies). I was going down that route as you described but would like to avoid running this as root. – user130888 – 2019-10-31T23:30:46.233

He tagged MacOs – Keltari – 2019-10-31T23:30:58.150

Sorry - missed tag. I mainly use OS X 10.6.8. My script is contained within a custom file at /Library/LaunchDaemons/hosts.plist which I load and edit via AppleScript. – Neville Hillyer – 2019-10-31T23:55:58.800

10.6.8? That's horribly ancient and unsupported. I only use it in a VM so I can run MT-Newswatcher. – Barmar – 2019-11-01T17:40:59.670