手动配置 DNS 服务器 - raspbian

手动配置 DNS 服务器 - raspbian

我编写了一个简单的脚本,允许用户手动配置其 IP 地址和 DNS 服务器。通过创建一个/etc/resolv.conf包含用户输入的行的新文件来更改 DNS 服务器,例如,该文件最终可能如下所示:

nameserver 12.34.56.78
nameserver 12.34.56.79

然而,重新启动后,这些更改似乎不起作用,并且使用 DNS 失败。

我仍然可以 ping IP 地址,但尝试 ping 网站失败。

以下是完整的脚本,请告诉我们您认为问题可能是什么。

#!/bin/bash

# wipes any corrent set up
> /etc/network/interfaces

echo "Automatic DHCP or Manual?,(D/M) followed by [ENTER]:"
read network

if [ $network == "D" ]; then
    echo "auto lo" >> /etc/network/interfaces

    echo "iface lo inet loopback" >> /etc/network/interfaces
    echo "iface eth0 inet dhcp" >> /etc/network/interfaces
    echo "iface default inet dhcp" >> /etc/network/interfaces
    echo "Network set up!"
    exit 0
fi

if [ $network == "M" ]; then
    echo "Enter IP address (e.g 192.168.0.7), followed by [ENTER]:"
    read address
    echo "Enter Netmask (e.g 255.255.255.0, followed by [ENTER]:"
    read mask
    echo "Enter router IP (e.g 192.168.0.1), followed by [ENTER]:"
    read router
    echo "Enter first DNS server (e.g 8.8.8.8), followed by [ENTER]:"
    read dns1
    echo "Enter second DNS server (e.g 8.8.8.8), followed by [ENTER]:"
    read dns2

    echo "auto lo" >> /etc/network/interfaces
    echo "iface lo inet loopback" >> /etc/network/interfaces

    echo "iface eth0 inet static" >> /etc/network/interfaces
    echo "  address $address" >> /etc/network/interfaces    
    echo "  netmask $mask" >> /etc/network/interfaces
    echo "  gateway $router" >> /etc/network/interfaces

    echo "iface default inet dhcp" >> /etc/network/interfaces

    > /etc/resolv.conf
    echo "nameserver $dns1" >> /etc/resolv.conf
    echo "nameserver $dns2" >> /etc/resolv.conf

    echo "Network set up!"
    exit 0

fi

echo "ERROR: you do not enter D or M";
exit 0

该脚本基于此处的手动配置信息http://wiki.debian.org/NetworkConfiguration

当使用自动 DHCP 时,/etc/resolv.conf 包含:

domain zyxel.com
search zyxel.com
nameserver 192.168.1.1

答案1

默认的 resolvconf 软件包会/etc/resolv.conf生成一个符号链接。如果您删除该符号链接并创建新的 resolv.conf,它将在重启后保留下来。您非常接近该> /etc/resolv.conf行,但显然这不会覆盖符号链接。我建议首先在该行之前使用 删除旧符号链接rm /etc/resolv.conf(或者更好的是,使用 备份它mv /etc/resolv.conf /etc/resolv.conf.bak)。

相关内容