AWS Ubuntu设置DNS解析(解决resolve.conf被覆盖问题)

众所周知:

Ubuntu在域名解析时,最直接使用的是/etc/resolve.conf文件,它是/run/systemd/resolve/resolve.conf的软链接,而对于刚装完的ubuntu系统,该文件的内容如下

bash 复制代码
ubuntu@ip-172-31-36-184:/etc$ cat resolv.conf
# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients directly to
# all known uplink DNS servers. This file lists all configured search domains.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 172.31.0.2
search ap-northeast-1.compute.internal

第1步, 修改/etc/systemd/resolved.conf,例如:

谷歌的dns=8.8.8.8 cloudflare的dns: 1.1.1.1

bash 复制代码
ubuntu@ip-172-31-36-184:/etc/systemd$ ls
journald.conf  logind.conf  network  networkd.conf  pstore.conf  resolved.conf  sleep.conf  system  system.conf  timesyncd.conf  user  user.conf
ubuntu@ip-172-31-36-184:/etc/systemd$ cat resolved.conf 
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See resolved.conf(5) for details

[Resolve]
DNS=8.8.8.8 1.1.1.1
#FallbackDNS=
#Domains=
#LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#DNSOverTLS=no
#Cache=no-negative
#DNSStubListener=yes
#ReadEtcHosts=yes

个人猜测,这个时候,系统在往/run/systemd/resolve/resolv.conf里面写域名解析服务器地址的时候,会从/etc/systemd/resolved.conf中取得DNS相关的配置。

复制代码
    2,重启域名解析服务

            systemctl restart systemd-resolved
            systemctl enable systemd-resolved

    3,备份当前的/etc/resolve.conf,并重新设置/run/systemd/resolve/resolv.conf  到/etc/resolve.conf的软链接

            mv    /etc/resolv.conf    /etc/resolv.conf.bak
            ln  -s   /run/systemd/resolve/resolv.conf    /etc/

验证:

bash 复制代码
ubuntu@ip-172-31-36-184:/etc$ cat resolv.conf
# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients directly to
# all known uplink DNS servers. This file lists all configured search domains.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 8.8.8.8
nameserver 1.1.1.1
nameserver 172.31.0.2
search ap-northeast-1.compute.internal

参考:
1-华为云开发者论坛

2-csdn https://blog.csdn.net/booklijian/article/details/116491288

相关推荐
debug 小菜鸟1 小时前
浏览器访问 AWS ECS 上部署的 Docker 容器(监听 80 端口)
docker·云计算·aws
地衣君2 小时前
RISC-V 开发板 + Ubuntu 23.04 部署 open_vins 过程
linux·ubuntu·risc-v
爱学习的小道长3 小时前
Ubuntu Cursor升级成v1.0
linux·运维·ubuntu
EelBarb3 小时前
seafile:ubuntu搭建社区版seafile12.0
linux·运维·ubuntu
Xam_d_LM3 小时前
【Latex】Windows/Ubuntu 绘制 eps 矢量图通用方法(drawio),支持插入 Latex 数学公式
linux·ubuntu·科研·矢量图·drawio
可观测性用观测云6 小时前
AWS EKS 集群日志上报观测云实践
aws
upp8 小时前
【bug】Error: /undefinedfilename in (/tmp/ocrmypdf.io.9xfn1e3b/origin.pdf)
ubuntu·pdf·bug·ghostscript
行云流水剑9 小时前
【学习记录】在 Ubuntu 中将新硬盘挂载到 /home 目录的完整指南
服务器·学习·ubuntu
藥瓿亭9 小时前
K8S认证|CKS题库+答案| 7. Dockerfile 检测
运维·ubuntu·docker·云原生·容器·kubernetes·cks
xiangyong5811 小时前
ubuntu系统文件误删(/lib/x86_64-linux-gnu/libc.so.6)修复方案 [成功解决]
linux·ubuntu·gnu