问题原文
原帖链接 · miniBill · CC BY-SA 3.0
Is there a way to specify a per-user resolv.conf?
What I found are vague references to the possibility of having a per-user host file, but I'm not interested in that, I'm actually interested in a full resolv.conf, because I want to set different nameservers.
If you're asking why the point is testing cjdns nameserver(s) on a multi-user environment in which I don't want to affect other users of the system.
Would it be possible to perhaps abuse the nsswitch system?
社区高分解答
原帖链接 · Charles Duffy · CC BY-SA 3.0
Local filesystem namespaces are your friend, though they do require root permissions to set up.
sudo unshare --mount bash -s <<'EOF'
mount --bind /path/to/your/resolv.conf /etc/resolv.conf
sudo -u username-to-run-as command-to-run-with-alternate-resolv-conf
EOF
If you want a script which will run an arbitrary command with your updated resolv.conf, consider:
#!/bin/bash
## usage: with-custom-resolver /path/to/resolv.conf cmd arg1 arg2 ...
## ...note that this requires root.
script=""
add_cmd() {
local cmd_str
printf -v cmd_str '%q ' "$@"
script+="$cmd_str"$'\n'
}
resolv_conf=$1; shift
[[ $EUID = 0 ]] || { echo "Must be run as root" >&2; exit 1; }
[[ -e $resolv_conf ]] || { echo "No file found at: $resolv_conf" >&2; exit 1; }
add_cmd mount --bind "$resolv_conf" /etc/resolv.conf
add_cmd exec "$@"
unshare --mount sh -e -c "$script"
Thus, this could be used as:
with-custom-resolver your-resolv.conf sudo -u someuser some-command arg1
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
