大网数据广告活动
服务器运维问答

How to set a per-user resolv.conf

· 52CE 我爱测

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 wan…

问题原文

原帖链接 · 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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2013-05-22)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。