ubuntu 怎么设置静态ip及dns

首先不推荐修改/etc/resolv.conf或者修改/etc/resolvconf/resolv.conf.d/目录下的head,base文件,添加tail文件的方法也不赞成。

因为resolv.conf和head文件中都有提醒文字:

[plain]  view plain copy print ?
  1. cat /etc/resolv.conf   
  2. # Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)  
  3. #     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN  

[plain]  view plain copy print ?
  1. cat /etc/resolvconf/resolv.conf.d/head  
  2. # Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)  
  3. #     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN  

不要对这个视若无睹。以为即便你当时改对了,网络服务一重启,又不行了。


用man resolvconf命令查看文档,可以看到一段提醒注意的文字:

[plain]  view plain copy print ?
  1. N.B.: On a machine where resolvconf has just been or is about to be installed and which previously relied on a static /etc/resolv.conf file, the nameserver  informa‐  
  2.        tion in that static file should be migrated to the appropriate iface stanza(s) in interfaces(5).  
证明了的确不推荐修改上面的文件,而建议修改/etc/network/interfaces文件。下面是个正确的例子:

[plain]  view plain copy print ?
  1. # This file describes the network interfaces available on your system  
  2. # and how to activate them. For more information, see interfaces(5).  
  3.   
  4. # The loopback network interface  
  5. auto lo  
  6. iface lo inet loopback  
  7.   
  8. # The primary network interface  
  9. auto eth0  
  10. iface eth0 inet static  
  11.       address 10.112.18.106  
  12.       network 10.112.18.0  
  13.       netmask 255.255.255.0  
  14.       broadcast 10.112.18.255  
  15.       gateway 10.112.18.254  
  16.       dns-nameservers 10.112.18.1  

就是最后一行dns-nameservers,可以添加多个,用空格分开


上面我用的是静态IP地址,很多时候需要使用DHCP动态分配IP, 这个时候dns-nameservers仍然能够使用,不过优先级比DHCP提供的dns要低。可以改变这个优先级,通过修改/etc/resolvconf/interface-order配置文件。


现在用nslookup来查找dns,一切正常:

[plain]  view plain copy print ?
  1. nslookup google.com  
  2. Server:     10.112.18.1  
  3. Address:    10.112.18.1#53  
  4.   
  5. Non-authoritative answer:  
  6. Name:   google.com  
  7. Address: 74.125.224.229  
  8. Name:   google.com  
  9. Address: 74.125.224.224  
  10. Name:   google.com  
  11. Address: 74.125.224.238  
  12. Name:   google.com  
  13. Address: 74.125.224.230  
  14. Name:   google.com  
  15. Address: 74.125.224.232  
  16. Name:   google.com  
  17. Address: 74.125.224.227  
  18. Name:   google.com  
  19. Address: 74.125.224.233  
  20. Name:   google.com  
  21. Address: 74.125.224.231  
  22. Name:   google.com  
  23. Address: 74.125.224.225  
  24. Name:   google.com  
  25. Address: 74.125.224.226  
  26. Name:   google.com  
  27. Address: 74.125.224.228  

但是,我的一台服务器用上面的方法仍然有故障。

用下面的命令没有问题,interfaces文件中的dns配置会被自动复制到/etc/resolv.conf文件中

[plain]  view plain copy print ?
  1. ifdown eth0; ifup eth0  

但是如果用service networking restart就不行了。

解决方法是:

1.确定安装了resolvconf

[plain]  view plain copy print ?
  1. apt-get install resolvconf  
2. 重新配置resolvconf

[plain]  view plain copy print ?
  1. dpkg-reconfigure resolvconf  
确认框中都选择yes.

3.然后重新启动,问题解决。

Ubuntu 系统中,即使没有设置静态 IP 地址,仍然可以配置 DNSUbuntu 默认通过 DHCP 获取网络配置,其中包括 DNS 服务器的地址。DHCP 服务器可以在分配 IP 地址的同时提供 DNS 配置信息,例如默认的网关、子网掩码和 DNS 服务器地址等。因此,在没有静态 IP 的情况下,DNS 配置依然可以正常工作[^2]。 ### 配置 DNS 的方法 1. **通过 `resolv.conf` 文件手动设置 DNS** Ubuntu 系统中,DNS 配置通常由 `/etc/resolv.conf` 文件管理。如果希望手动配置 DNS,可以编辑该文件并添加所需的 DNS 服务器地址。例如: ```bash sudo nano /etc/resolv.conf ``` 添加以下内容以配置 Google 的公共 DNS: ``` nameserver 8.8.8.8 nameserver 8.8.4.4 ``` 请注意,如果系统仍然使用 DHCP 分配 IP 地址,则 `resolv.conf` 文件可能会被覆盖。为了避免这种情况,可以禁用 `resolvconf` 服务或将其配置为只读模式。 2. **使用 `netplan` 或 `interfaces` 文件配置 DNS** 对于 Ubuntu Server 16.04 及更高版本,可以通过网络配置文件(如 `/etc/network/interfaces` 或 `/etc/netplan/` 中的 YAML 文件)指定 DNS 服务器。例如,在 `interfaces` 文件中,可以添加如下内容: ``` auto eth0 iface eth0 inet dhcp dns-nameservers 8.8.8.8 8.8.4.4 ``` 如果使用 `netplan`,则需要编辑 YAML 文件,例如: ```yaml network: version: 2 renderer: networkd ethernets: enp3s0: dhcp4: yes nameservers: addresses: - 8.8.8.8 - 8.8.4.4 ``` 保存后运行 `sudo netplan apply` 以应用更改。 3. **使用 `systemd-resolved` 配置 DNS** 在某些 Ubuntu 版本中,`systemd-resolved` 被用作 DNS 解析器。可以通过编辑 `/etc/systemd/resolved.conf` 文件来配置 DNS: ``` [Resolve] DNS=8.8.8.8 DNS=8.8.4.4 ``` 保存后重启 `systemd-resolved` 服务: ```bash sudo systemctl restart systemd-resolved ``` ### 注意事项 - 在使用 DHCP 获取 IP 地址时,确保 DHCP 服务器提供了正确的 DNS 配置。 - 如果手动配置 DNS,请确保 `/etc/resolv.conf` 不被其他服务(如 `resolvconf` 或 `systemd-resolved`)覆盖。 - 检查当前 DNS 配置是否生效,可以使用命令 `nslookup example.com` 或 `dig example.com` 进行验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值