ubuntu20.04——网卡及DNS配置

该博客介绍了如何在Ubuntu系统中配置网卡和DNS。首先,通过提权到root用户,使用vim编辑网卡配置文件`/etc/netplan/00-installer-config.yaml`,设置IP地址、子网掩码、网关和DNS。然后应用配置并验证连接。接着,修改`/etc/systemd/resolved.conf`文件以设置DNS,并启用并重启`systemd-resolved`服务。最后,通过创建软链接确保DNS设置生效。注意,直接编辑`/etc/resolv.conf`可能会因文件被管理系统覆盖而失效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网卡配置

ubuntu@p006:~$ 			# 普通用户
ubuntu@p006:~$ sudo su -			# 提权root
[sudo] password for ubuntu:
(venv) root@p006:~# vim /etc/netplan/00-installer-config.yaml			# 编辑网卡
# This is the network config written by 'subiquity'
network:
  ethernets:
    eno1:
      addresses:
      - 10.12.1.6/21
      gateway4: 10.12.1.254
      nameservers:
        addresses:
        - 114.114.114.114
        search: []
  version: 2

(venv) root@p006:~# netplan apply			# 将当前网络计划配置应用于运行中的系统  

DNS配置

(venv) root@p006:/etc# vim /etc/systemd/resolved.conf
DNS=114.114.114.114

(venv) root@p006:/etc# systemctl restart systemd-resolved
(venv) root@p006:/etc# systemctl enable systemd-resolved
(venv) root@p006:/etc# mv /etc/resolv.conf /etc/resolv.conf.bak
(venv) root@p006:/etc# ln -s /run/systemd/resolve/resolv.conf /etc/

(venv) root@p006:/etc# ping baidu.com
PING baidu.com (220.181.38.251) 56(84) bytes of data.
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=1 ttl=47 time=15.0 ms

还有一种方法,结果就是重启之后就失效了,内容也还原了,原因就是 /etc/resolv.conf 文件中的第一行写了 “Do not edit”

(venv) root@p006:~# vim /etc/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 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 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 127.0.0.53
options edns0 trust-ad
nameserver 114.114.114.114

(venv) root@p006:/etc# systemctl restart systemd-resolved
### Ubuntu 20.04配置网卡Ubuntu Server 20.04 中,通过编辑位于 `/etc/netplan/` 下的 YAML 文件来完成网卡配置。通常情况下,这个文件名为 `00-installer-config.yaml` 或者类似的名称[^1]。 #### 编辑 Netplan 配置文件 打开终端使用具有管理员权限的文本编辑器(如 Vim),输入如下命令: ```bash sudo vim /etc/netplan/00-installer-config.yaml ``` 根据需求调整 `.yaml` 文件中的参数设置。一个典型的静态 IP 地址配置可能看起来像这样[^3]: ```yaml network: version: 2 renderer: networkd ethernets: enp3s0: dhcp4: no addresses: - 192.168.1.100/24 gateway4: 192.168.1.1 nameservers: addresses: [8.8.8.8, 8.8.4.4] ``` 在这个例子中: - `enp3s0` 是要配置的具体网络接口名; - 关闭 DHCP (`dhcp4: no`) 手动指定 IPv4 地址及其子网掩码长度 (CIDR 表示法); - 设置默认路由到给定的网关地址; - 定义两个 Google 的公共 DNS 服务器作为域名解析服务提供商。 保存所做的更改后退出编辑模式。 #### 应用新的网络配置 为了使新设定生效,在大多数情形下只需要运行下面这条指令即可应用最新的 netplan 配置而无需重启整个机器: ```bash sudo netplan apply ``` 对于某些特殊情况或是当遇到问题时,则可以通过重启 NetworkManager 来强制刷新所有连接状态[^2]: ```bash sudo systemctl restart NetworkManager ``` 或者直接重载 systemd 网络栈: ```bash sudo systemctl daemon-reload && sudo systemctl restart systemd-networkd ``` 以上就是如何在 Ubuntu 20.04配置网卡以及怎样重启相关服务的过程概述。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值