In a linux shell script, I want to change the dns address temporarily. To do this, I can insert temprary dns address into resolv.conf right after the shell run and remove it before the script ends. That’s it.
But the problem is, all every other scripts also hit the address while the script is running. This is not what I expected. Isn’t there another way that can changes the dns temporarily in a shell without affecting the others ?
Advertisement
Answer
Unshare able to help you. Unshare help to run program with some namespaces unshared from parent. Using unshare you can able to use your custom resolv.conf limited to your script session & will not affect to other system users and queries.
sudo unshare --mount bash -s <<'EOF' mount --bind custom_resolv.conf /etc/resolv.conf sudo -u username run_command EOF