Linux-云平臺(tái)主機(jī)新增DNS

一、環(huán)境說明

  • IP是由DHCP獲取的,由NetworkManager管理,要永久修改DNS,不能直接編輯 ifcfg-eth0 或 resolv.conf 文件(重啟后會(huì)被覆蓋)
[root@localhost  ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 
# Created by cloud-init on instance boot automatically, do not edit.
#
BOOTPROTO=dhcp
DEVICE=eth0
DHCPV6C=yes
IPV6INIT=yes
IPV6_AUTOCONF=no
ONBOOT=yes
TYPE=Ethernet
USERCTL=no
[root@localhost  ~]# 
[root@localhost  ~]# cat /etc/resolv.conf 
# Generated by NetworkManager
search cluster.local
nameserver 114.114.114.114
nameserver 8.8.8.8
[root@localhost  ~]#

二、解決方法

第一步:找到正確的連接名稱
運(yùn)行以下命令來列出所有 NetworkManager 連接:
nmcli connection show
或者使用簡寫:

nmcli c s
輸出會(huì)是一個(gè)表格,其中 NAME 列下的就是你需要的連接名稱。它很可能是 eth0、Wired connection 1、System eth0 或類似的名稱。

# 1. 將當(dāng)前DNS替換為新的列表(會(huì)覆蓋原有的DNS)
sudo nmcli connection modify eth0 ipv4.dns "223.5.5.5 1.1.1.1"
# 或 2. 在原有DNS基礎(chǔ)上追加新的DNS(保留原有的)
sudo nmcli connection modify eth0 +ipv4.dns "223.5.5.5"
sudo nmcli connection modify eth0 +ipv4.dns "1.1.1.1"

# 確保配置不被DHCP覆蓋
sudo nmcli connection modify eth0 ipv4.ignore-auto-dns yes

# 重新激活連接使更改生效
sudo nmcli connection up eth0

三、實(shí)操

[root@localhost ~]# nmcli c s
NAME         UUID                                  TYPE      DEVICE 
System eth0  5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03  ethernet  eth0   
[root@localhost ~]# 
[root@localhost ~]# nmcli connection modify "System eth0" +ipv4.dns "110.123.2.5"
[root@localhost ~]# nmcli connection modify "System eth0" +ipv4.dns "110.123.74.253"
[root@localhost ~]# nmcli connection modify "System eth0" ipv4.ignore-auto-dns yes
[root@localhost ~]# nmcli connection up "System eth0"
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2)

[root@localhost ~]# 
[root@localhost ~]# ping gng.cqff.hsip.gov.cn
PING gng.cqff.hsip.gov.cn (110.123.34.60) 56(84) bytes of data.
64 bytes from 110.123.34.60 (110.123.34.60): icmp_seq=1 ttl=52 time=7.57 ms
64 bytes from 110.123.34.60 (110.123.34.60): icmp_seq=2 ttl=52 time=6.62 ms
64 bytes from 110.123.34.60 (110.123.34.60): icmp_seq=3 ttl=52 time=6.34 ms
64 bytes from 110.123.34.60 (110.123.34.60): icmp_seq=4 ttl=52 time=6.47 ms
64 bytes from 110.123.34.60 (110.123.34.60): icmp_seq=5 ttl=52 time=6.44 ms
64 bytes from 110.123.34.60 (110.123.34.60): icmp_seq=6 ttl=52 time=6.32 ms
64 bytes from 110.123.34.60 (110.123.34.60): icmp_seq=7 ttl=52 time=6.50 ms

^C
--- ggng.cq.hsip.gov.cn ping statistics ---
15 packets transmitted, 15 received, 0% packet loss, time 14021ms
rtt min/avg/max/mdev = 6.322/6.545/7.567/0.302 ms
[root@localhost ~]# 

四、配置后變化

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 
# Created by cloud-init on instance boot automatically, do not edit.
#
BOOTPROTO=dhcp
DEVICE=eth0
DHCPV6C=yes
IPV6INIT=yes
IPV6_AUTOCONF=no
ONBOOT=yes
TYPE=Ethernet
USERCTL=no
PROXY_METHOD=none
BROWSER_ONLY=no
DNS1=110.123.2.5
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME="System eth0"
UUID=5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03
DNS2=110.123.74.253
PEERDNS=no
[root@localhost ~]# 
[root@localhost ~]# cat /etc/resolv.conf 
# Generated by NetworkManager
nameserver 110.123.2.5
nameserver 110.123.74.253
[root@localhost ~]#
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容