Open LyleLee opened 4 years ago
Some awesome guys have talk about this topic: https://unix.stackexchange.com/questions/471122/namespace-management-with-ip-netns-iproute2/471214#471214
user1@intel:~/go/src/github.com/Lylelee/jail-program$ sudo strace -e open,unshare,setns,mount,umount2 ip netns exec jailns bash
setns(5, CLONE_NEWNET) = 0
unshare(CLONE_NEWNS) = 0
mount("", "/", 0x55ebdaa45725, MS_REC|MS_SLAVE, NULL) = 0
umount2("/sys", MNT_DETACH) = 0
mount("jailns", "/sys", "sysfs", 0, NULL) = 0
mount("/etc/netns/jailns/resolv.conf", "/etc/resolv.conf", 0x55ebdaa45725, MS_BIND, NULL) = 0
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=66189, si_uid=0, si_status=0, si_utime=0, si_stime=0} ---
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=66194, si_uid=0, si_status=0, si_utime=0, si_stime=0} ---
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=66196, si_uid=0, si_status=0, si_utime=0, si_stime=0} ---
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=66240, si_uid=0, si_status=0, si_utime=0, si_stime=0} ---
Confirm that
ip netns could have read /etc/netns/{nsName}/resolv.conf and bind mount it into namespace /etc/resolv.conf
So I add lines of code about bind mount
if err := syscall.Mount("/etc/netns/jailns/resolv.conf", "/etc/resolv.conf", "", syscall.MS_BIND, ""); err != nil {
log.Println(err.Error())
}
It works, but /etc/netns/jailns/resolv.conf propagate to host /etc/resolv.conf even the program exits. we have to manually umount /etc/resolv.conf after that. I think chroot my help.
Inside a network namespace we create, I see DNS resolve error:
Because ping tries to talk to DNS server, which locate default at 127.0.0.53, for DNS resolving. Unfortunately, there is no one listen there, since the network namespace is isolate.
According to
man ip-netns
link, placing a independent file resolv.conf, at which explicit point to a nameserver, to/etc/netns/{nsName}/resolv.conf
solve the problemsBut, when I write my own code and execute a command inside the same network namespace seeing problems again. I can't tell the exact cause.
ip netns
could have read/etc/netns/{nsName}/resolv.conf
and bind mount it into namespace/etc/resolv.conf
. but I can't find related code on ip route2 git repoFinally, the compromise is replacing host
/etc/resolv.conf
default setting with my custom nameserver.OS&kernel:
Be careful:
/etc/resolv.conf
is controlled bysystemd-resolved.service
. And could be overwritten anytime.I can verified it by fire: