Linux /etc/resolv.conf 配置DNS

本文介绍Linux系统中resolv.conf文件的关键配置项,包括nameserver、domain和search等,解释这些配置如何影响DNS查询过程。

文件/etc/resolv.conf配置DNS客户,它包含了主机的域名搜索顺序和DNS服务器的地址,每一行应包含一个关键字和一个或多个的由空格隔开的参数。下面是一个例子文件: 

  search mydom.edu.cn
  nameserver 210.34.0.14
  nameserver 210.34.0.2

  合法的参数及其意义如下:


  nameserver 表明DNS服务器的IP地址。可以有很多行的nameserver,每一个带一个IP地址。在查询时就按nameserver在本文件中的顺序进行,且只有当第一个nameserver没有反应时才查询下面的nameserver。 

  domain   声明主机的域名。很多程序用到它,如邮件系统;当为没有域名的主机进行DNS查询时,也要用到。如果没有域名,主机名将被使用,删除所有在第一个点( .)前面的内容。 

  search   它的多个参数指明域名查询顺序。当要查询没有域名的主机,主机将在由search声明的域中分别查找。domain和search不能共存;如果同时存在,后面出现的将会被使用。 

  sortlist  允许将得到域名结果进行特定的排序。它的参数为网络/掩码对,允许任意的排列顺序。 

  Red Hat中没有提供缺省的/etc/resolv.conf文件,它的内容是根据在安装时给出的选项动态创建的。

原文链接:
 Linux下/etc/resolv.conf 配置DNS客户

在/etc/resolv.conf 配置文件:domain 和search作用是一样的

下面的说明更清晰

domain linpro.no
search linpro.no uio.no ifi.uio.no

domain function:

Had I typed telnet math.uio.no. with the trailing dot, the resolver would have known it was an FQDN and would have looked up math.uio.no at once, without trying to append the specified domain first. Not all applications are tolerant of the trailing dot, though, so it can't always be specified.

search function:

When ssh gram is executed, the resolver first looks for gram.linpro.no, which does not exist; then gram.uio.no, which does not exist, either; and finally gram.ifi.uio.no, which will succeed because it does exist

原文链接:/etc/resolv.conf中关键字search和domain的作用

关于domain和search与/etc/hosts的区别在《鸟哥的linux私房菜(网络架设)》第十九章 DNS服务器 小节:Client 端的设定 中有说:
· /etc/hosts :刚刚上面就提过了,这个是最早的 hostname 对应 IP 的档案; 

· /etc/resolv.conf :这个就是设定你 Client 端连上 DNS 主机的 IP 设定文件; 

· /etc/nsswitch.conf:这个档案则是在『决定』先要使用 /etc/hosts 还是 /etc/resolv.conf的设定!

  一般而言, Linux 的预设 hostname 搜寻都是先 /etc/hosts 来的,为什么呢?你可以查看一下
/etc/nsswitch.conf ,并找到 hosts 的项目: 

[root@linux ~]# vi /etc/nsswitch.conf hosts: files dns
  上面那个 files 就是使用 /etc/hosts 而最后的 dns 则是使用 /etc/resolv.conf 的 DNS 主机 IP搜 
寻啦!因此,你可以先以 /etc/hosts 来设定 IP 对应ㄋㄟ!当然啦,你也可以将他调换过来,不过,总是
/etc/hosts 比较简单,所以将他摆在前面比较好啦!

  好啦,既然我们是要进行 DNS 测试的,那么 /etc/resolv.conf 的内容,自然就要填写我们自己的IP
啰!所以你应该这样写: 

[root@linux ~]# vi /etc/resolv.conf nameserver 192.168.1.254 nameserver 168.95.1.1 nameserver 139.175.10.20
  nameserver 可以设定多个,但是由于 nameserver 是一个一个的追查下来的,所以,你的 DNS 主机的 IP 要写在最上面喔!然后就可以开始测试了

<think>在Linux中,清空`/etc/resolv.conf`文件中的DNS配置通常意味着删除所有`nameserver`行,但保留其他配置(如`domain`、`search`等)可能不是我们想要的。不过,根据问题,我们只关注清空DNS配置(即所有`nameserver`行)。但请注意,有时我们可能需要完全清空文件,或者仅删除`nameserver`行而保留其他内容。 这里提供几种方法: 1. **完全清空文件**:这将删除文件的所有内容,但通常我们不希望这样,因为文件中可能包含其他配置(如由`resolvconf`生成的头信息)。 2. **仅删除所有`nameserver`行**:这样会保留其他配置。 由于问题要求清空DNS配置,而DNS配置在`resolv.conf`中主要通过`nameserver`行来指定,因此我们选择删除所有`nameserver`行。 我们可以使用以下方法(假设需要以编程方式,且具有root权限): ### 方法1:使用sed命令(简单高效) ```bash sudo sed -i '/^nameserver /d' /etc/resolv.conf ``` 解释: - `-i`:直接修改文件 - `'/^nameserver /d'`:删除以`nameserver `开头的行(注意后面有一个空格,避免匹配到注释等) - 如果需要不区分大小写,可以加上`I`选项(但注意不同sed版本的差异): ```bash sudo sed -i '/^nameserver /Id' /etc/resolv.conf # GNU sed ``` 但是,有些系统可能使用`nameserver`后面没有空格(虽然标准格式有空格),为了更通用,我们可以匹配行首的`nameserver`后面可能跟有空格或制表符: ```bash sudo sed -i -E '/^\s*nameserver\s+/d' /etc/resolv.conf ``` ### 方法2:使用Qt/C++编程实现(如果需要集成到Qt应用程序中) 以下是一个使用Qt的示例函数,它删除`/etc/resolv.conf`中所有以`nameserver`开头的行(不区分大小写,并允许前导空白): ```cpp #include <QFile> #include <QTextStream> #include <QRegularExpression> #include <QDebug> bool clearDnsConfiguration() { QString filePath = "/etc/resolv.conf"; QFile file(filePath); // 备份文件(可选) if (!QFile::copy(filePath, filePath + ".backup")) { qWarning() << "Failed to create backup"; // 可以选择继续,但备份失败可能意味着权限问题 } // 打开文件读取 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { qWarning() << "Cannot open file for reading:" << file.errorString(); return false; } QTextStream in(&file); QStringList lines; QRegularExpression regex("^\\s*nameserver\\s+", QRegularExpression::CaseInsensitiveOption); while (!in.atEnd()) { QString line = in.readLine(); if (!regex.match(line).hasMatch()) { lines.append(line); } } file.close(); // 重新打开文件以写入 if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) { qWarning() << "Cannot open file for writing:" << file.errorString(); return false; } QTextStream out(&file); for (const QString &line : lines) { out << line << "\n"; } file.close(); return true; } ``` 说明: - 使用正则表达式`^\\s*nameserver\\s+`匹配以任意空白开头,然后是`nameserver`,后面至少有一个空白(避免匹配到注释中的`nameserver`)的行。 - 不区分大小写(`QRegularExpression::CaseInsensitiveOption`)。 - 先读取所有行,过滤后再写回文件(使用`Truncate`模式覆盖原文件)。 ### 方法3:使用临时文件替换(更安全) 在写入时,可以先写入一个临时文件,然后替换原文件,这样可以避免在写入过程中出错导致原文件损坏。 ```cpp #include <QFile> #include <QSaveFile> #include <QTextStream> #include <QRegularExpression> #include <QDebug> bool clearDnsConfigurationSafe() { QString filePath = "/etc/resolv.conf"; QFile inputFile(filePath); if (!inputFile.open(QIODevice::ReadOnly | QIODevice::Text)) { qWarning() << "Cannot open file for reading:" << inputFile.errorString(); return false; } QTextStream in(&inputFile); QRegularExpression regex("^\\s*nameserver\\s+", QRegularExpression::CaseInsensitiveOption); QStringList lines; while (!in.atEnd()) { QString line = in.readLine(); if (!regex.match(line).hasMatch()) { lines.append(line); } } inputFile.close(); // 使用QSaveFile进行安全写入(原子操作) QSaveFile saveFile(filePath); if (!saveFile.open(QIODevice::WriteOnly | QIODevice::Text)) { qWarning() << "Cannot open for writing:" << saveFile.errorString(); return false; } QTextStream out(&saveFile); for (const QString &line : lines) { out << line << "\n"; } // 提交更改,如果失败则回滚 if (!saveFile.commit()) { qWarning() << "Failed to commit changes:" << saveFile.errorString(); return false; } return true; } ``` 注意:`QSaveFile`在提交时会使用原子替换(通过重命名),这可以防止在写入过程中发生崩溃导致文件损坏。 ### 权限问题 由于`/etc/resolv.conf`通常属于root用户,普通应用程序需要提升权限。在图形界面程序中,可以使用`pkexec`(或其他授权机制)来运行一个帮助程序,或者使用`sudo`。 在Qt中,可以使用`QProcess`以root权限运行命令: ```cpp void clearDnsWithPrivilege() { QProcess process; process.start("pkexec", {"sed", "-i", "/^\\s*nameserver\\s+/Id", "/etc/resolv.conf"}); process.waitForFinished(-1); if (process.exitCode() != 0) { qDebug() << "Error:" << process.readAllStandardError(); } else { qDebug() << "Success"; } } ``` 或者使用我们之前编写的C++程序,然后通过`pkexec`运行它。 ### 注意事项 1. 在修改系统文件前,最好先备份。 2. 注意文件权限和所有权。 3. 如果系统使用`resolvconf`服务管理`/etc/resolv.conf`,则直接修改文件可能不是持久的(重启网络服务后可能被覆盖)。在这种情况下,应该通过`resolvconf`的接口来管理。 根据需求选择合适的方法。 §§相关问题§§: 1. 如何防止在修改系统配置文件时出现权限不足的问题? 2.Linux中,如何使对`/etc/resolv.conf`的修改在重启网络服务后仍然有效? 3. 使用`QSaveFile`相比直接使用`QFile`有什么优势? 4. 在Qt中,如何以跨平台的方式修改系统文件(特别是Windows和macOS)?</think>### 清空 /etc/resolv.conf 中的 DNS 配置Linux 中清空 `/etc/resolv.conf` 中的 DNS 配置有几种方法,以下是完整的解决方案: #### 方法 1:使用命令行工具(推荐) ```bash # 使用 sed 删除所有 nameserver 行 sudo sed -i '/^nameserver\s/d' /etc/resolv.conf # 或者完全清空文件(保留其他配置) sudo sed -i '/nameserver/d' /etc/resolv.conf # 完全清空文件内容(谨慎使用) echo "" | sudo tee /etc/resolv.conf ``` #### 方法 2:使用 Qt/C++ 实现 ```cpp #include <QFile> #include <QTextStream> #include <QRegularExpression> #include <QProcess> #include <QDebug> bool clearResolvDnsConfig() { const QString filePath = "/etc/resolv.conf"; // 备份原文件 QDateTime now = QDateTime::currentDateTime(); QString backupPath = filePath + ".bak_" + now.toString("yyyyMMdd_hhmmss"); if (!QFile::copy(filePath, backupPath)) { qWarning() << "无法创建备份文件"; return false; } // 读取文件内容 QFile file(filePath); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { qWarning() << "无法打开文件:" << file.errorString(); return false; } // 删除所有 nameserver 行 QStringList filteredLines; QTextStream in(&file); QRegularExpression regex("^\\s*nameserver\\b", QRegularExpression::CaseInsensitiveOption); while (!in.atEnd()) { QString line = in.readLine(); if (!regex.match(line).hasMatch()) { filteredLines.append(line); } } file.close(); // 写入修改后的内容 if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) { qWarning() << "无法写入文件:" << file.errorString(); return false; } QTextStream out(&file); for (const QString &line : filteredLines) { out << line << "\n"; } file.close(); return true; } // 需要 root 权限的版本 void secureClearDnsConfig() { QProcess process; process.start("pkexec", {"bash", "-c", "cp /etc/resolv.conf /etc/resolv.conf.bak && " "sed -i '/^\\s*nameserver\\s/d' /etc/resolv.conf" }); if (!process.waitForFinished(5000)) { qWarning() << "操作超时"; return; } if (process.exitCode() != 0) { qWarning() << "错误: " << process.readAllStandardError(); } else { qDebug() << "DNS 配置已清空"; } } ``` #### 方法 3:使用系统命令的 Qt 封装 ```cpp void clearDnsWithSystemCommand() { QProcess process; process.start("pkexec", {"sed", "-i", "/^\\s*nameserver\\s/d", "/etc/resolv.conf"}); process.waitForFinished(3000); if (process.exitCode() == 0) { qDebug() << "DNS 配置已成功清空"; } else { qWarning() << "错误代码:" << process.exitCode(); qWarning() << "错误输出:" << process.readAllStandardError(); } } ``` ### 代码解释 #### 1. 正则表达式匹配 ```cpp QRegularExpression regex("^\\s*nameserver\\b", QRegularExpression::CaseInsensitiveOption); ``` - `^\\s*`:匹配行首的任意空白(包括空格和制表符) - `nameserver\\b`:匹配完整的 "nameserver" 单词(避免匹配部分单词) - `CaseInsensitiveOption`:不区分大小写 #### 2. 文件处理流程 1. 创建备份文件 2. 读取文件内容 3. 过滤掉所有 nameserver 行 4. 将剩余内容写回文件 5. 使用 `QIODevice::Truncate` 清空原文件内容 #### 3. 权限处理 - 使用 `pkexec` 获取 root 权限 - 错误处理包括超时检测和退出代码检查 - 捕获并显示标准错误输出 ### 替代方法:使用临时文件 ```cpp void clearDnsWithTempFile() { QTemporaryFile tempFile; if (!tempFile.open()) { qWarning() << "无法创建临时文件"; return; } // 读取原文件并过滤 QFile inputFile("/etc/resolv.conf"); if (inputFile.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream in(&inputFile); QTextStream out(&tempFile); QRegularExpression regex("^\\s*nameserver\\b", QRegularExpression::CaseInsensitiveOption); while (!in.atEnd()) { QString line = in.readLine(); if (!regex.match(line).hasMatch()) { out << line << "\n"; } } inputFile.close(); } tempFile.close(); // 使用 root 权限替换原文件 QProcess process; process.start("pkexec", {"cp", tempFile.fileName(), "/etc/resolv.conf"}); process.waitForFinished(3000); if (process.exitCode() == 0) { qDebug() << "DNS 配置已清空"; } else { qWarning() << "错误: " << process.readAllStandardError(); } } ``` ### 系统注意事项 1. **文件权限问题**: - `/etc/resolv.conf` 通常属于 root 用户 - 普通应用需要提升权限才能修改 - 使用 `ls -l /etc/resolv.conf` 检查文件所有权 2. **系统兼容性**: ```bash # 不同发行版处理方式 # Systemd-resolved sudo systemd-resolve --reset-server-features # NetworkManager nmcli con mod <connection-name> ipv4.dns "" ``` 3. **配置恢复**: ```bash # 恢复默认配置 sudo ln -sf /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf # 或使用 DHCP 配置 sudo dhclient -r && sudo dhclient ``` ### 验证 DNS 配置已清空 ```cpp bool isDnsCleared() { QFile file("/etc/resolv.conf"); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { return false; } QTextStream in(&file); QRegularExpression regex("^\\s*nameserver\\b", QRegularExpression::CaseInsensitiveOption); while (!in.atEnd()) { if (regex.match(in.readLine()).hasMatch()) { return false; } } return true; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值