1
0
Files
frost-zx.github.io/docs/content/ubuntu-20-04-netplan.md
2025-10-13 10:20:34 +08:00

95 lines
2.0 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: "记一次 Ubuntu 20.04 双网卡配置过程"
date: 2025-03-16T15:59:37Z
lastmod: 2025-03-16T15:59:55Z
tags: [网络,Linux,Ubuntu,系统,配置]
---
# 记一次 Ubuntu 20.04 双网卡配置过程
## 情况说明
现有如图所示的网络结构(非专业网络拓扑图,仅供参考)
![网络结构](assets/network-asset-01-20250316155958-gldjlz4.png "网络结构")
其中:
- 主路由有两个 DHCP 服务器IP 为 10.32.1.1 和 10.32.2.1)。
- 子路由设置了 DMZ 主机指向服务器192.168.148.100)。
- 网卡A - `enp6s0`网卡B - `ens4`
注意:
- 一台主机只配置一个默认网关,若有多个,会自动选择优先级较高的一个进行通信。
因此,需要手动给网卡配置静态路由。
## 要求
- 服务器可以访问子路由、主路由,以及外网。
- 连接到主路由的设备能通过 IP 10.32.1.34 和 10.32.2.1 访问服务器。
## 参考资料
- [Netplan](https://netplan.io/reference/)
## 用到的命令
```text
ip a
sudo netplan apply
sudo service systemd-networkd restart
sudo ip route show
sudo netstat -rn
traceroute -m 10 -n www.baidu.com
```
## 配置
修改 `/etc/netplan/00-installer-config.yaml`(文件名不一定是这个)
### 方案一
服务器通过主路由访问外网
```yaml
network:
renderer: networkd
ethernets:
ens4:
dhcp4: true
enp6s0:
dhcp4: false
addresses: [192.168.148.100/24]
routes:
- to: 192.168.148.0/24
via: 192.168.148.1
- to: 10.32.2.0/24
via: 192.168.148.1
version: 2
```
### 方案二
服务器通过子路由访问外网
> 一般来说,使用方案一就可以了(已测试),但在部分网络环境下需要使用方案二这样的配置(原因未知,猜测可能与路由器设置有关)。
```yaml
network:
renderer: networkd
ethernets:
ens4:
dhcp4: false
addresses: [10.32.1.29/24]
enp6s0:
dhcp4: true
routes:
- to: 192.168.148.0/24
via: 192.168.148.1
version: 2
```