/etc/hosts can't be a symbolic link?

4

0

I need to write a small script that allows me to switch between development environments by changing the /etc/hosts file.

I would like to use a symbolic link instead of replacing the whole /etc/hosts file, but when I do the /etc/hosts is ignored. Why?

thedp

Posted 2018-04-08T14:01:59.630

Reputation: 255

Answers

5

Yes, a symbolic link does not work.

Use a hard link instead:

sudo ln -f {source} /etc/hosts

Leon Jiang

Posted 2018-04-08T14:01:59.630

Reputation: 66

1I'd say this is a good alternative to what OP is trying to achieve instead of the accepted answer. – shriek – 2019-02-07T19:09:52.283

5

Hosts is a very basic system file and many OS functions depend on it. So they won't accept sym links on such files and expect them to be of type 'ordinary'.

Try overwriting it with your versions, keeping the originals. By the way, you may want to try running local DNS with different zones instead.

MTG

Posted 2018-04-08T14:01:59.630

Reputation: 184

You didn't answer the question. – thedp – 2018-04-08T14:12:50.060

updated to meet your request. – MTG – 2018-04-08T14:56:46.920

It seems to be the case, I guess I will just cp the different hosts files I have to /etc/hosts. Thank you. – thedp – 2018-04-08T21:31:22.057