14

I've created a network namespace in Linux.

I thought there was a resolv.conf file for each namespaces created but it's not the case on my system. I don't have the following path /etc/netns/namespace_name/resolv.conf.. The folder netns doesn't exist.

There is only one resolv.conf (/etc/resolv.conf and another one mirrored in /run/resolv.conf). Any changes in this file affect all network interfaces. Is there a way I can use different DNS for both the host and the namespace?

1 Answer 1

22

You must create directory /etc/netns/_namespace_name_ yourself , and place different version resolv.conf there.

4
  • 1
    Would /etc/netns/namespace-name/resolv.conf be used automatically? Commented Sep 3, 2015 at 16:00
  • And can this be used for iptables too? Commented Sep 3, 2015 at 16:08
  • 3
    It will be used automatically because the full path above is bind-mounted by ip netns to /etc/resolv.conf, as explained in the man page of ip netns man7.org/linux/man-pages/man8/ip-netns.8.html. So from within a given network namespace, /etc/resolv.conf is the per-namespace resolv.conf file if and only if /etc/netns/namespace-name/resolv.conf has been created. Otherwise, an application will see the resolv.conf of the host machine, which might not be the desired behaviour. Commented Apr 4, 2017 at 14:17
  • Only additional thing, which I had to do was to provide permission. Otherwise, it was using resolv.conf of host machine. I gave chmod 777; didnt explore what is the least permission that was needed. Commented Jun 18, 2024 at 11:32

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.