Linux 通过编辑 /etc/resolv.conf 文件(使用 sed)和问题来更改 DNS 服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7014019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change DNS server by editing /etc/resolv.conf file (using sed) and problem
提问by sees
I want to change the DNS server for my Linux machine. So, I'm going to edit /etc/resolv.conf file.
我想为我的 Linux 机器更改 DNS 服务器。所以,我要编辑 /etc/resolv.conf 文件。
The command I'm using is SED. And doing as below for change DNS server to 192.168.1.5:#cat /etc/resolv.conf | sed '/nameserver/ c\ nameserver 192.168.1.5' > /etc/resolv.conf
我使用的命令是SED。并按照以下操作将 DNS 服务器更改为192.168.1.5:#cat /etc/resolv.conf | sed '/nameserver/ c\ nameserver 192.168.1.5' > /etc/resolv.conf
The problem is:
问题是:
When I execute the command the first time and it changes the resolv.conf to something like:
当我第一次执行命令并将 resolv.conf 更改为以下内容时:
domain somedomain
namserver 192.168.1.5
domain somedomain
namserver 192.168.1.5
but when I execute it once again to change DNS server to 192.168.1.4:#cat /etc/resolv.conf | sed '/nameserver/ c\ nameserver 192.168.1.4' > /etc/resolv.conf
但是当我再次执行它以将 DNS 服务器更改为 192.168.1.4 时:#cat /etc/resolv.conf | sed '/nameserver/ c\ nameserver 192.168.1.4' > /etc/resolv.conf
The file resolv.conf becomes empty
文件 resolv.conf 变为空
Questions:
1. Am I doing the right way to change DNS server?
2. Is there problem with the sedcommand in the above command?
问题:
1. 我更改 DNS 服务器的方法正确吗?
2 、上面命令中的sed命令有问题吗?
采纳答案by jw013
The way >
redirection operates, the output file is truncated before any of the commands are run, which means the cat
ought to see an empty file so the expected result is nothing. I am a bit puzzled as to why your first invocation works. You should use a temporary file (e.g. mv resolv.conf resolv.conf~
and run sed -e '...' resolv.conf~ > resolv.conf
, no need for cat
). Alternatively, if you have GNU sed you can use the in-place editing option (sed -i
), again no need for cat
.
>
重定向操作的方式是,在运行任何命令之前,输出文件被截断,这意味着cat
应该看到一个空文件,因此预期结果什么都没有。我对您的第一次调用为何有效感到有些困惑。您应该使用临时文件(例如mv resolv.conf resolv.conf~
并运行sed -e '...' resolv.conf~ > resolv.conf
,不需要cat
)。或者,如果您有 GNU sed,您可以使用就地编辑选项 ( sed -i
),同样不需要cat
.
回答by TMS
use this code (untested):
使用此代码(未经测试):
sed 's/nameserver.*/nameserver 192.168.1.5/' /etc/resolf.conf > /etc/resolf.conf.new
mv /etc/resolf.conf.new /etc/resolf.conf
回答by CodeReaper
I would imagine doing something like the following. Use sed to manipulate the cat stream to a different file, then validate this new file somehow e.g. count the number of lines and if the new file is valid replace the new file with /etc/resolv.conf.
我会想象做类似以下的事情。使用 sed 将 cat 流操作到不同的文件,然后以某种方式验证这个新文件,例如计算行数,如果新文件有效,则用 /etc/resolv.conf 替换新文件。
Could be something like this:
可能是这样的:
cat /etc/resolv.conf | sed "s/.*nameserver.*/nameserver 192.168.1.5/" > /etc/resolv.conf.sed;
if [ "$(wc -l /etc/resolv.conf.sed)" -eq "0" ]; then
rm /etc/resolv.conf.sed;
fi;
if [ -e /etc/resolv.conf.sed ]; then
mv /etc/resolv.conf.sed /etc/resolv.conf
fi;
Only the last line, well try counting the lines first, use head to print all lines but one to the output file and use tail to print the last line, perform the sed manipulation and appendto the output file, like so:
只有最后一行,首先尝试计算行数,使用 head 将所有行打印到输出文件,并使用 tail 打印最后一行,执行 sed 操作并附加到输出文件,如下所示:
I=$(wc -l /etc/resolv.conf | cut -d\/ -f1);
N=$[I-1];
head -n$N /etc/resolv.conf > /etc/resolv.conf.sed
tail -n1 /etc/resolv.conf | sed "s/.*nameserver.*/nameserver 192.168.1.5/" >> /etc/resolv.conf.sed;
if [ "$(wc -l /etc/resolv.conf.sed)" -eq "$I" ]; then
rm /etc/resolv.conf.sed;
fi;
if [ -e /etc/resolv.conf.sed ]; then
mv /etc/resolv.conf.sed /etc/resolv.conf
fi;
回答by hmontoliu
Sed has in-file edition; this is how I would do it (if I'm sure that there is just only one nameserver defined in that file; otherwise I'd do other things, but I guess that they are out of this question). In the example sed generates a resolv.conf_bak file for safety.
Sed 有文件内版本;这就是我的做法(如果我确定该文件中只定义了一个名称服务器;否则我会做其他事情,但我想他们不在这个问题范围内)。在示例中 sed 生成一个 resolv.conf_bak 文件以确保安全。
~# NAMESERVER=192.168.1.5;
~# sed -i_bak "s/\(nameserver\) .*/ $NAMESERVER/" /etc/resolv.conf
And this is a more elaborate command:
这是一个更复杂的命令:
- checks if there is a nameserver entry
- if so edits it with sed
- and if not creates a new one
- 检查是否有名称服务器条目
- 如果是这样,用 sed 编辑它
- 如果没有创建一个新的
This is the code:
这是代码:
~# grep -q nameserver /etc/resolv.conf && sed -i_bak "s/\(nameserver\) .*/ $NAMESERVER/" /etc/resolv.conf || echo "nameserver $NAMESERVER" >> /etc/resolv.conf
Again provided that you work with just a singlenameserver entry!
再次假设您只使用一个名称服务器条目!