通过编辑 /etc/resolv.conf 文件(使用 sed)更改 DNS 服务器和问题
我想更改我的 Linux 计算机的 DNS 服务器。所以,我要编辑 /etc/resolv.conf 文件。
我使用的命令是SED。并执行以下操作将 DNS 服务器更改为 192.168.1.5:
#cat /etc/resolv.conf | #cat /etc/resolv.conf | sed '/nameserver/ c\ nameserver 192.168.1.5' > /etc/resolv.conf
问题是:
当我第一次执行该命令时,它将 resolv.conf 更改为:
domain somedomain
namserver 192.168.1.5
但是当我再次执行它以将 DNS 服务器更改为 192.168.1.4 时:
#cat /etc/resolv.conf | #cat /etc/resolv.conf | sed '/nameserver/ c\ nameserver 192.168.1.4' > /etc/resolv.conf
文件 resolv.conf 变为空
问题:
1. 我更改 DNS 服务器的方法是否正确?
2、上面命令中的sed命令有问题吗?
I want to change the DNS server for my Linux machine. So, I'm going to edit /etc/resolv.conf file.
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
The problem is:
When I execute the command the first time and it changes the resolv.conf to something like:
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
The file resolv.conf becomes empty
Questions:
1. Am I doing the right way to change DNS server?
2. Is there problem with the sed command in the above command?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用此代码(未经测试):
use this code (untested):
>
重定向的操作方式是,在运行任何命令之前输出文件都会被截断,这意味着cat
应该看到一个空文件,因此预期结果是什么都没有。我有点困惑为什么你的第一次调用有效。您应该使用临时文件(例如mv resolv.conf resolv.conf~
并运行sed -e '...' resolv.conf~ > resolv.conf
,不需要cat
)。或者,如果您有 GNU sed,则可以使用就地编辑选项 (sed -i
),同样不需要cat
。The way
>
redirection operates, the output file is truncated before any of the commands are run, which means thecat
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 runsed -e '...' resolv.conf~ > resolv.conf
, no need forcat
). Alternatively, if you have GNU sed you can use the in-place editing option (sed -i
), again no need forcat
.Sed 有文件内版本;这就是我要做的事情(如果我确定该文件中只定义了一个名称服务器;否则我会做其他事情,但我想他们不属于这个问题)。在示例中,为了安全起见,sed 生成了一个 resolv.conf_bak 文件。
这是一个更复杂的命令:
这是代码:
再次假设您仅使用一个单个名称服务器入口!
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.
And this is a more elaborate command:
This is the code:
Again provided that you work with just a single nameserver entry!
我会想象做如下的事情。使用 sed 将 cat 流操作到另一个文件,然后以某种方式验证这个新文件,例如计算行数,如果新文件有效,则用 /etc/resolv.conf 替换新文件。
可能是这样的:
只有最后一行,我们先尝试计算行数,使用 head 打印除一行之外的所有行到输出文件,并使用 tail 打印最后一行,执行 sed 操作并附加 em> 到输出文件,如下所示:
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.
Could be something like this:
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 append to the output file, like so: