如何设置每个用户的 resolv.conf

min*_*ill 5 domain-name-system resolv.conf

有没有办法指定每个用户resolv.conf

我发现的是对拥有每个用户主机文件的可能性的模糊引用,但我对此不感兴趣,我实际上对完整的resolv.conf.感兴趣,因为我想设置不同的名称服务器。

如果您问为什么要在我不想影响系统其他用户的多用户环境中测试cjdns名称服务器。

是否有可能滥用 nsswitch 系统?

Cha*_*ffy 11

本地文件系统命名空间是您的朋友,尽管它们确实需要 root 权限才能设置。

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
Run Code Online (Sandbox Code Playgroud)

如果您想要一个脚本,它将使用更新的 resolv.conf 运行任意命令,请考虑:

#!/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"
Run Code Online (Sandbox Code Playgroud)

因此,这可以用作:

with-custom-resolver your-resolv.conf sudo -u someuser some-command arg1
Run Code Online (Sandbox Code Playgroud)


min*_*ill 1

我也许可以通过向 NSS 添加服务来解决这个问题。