通过编辑 /etc/resolv.conf 文件(使用 sed)更改 DNS 服务器和问题

发布于 2024-11-29 07:08:30 字数 674 浏览 1 评论 0原文

我想更改我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

∝单色的世界 2024-12-06 07:08:30

使用此代码(未经测试):

sed 's/nameserver.*/nameserver 192.168.1.5/' /etc/resolf.conf > /etc/resolf.conf.new
mv /etc/resolf.conf.new /etc/resolf.conf

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
時窥 2024-12-06 07:08:30

> 重定向的操作方式是,在运行任何命令之前输出文件都会被截断,这意味着 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 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.

栀子花开つ 2024-12-06 07:08:30

Sed 有文件内版本;这就是我要做的事情(如果我确定该文件中只定义了一个名称服务器;否则我会做其他事情,但我想他们不属于这个问题)。在示例中,为了安全起见,sed 生成了一个 resolv.conf_bak 文件。

~# NAMESERVER=192.168.1.5;
~# sed -i_bak "s/\(nameserver\) .*/\1 $NAMESERVER/" /etc/resolv.conf

这是一个更复杂的命令:

  1. 检查是否有名称服务器条目,
  2. 如果有,则使用 sed 对其进行编辑
  3. ,如果没有,则创建一个新条目

这是代码:

~# grep -q  nameserver /etc/resolv.conf && sed -i_bak "s/\(nameserver\) .*/\1 $NAMESERVER/" /etc/resolv.conf || echo "nameserver $NAMESERVER" >> /etc/resolv.conf

再次假设您仅使用一个单个名称服务器入口!

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.

~# NAMESERVER=192.168.1.5;
~# sed -i_bak "s/\(nameserver\) .*/\1 $NAMESERVER/" /etc/resolv.conf

And this is a more elaborate command:

  1. checks if there is a nameserver entry
  2. if so edits it with sed
  3. and if not creates a new one

This is the code:

~# grep -q  nameserver /etc/resolv.conf && sed -i_bak "s/\(nameserver\) .*/\1 $NAMESERVER/" /etc/resolv.conf || echo "nameserver $NAMESERVER" >> /etc/resolv.conf

Again provided that you work with just a single nameserver entry!

成熟稳重的好男人 2024-12-06 07:08:30

我会想象做如下的事情。使用 sed 将 cat 流操作到另一个文件,然后以某种方式验证这个新文件,例如计算行数,如果新文件有效,则用 /etc/resolv.conf 替换新文件。

可能是这样的:

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;

只有最后一行,我们先尝试计算行数,使用 head 打印除一行之外的所有行到输出文件,并使用 tail 打印最后一行,执行 sed 操作并附加 em> 到输出文件,如下所示:

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;

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:

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 append to the output file, like so:

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文