sudo systemd-resolve --set-dns添加多个dns
时间: 2023-10-08 08:04:46 浏览: 356
可以通过在命令中多次使用 `--set-dns` 参数来添加多个 DNS 服务器。例如,假设你要添加两个 DNS 服务器 8.8.8.8 和 8.8.4.4,可以使用以下命令:
```
sudo systemd-resolve --set-dns 8.8.8.8 --set-dns 8.8.4.4
```
这将覆盖当前的 DNS 服务器并设置新的 DNS 服务器。请注意,这些更改可能会在下次系统重启时丢失,除非将其写入系统配置文件。
相关问题
ubuntu如何配置dns服务器
### Ubuntu 配置 DNS 服务器教程
#### 编辑 `/etc/systemd/resolved.conf` 文件
为了设置全局的DNS解析,可以编辑`/etc/systemd/resolved.conf`文件并添加所需的DNS服务器IP地址。具体操作是在该配置文件中的 `[Resolve]`部分下加入如下内容来指定首选和备用DNS服务器:
```ini
[Resolve]
DNS=119.29.29.29 223.5.5.5 223.6.6.6
```
完成上述更改之后,需重启 `systemd-resolved.service`服务以便让新设定生效[^1]。
#### 设置主机名与静态 IP 地址映射关系
对于作为DNS服务器运行的机器而言,应当为其分配固定的主机名称以及对应的内部网络IP地址。这可以通过修改`/etc/hosts`文件实现。例如,在名为ns1的服务器上应执行以下指令以更新其主机名,并向`/etc/hosts`追加相应的记录条目:
```bash
sudo hostnamectl set-hostname ns1.hwdomain.io
echo "192.168.202.128 ns1.hwdomain.io ns1" | sudo tee -a /etc/hosts
```
同样地,针对另一台命名为ns2的服务器也采取相同的操作流程[^2]。
#### 更新本地解析器配置 (`resolv.conf`)
尽管通过systemd管理的主要解析程序已经指定了外部DNS服务器列表,但在某些情况下可能还需要直接调整位于`/etc/resolv.conf`内的nameserver参数。此过程涉及将目标DNS服务器的信息写入到这个文件里去,像这样:
```plaintext
nameserver 127.0.0.53 # 这通常是systemd-resolved监听端口
nameserver 114.114.114.114
nameserver 8.8.8.8 # Google Public DNS
options edns0 trust-ad # 启用EDNS支持和其他选项
```
值得注意的是,默认情况下`/etc/resolv.conf`可能是由其他守护进程动态维护着;因此如果手动改动后发现变化未被保存,则考虑查阅相关文档了解更详细的定制方法[^3]。
#### BIND (Berkeley Internet Name Domain) 的安装与初步配置
BIND是一款广泛使用的开源DNS软件包。要将其部署于Ubuntu系统之上,首先要确保已正确安装bind9及相关工具集。接着按照官方指导说明对默认配置做出适当修改——特别是`/etc/bind/named.conf.options`里的forwarders字段用来声明上游转发的目标地址集合。比如采用Google提供的公共DNS服务为例:
```bash
sudo apt update && sudo apt install bind9 bind9utils bind9-doc
# 接下来编辑named.conf.options文件...
sudo nano /etc/bind/named.conf.options
```
找到并取消注释forwarders区块的内容,替换为期望查询请求所指向的一组权威性更强的DNS节点:
```apache
forwarders {
8.8.8.8;
8.8.4.4;
};
```
最后记得重新加载或启动BIND服务使这些变更立即起效[^4]。
D:\JAVAPROJECT\shixun\vue3.0-template-admin-main>yarn yarn install v1.22.22 $ npx only-allow yarn [1/4] Resolving packages... [2/4] Fetching packages... info There appears to be trouble with your network connection. Retrying... info There appears to be trouble with your network connection. Retrying... info There appears to be trouble with your network connection. Retrying... info There appears to be trouble with your network connection. Retrying... error Error: getaddrinfo ENOTFOUND registry.nlark.com at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:122:26) info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
### 解决 Yarn 安装时网络连接问题以及特定错误代码的解决方案
#### 1. 确保正确的镜像源配置
当遇到 `error ECONNREFUSED` 或 `ENOTFOUND` 错误时,可能是因为当前使用的镜像源无法访问。可以尝试将镜像源切换为国内的淘宝源或其他可用源。使用以下命令设置淘宝源:
```bash
yarn config set registry https://registry.npmmirror.com
```
如果需要切换回官方源,可以运行以下命令:
```bash
yarn config set registry https://registry.yarnpkg.com
```
此外,对于某些特定依赖(如 `node-sass`),可以通过设置其二进制文件的下载地址来避免网络问题:
```bash
yarn config set sass-binary-site http://npm.taobao.org/mirrors/node-sass
```
此方法适用于解决与 `node-sass` 相关的网络超时问题[^4]。
#### 2. 检查代理设置
如果本地启用了 HTTP 或 HTTPS 代理,可能会导致 Yarn 的请求被拒绝。可以通过以下命令检查当前的代理配置:
```bash
yarn config get proxy
yarn config get https-proxy
```
如果输出了代理地址,则需要禁用代理以避免冲突:
```bash
yarn config delete proxy
yarn config delete https-proxy
```
此操作可以移除代理配置,从而避免因代理导致的连接失败[^1]。
#### 3. 增加超时时间
Yarn 默认的网络超时时间可能不足以完成某些依赖的下载。可以通过增加超时时间来解决此问题:
```bash
yarn install --network-timeout 600000
```
如果该方法有效,可以将超时时间配置到 `.yarnrc` 文件中,以便在后续安装中自动应用:
```yaml
network-timeout 600000
```
此方法特别适用于网络条件较差的情况[^5]。
#### 4. 手动下载并安装依赖
如果某些依赖项始终无法通过 Yarn 自动下载,可以手动下载并放置到正确的位置。例如,对于 Electron 依赖,可以在淘宝镜像上下载对应版本的文件,并解压到 `node_modules/electron/dist` 目录下。
#### 5. 检查 DNS 和防火墙设置
DNS 解析失败或防火墙阻止可能导致 `ENOTFOUND` 错误。可以尝试修改系统的 DNS 设置为公共 DNS 服务器(如 Google DNS 或阿里云 DNS)。此外,清除 DNS 缓存也可能有所帮助:
```bash
sudo systemd-resolve --flush-caches
```
或者在 Windows 系统中运行:
```cmd
ipconfig /flushdns
```
#### 6. 使用离线模式
如果网络环境极其不稳定,可以考虑使用 Yarn 的离线模式。确保所有依赖项已预先下载后,运行以下命令:
```bash
yarn install --offline
```
此方法仅适用于依赖项已在缓存中存在的情况[^3]。
---
```python
# 示例代码:检查当前 Yarn 配置
import subprocess
def check_yarn_config():
try:
result = subprocess.run(['yarn', 'config', 'list'], capture_output=True, text=True)
print(f"Current Yarn configuration:\n{result.stdout.strip()}")
except Exception as e:
print(f"Error checking Yarn configuration: {e}")
check_yarn_config()
```
---
阅读全文
相关推荐







