Linux 临时修改和永久修改DNS的方法

前言

以下为手动修改DNS的两种方法;
也可以使用博主的一键脚本进行设置:
设置虚拟内存及修改系统DNS一键脚本:https://suntl.com/1239.html

开始

查看当前DNS配置信息:

cat /etc/resolv.conf

修改DNS(临时修改,重启失效)

# 编辑文件
vi /etc/resolv.conf

# 加入需要的DNS:
nameserver 8.8.8.8
nameserver 8.8.4.4

如果多个DNS,就一行一个,修改之后保存退出即可
此方法修改后即刻生效,但重启后失效;

查看是否已经生效:

cat /etc/resolv.conf

如果已经变成了你设置的DNS,那就设置成功了。

Ubuntu 20.04/22.04

Ubuntu 20.04/22.04系统中DNS设置是由systemd-resolved进行接管
修改resolv.conf文件会提示你:
This file is managed by man:systemd-resolved(8). Do not edit.
这时直接修改resolv.conf文件会被动态覆盖,因而使修改失效;
这种情况应该修改resolved.conf配置文件

修改resolved.conf配置文件

vi /etc/systemd/resolved.conf

删除DNS前面的注释符#添加你需要的DNS,然后保存退出

DNS=1.1.1.1 8.8.8.8

重启解析服务

systemctl restart systemd-resolved

重新设置run/systemd/resolve/resolv.confetc/resolve.conf的软链接;

mv /etc/resolv.conf /etc/resolv.conf.bak

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

查看是否已经生效:

cat /etc/resolv.conf

此方法修改后重启服务器会持续生效;

其它:也可通过修改/etc/netplan目录下的配置文件进行修改;

nslookup

nslookup是一种网络管理命令行工具,可用于查询DNS域名和IP地址
安装nslookup

#Ubuntu
apt-get install dnsutils
    
#Debian
apt-get update
apt-get install dnsutils
    
#Centos
yum install bind-utils

使用方法:

nslookup www.baidu.com
    
说明
    
nslookup 你需要解析的域名

示例:

root@ubuntu:~# nslookup www.baidu.com
Server:         8.8.8.8
Address:        8.8.8.8#53

Non-authoritative answer:
www.baidu.com   canonical name = www.a.shifen.com.
www.a.shifen.com        canonical name = www.wshifen.com.
Name:   www.wshifen.com
Address: 104.193.88.77
Name:   www.wshifen.com
Address: 104.193.88.123
最后修改:2024 年 08 月 15 日

发表评论

1 条评论

  1. momo

    我使用第二个方法,为什么我执行到"使 DNS 生效"报错没有这个目录或文件呢