This is /run/systemd/resolve/stub-resolv.conf managed by man:systemd-resolved(8). Do not edit. This file might be symlinked as /etc/resolv.conf. If you're looking at /etc/resolv.conf and seeing this text, you have followed the symlink. This is a dynamic resolv.conf file for connecting local clients to the internal DNS stub resolver of systemd-resolved. This file lists all configured search domains. Run "resolvectl status" to see details about the uplink DNS servers currently in use. Third party programs should typically 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 127.0.0.53 options edns0 trust-ad search openstacklocal 我的/etc/resolv.conf里面提示这些,而且我每次修改文件后,都会给重置成最初的样子。
时间: 2025-07-14 16:45:24 浏览: 10
<think>嗯,用户的问题是关于他们系统中的/etc/resolv.conf文件被自动重置的情况。根据用户提供的内容,文件是由systemd-resolved管理的,并且每次修改后都会被恢复原状。我需要先确认用户对systemd-resolved的理解程度,可能他们不太清楚这个服务是如何管理DNS配置的。
首先,我应该解释/etc/resolv.conf在Linux中的作用,通常它负责配置DNS服务器。但自从systemd-resolved出现后,这个文件可能被符号链接到系统的动态生成文件,比如/run/systemd/resolve/stub-resolv.conf。用户可能尝试手动编辑这个文件,但发现修改无效,因为systemd-resolved会自动覆盖它。
接下来,用户的需求可能是想自定义DNS设置,比如更改DNS服务器或搜索域。我需要提供正确的配置方法,而不是直接编辑被管理的文件。可能需要引导用户使用resolvectl命令或者修改/etc/systemd/resolved.conf配置文件,然后重启服务。此外,如果用户确实需要静态的resolv.conf,可以替换符号链接为自定义文件,但需要提醒他们这样可能影响systemd-resolved的功能。
还要考虑用户的操作系统版本,是否使用NetworkManager或其他网络管理工具,这些也可能影响DNS配置的方式。不过根据用户提供的信息,他们使用的是systemd-resolved,所以应集中在该服务的配置方法上。
需要确保回答结构清晰,分步骤说明原因和解决方法。同时,避免使用复杂术语,用中文简明解释。可能需要指出三种处理方式:正确配置systemd-resolved、绕过它使用静态文件,或者使用其他网络管理工具。这样用户可以根据自己的情况选择合适的方法。
最后,提醒用户操作前备份文件,以防万一。确保所有命令准确,并解释每个步骤的作用,比如修改配置文件后重启服务的重要性。同时,强调不推荐直接编辑动态生成的文件,而是通过系统提供的工具进行配置,这样更安全可靠。</think>你遇到的问题是由于系统使用 `systemd-resolved` 服务管理 DNS 配置导致的。这个服务会动态控制 `/etc/resolv.conf` 文件,导致手动修改被重置。以下是分步解析和解决方案:
---
### 一、现象原因
1. **符号链接机制**
`/etc/resolv.conf` 实际上是一个符号链接(symlink),指向 `/run/systemd/resolve/stub-resolv.conf`。
```bash
$ ls -l /etc/resolv.conf # 查看符号链接状态
```
该文件由 `systemd-resolved` 服务动态生成,直接修改会被覆盖。
2. **systemd-resolved 的工作模式**
`systemd-resolved` 是 Linux 系统管理 DNS 的核心服务,支持以下模式:
- **Stub Resolver 模式**(默认):将本地程序 DNS 请求转发到上游 DNS。
- **Static 模式**:允许手动配置 `/etc/resolv.conf`。
- **其他模式**:如与 NetworkManager 集成。
---
### 二、解决方案(3 种方法)
#### 方法 1:通过 systemd-resolved 正确配置 DNS
1. **使用 `resolvectl` 命令**
查看当前 DNS 状态:
```bash
$ resolvectl status
```
设置 DNS 服务器(例如设为 `8.8.8.8`):
```bash
$ sudo resolvectl dns <接口名称> 8.8.8.8
```
2. **修改配置文件 `/etc/systemd/resolved.conf`**
```ini
[Resolve]
DNS=8.8.8.8 1.1.1.1 # 自定义 DNS 服务器
Domains=example.com # 自定义搜索域
FallbackDNS=9.9.9.9
```
重启服务生效:
```bash
$ sudo systemctl restart systemd-resolved
```
---
#### 方法 2:绕过 systemd-resolved(使用静态 resolv.conf)
1. **删除符号链接**
```bash
$ sudo rm /etc/resolv.conf
```
2. **创建静态文件**
```bash
$ sudo nano /etc/resolv.conf
```
输入自定义配置:
```plaintext
nameserver 8.8.8.8
search example.com
```
3. **锁定文件防止被修改**
```bash
$ sudo chattr +i /etc/resolv.conf # 解除锁定时用 `chattr -i`
```
---
#### 方法 3:通过 NetworkManager 配置(如果系统使用)
```bash
$ nmcli connection modify <连接名称> ipv4.dns "8.8.8.8 1.1.1.1"
$ nmcli connection up <连接名称>
```
---
### 三、关键注意事项
1. **兼容性问题**
直接使用静态 `resolv.conf` 可能导致 `systemd-resolved` 的部分功能失效(如 DNSSEC 验证)。
2. **恢复默认配置**
若要恢复 `systemd-resolved` 管理:
```bash
$ sudo ln -sf /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf
```
3. **文件权限问题**
操作前建议备份原文件:
```bash
$ sudo cp /etc/resolv.conf /etc/resolv.conf.bak
```
---
### 四、原文件内容解读
你提供的文件内容表明:
- `nameserver 127.0.0.53`:DNS 请求被转发到本地 `systemd-resolved` 服务。
- `options edns0 trust-ad`:启用 EDNS 扩展和接受 DNSSEC 验证结果。
- `search openstacklocal`:自动补全的域名后缀(常用于私有云环境)。
如需更深入的调试,可结合 `systemd-resolved` 日志:
```bash
$ journalctl -u systemd-resolved
```
阅读全文
相关推荐

















