双网卡 pve8.0.3 lxc 运行 openwrt作为主路由 最靠谱简单教程

在pve上运行openwrt作为主路由使用。无论单个网卡或者多个网卡其实操作都操作。
lxc运行的openwrt 对虚拟机方式运行的性能更好,资源占用更低。

我目前是有两台pve 均为单网卡。 一台 x240主板 15-20w i3-4030U 8G 128G+1T。 上面lxc 运行 openwrt做主路由,同时在lxc里面运行了几个开源软件做核心数据的nas. 另外虚拟机运行了一个win 和一个备用mac(x240内存8G跑太多不够玩)
一台 qnct组装机 20-60w 6核32G nvme 500G+12T pve装了桌面系统做日用机。并且挂了4块硬盘 也开了nas,虚拟运行win直通显卡打游戏 macos直通核显偶尔签app用。

# 场景

# pve单网卡

pve 单个网卡 接入交换机 光猫 桥接 接入交换机 其他设备 包括ap等同样接入交换机。 光猫通常插到交换机的第一个口上,不过家用交换机一般都是傻瓜式的,随便插都可以。 这是最常用的方案,top图大概如下 ,就是所有设备都接到交换机就ok

1
2
3
4
5
交换机 <====> 光猫 这里光猫是桥接
交换机 <====> 其他设备
交换机 <====> 其他设备
交换机 <====> 其他设备
交换机 <====> pve物理网卡===> pve vmbr0 ===> lxc和kvm

vmbr0 你可以视作一个虚拟万兆交换机就好理解了。pve默认用你在/etc/network/interfaces配置的ip,lxc和kvm内用你指定或者网内任意一个 使用虚拟机的情况下 pve内 就是 vmbr0 映射一个虚拟网卡到 到虚拟机。然后虚拟内部配置静态ip的和pve以及交换机上的其他设备在同一个网段就可以完成互相访问了。 那么到lxc的话也是一样。但是到openwrt的因为需要一个lan一个wan,所以我们这里直接映射两个虚拟网卡给openwrt就好了。

1
2
vmbr0 ===> net0 === lxc openwrt eth0做lan
vmbr0 ===> net1 === lxc openwrt eth1做wan

反应到lxc的配置文件里面就是

1
2
net0: name=eth0,bridge=vmbr0,hwaddr=随便填写个MAC地址,type=veth
net1: name=eth1,bridge=vmbr0,hwaddr=随便再来个MAC地址,type=veth

当然你也可以去openewrt内部做网桥来弄,但是完全没有必要。

# pve多网卡

pve 多个网卡,直接直通网卡给openwrt即可。 下面的例子是pve双网卡 板载接入交换机 usb网卡接入光猫。 pve 为单板载网卡enp0s25,但是有一个usb3千兆网卡enx0023545ca007。为例,多网卡一样操作。
在pve中 左侧 点数据中心下的pve ,然后点 中间的 系统下面的网络 ,查看这两个网口的名称。
板载网卡做为pve管理口 插入交换机 局域网其他设备也插入普通交换机。 usb网卡作为链接外网的网卡 插入光猫。 记住这usb3网口的名字即可。下文统一以 enx0023545ca007 代替。

# 步骤

# 获取 openwrt rootfs包

可以从IMG.GZ/SQUASHFS.IMG.GZ 提取,或者直接下载 ROOTFS.TAR.GZ 包。 从IMG.GZ/SQUASHFS.IMG.GZ 提取 提取的思路:解压gz 挂载img/SQUASHFS ,打包挂载后的文件到 ROOTFS.TAR.GZ 建议直接用 ROOTFS.TAR.GZ 即可。 我这里用 https://openwrt.ai/ supe提供的 ROOTFS.TAR.GZ

# rootfs包上传到 pve

pve 后台找到 CT模板 点上传。我这里是手动下载到pve的 /var/lib/vz/template/cache/ 都是一样的

# 创建一个CT/lxc

pve目前依旧无法直接创建op的lxc,这里还是用命令行。

1
pct create 100 local:vztmpl/openwrt-09.26.2023-x86-64-generic-rootfs.tar.gz --rootfs local:1 --ostype unmanaged --hostname OP --arch amd64 --cores 2 --memory 1024 --swap 0 -net0 bridge=vmbr0,name=eth0

# 解释

  • pct create 100 是pve里面CT或者VM的id值,你可以修改成你的
  • local:vztmpl/openwrt-09.26.2023-x86-64-generic-rootfs.tar.gz 是上面你上传到rootfs的包名
  • rootfs local:1 是本lxc储存位置和硬盘占用大小限制1G 如果用其他地方储存 rootfs local-lvm:1 rootfs local-brtfs:0.5
  • ostype unmanaged 操作系统类型
  • hostname OP 节点名称
  • arch amd64 cpu平台
  • cores 2 可占用的cpu核心数
  • memory 1024 swap 0 内存和交换内存,建议关闭交换内存 也就是0
  • net0 bridge=vmbr0,name=eth0 把pve的管理网桥给op用,也就是op把他作为 lan

# 手动修改配置

# 2-N网卡直通一个进去

cat /etc/pve/nodes/pve/lxc/100.conf 添加下面几行

1
2
3
4
5
6
7
8
9
lxc.include: /usr/share/lxc/config/openwrt.common.conf
lxc.net.1.type: phys
lxc.net.1.link: enx0023545ca007
lxc.net.1.flags: up
lxc.net.1.name: eth1
lxc.cgroup2.devices.allow: c 108:0 rwm
lxc.mount.entry: /dev/ppp dev/ppp none bind,create=file
lxc.mount.entry: /dev/net/tun dev/net/tun none bind,create=file
lxc.cap.drop:
# 完整配置
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
arch: amd64
cores: 2
hostname: OP
memory: 1024
net0: name=eth0,bridge=vmbr0,hwaddr=8A:8C:94:9B:ED:A4,type=veth
onboot: 0
ostype: unmanaged
rootfs: local:100/vm-100-disk-1.raw,size=1G
startup: up=2
swap: 0
lxc.include: /usr/share/lxc/config/openwrt.common.conf
lxc.net.1.type: phys
lxc.net.1.link: enx0023545ca007
lxc.net.1.flags: up
lxc.net.1.name: eth1
lxc.cgroup2.devices.allow: c 108:0 rwm
lxc.mount.entry: /dev/ppp dev/ppp none bind,create=file
lxc.mount.entry: /dev/net dev/net none bind,create=dir
lxc.cap.drop:
# 解释
  • lxc.include 包含pve自带的op配置模板
  • lxc.net.1 直通网卡enx0023545ca007进去作为 eth1
  • lxc.cgroup2 的配置,这是lxc运行一些服务类的系统必须的
  • lxc.mount.entry 挂载ppoe和tun到lxc内
  • lxc.cap.drop: 取消 openwrt.common.conf 里面 对 cap的限制,不然openclash无法使用

# 单网卡

# 完整配置
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
arch: amd64
cores: 2
hostname: OP
memory: 1024
net0: name=eth0,bridge=vmbr0,hwaddr=8A:8C:94:9B:ED:A4,type=veth
net1: name=eth1,bridge=vmbr0,hwaddr=8A:8C:94:9B:ED:A3,type=veth
onboot: 1
ostype: unmanaged
rootfs: local:100/vm-100-disk-1.raw,size=1G
startup: up=2
swap: 0
lxc.include: /usr/share/lxc/config/openwrt.common.conf
lxc.cgroup2.devices.allow: c 108:0 rwm
lxc.mount.entry: /dev/ppp dev/ppp none bind,create=file
lxc.mount.entry: /dev/net dev/net none bind,create=dir
lxc.cap.drop:
# 解释

两个虚拟网卡直接映射进去就可以了e .

# 启动

pct start 100

# 目前发现的问题

# NAT 环回 无效

简单的说,就是内网使用ddns的域名访问内网的服务器,无法访问。
目前尚不知道如何解决。貌似和openwrt的版本有关系,在使用git-24.286.72457-5518424编译 并且未在openwrt内部使用docker的情况下此问题正常。 但是在更新的版本中又再次出现。 临时解决方法有两种

# 别名主机

别名主机可以在 openwrt 网络 -> dncp -> 主机名映射的地方 添加域名到对应的内网机器ip上。然后清理本地dns缓存后即可访问。也可以在openwrt里面运行

1
2
3
4
5
uci add dhcp domain
uci set dhcp.@domain[-1].name='nas.leiyanhui.com'
uci add_list dhcp.@domain[-1].ip='10.1.1.50'
uci commit dhcp
/etc/init.d/dnsmasq restart
# 自定域名解析

如果你有使用adguardHome 之类的自建dns,可以在dns内添加对应的域名解析记录到内网ip。或者修改需要访问的的电脑的hosts文件。

# openclash tun无法使用

暂时不知道如何解决。

# 部分情况下 udp无法转发

需要PVE系统加载 xt_TPROXY 模块。在PVE系统下执行改命令后重启。

1
echo 'xt_TPROXY' > /etc/modules-load.d/tproxy.conf

# 偶尔发生pve外机器无法上网

后续发现是ap的原因,重启ap后解决。 如果你的ap刷过系统,可以在ap内检测主路由状态,如果ping不到lxc的主路由,那么重启即可。

# openclash配置参考

/etc/config/openclash 注意下文中的 testingcf.jsdelivr.net 地址在部分地区可能已经干扰,请

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
config openclash 'config'
	option proxy_port '7892'
	option tproxy_port '7895'
	option mixed_port '7893'
	option socks_port '7891'
	option http_port '7890'
	option dns_port '7874'
	option enable '1'
	option update '0'
	option en_mode 'redir-host'
	option auto_update '1'
	option cn_port '9090'
	option dashboard_password '123456'
	option dashboard_forward_ssl '0'
	option rule_source '0'
	option enable_custom_dns '1'
	option ipv6_enable '1'
	option ipv6_dns '1'
	option enable_custom_clash_rules '0'
	option other_rule_auto_update '0'
	option core_version 'linux-amd64'
	option enable_redirect_dns '1'
	option servers_if_update '0'
	option disable_masq_cache '1'
	option servers_update '0'
	option log_level '0'
	option proxy_mode 'rule'
	option intranet_allowed '1'
	option disable_udp_quic '1'
	option lan_ac_mode '0'
	option operation_mode 'redir-host'
	option enable_rule_proxy '0'
	option redirect_dns '1'
	option cachesize_dns '1'
	option filter_aaaa_dns '1'
	option small_flash_memory '0'
	option interface_name '0'
	option common_ports '21 22 23 53 80 123 143 194 443 465 587 853 993 995 998 2052 2053 2082 2083 2086 2095 2096 5222 5228 5229 5230 8080 8443 8880 8888 8889'
	option log_size '1024'
	option tolerance '0'
	option store_fakeip '0'
	option custom_fallback_filter '0'
	option custom_fakeip_filter '0'
	option custom_host '0'
	option custom_name_policy '0'
	option append_wan_dns '1'
	option stream_domains_prefetch '0'
	option stream_auto_select '0'
	option bypass_gateway_compatible '0'
	option github_address_mod '自定义的github代理地址'
	option urltest_address_mod '0'
	option urltest_interval_mod '0'
	option delay_start '15'
	option router_self_proxy '1'
	option release_branch 'master'
	option enable_meta_core '1'
	option dashboard_type 'Official'
	option yacd_type 'Official'
	option append_default_dns '0'
	option geo_custom_url 'https://testingcf.jsdelivr.net/gh/alecthw/mmdb_china_ip_list@release/lite/Country.mmdb'
	option chnr_custom_url 'https://ispip.clang.cn/all_cn.txt'
	option chnr6_custom_url 'https://ispip.clang.cn/all_cn_ipv6.txt'
	option cndomain_custom_url 'https://testingcf.jsdelivr.net/gh/felixonmars/dnsmasq-china-list@master/accelerated-domains.china.conf'
	option default_resolvfile '/tmp/resolv.conf.d/resolv.conf.auto'
	option find_process_mode '0'
	option global_client_fingerprint '0'
	option geodata_loader '0'
	option enable_geoip_dat '0'
	option enable_meta_sniffer '1'
	option enable_meta_sniffer_pure_ip '1'
	option enable_meta_sniffer_custom '0'
	option config_reload '1'
	option config_auto_update_mode '1'
	option config_update_interval '45'
	option config_path '/etc/openclash/config/skyvpn.yaml'
	option restricted_mode '0'
	option dnsmasq_resolvfile '/tmp/resolv.conf.d/resolv.conf.auto'
	option enable_custom_domain_dns_server '0'
	option china_ip_route '1'
	option geo_auto_update '1'
	option geo_update_week_time '*'
	option geo_update_day_time '6'
	option geoip_auto_update '1'
	option geosite_auto_update '1'
	option chnr_auto_update '1'
	option chnr_update_week_time '*'
	option chnr_update_day_time '3'
	option auto_restart '1'
	option auto_restart_week_time '*'
	option auto_restart_day_time '13'
	option core_type 'Meta'
	option enable_udp_proxy '1'
	option geoip_update_week_time '*'
	option geoip_update_day_time '7'
	option geoip_custom_url 'https://testingcf.jsdelivr.net/gh/Loyalsoldier/v2ray-rules-dat@release/geoip.dat'
	option geosite_update_week_time '*'
	option geosite_update_day_time '8'
	option geosite_custom_url 'https://testingcf.jsdelivr.net/gh/Loyalsoldier/v2ray-rules-dat@release/geosite.dat'
	option dnsmasq_noresolv '0'
	option ipv6_mode '1'
	option enable_v6_udp_proxy '1'
	option china_ip6_route '0'
	option dnsmasq_cachesize '10000'
	option dnsmasq_filter_aaaa '0'

config dns_servers
	option group 'nameserver'
	option type 'udp'
	option ip '114.114.114.114'
	option enabled '0'

config dns_servers
	option group 'nameserver'
	option type 'udp'
	option ip '119.29.29.29'
	option enabled '0'

config dns_servers
	option group 'nameserver'
	option type 'udp'
	option ip '119.28.28.28'
	option enabled '0'

config dns_servers
	option group 'nameserver'
	option type 'udp'
	option ip '223.5.5.5'
	option enabled '0'

config dns_servers
	option type 'https'
	option ip 'doh.pub/dns-query'
	option group 'nameserver'
	option enabled '0'

config dns_servers
	option type 'https'
	option ip 'dns.alidns.com/dns-query'
	option group 'nameserver'
	option enabled '0'

config dns_servers
	option type 'https'
	option group 'fallback'
	option ip 'dns.cloudflare.com/dns-query'
	option enabled '0'

config dns_servers
	option group 'fallback'
	option ip 'dns.google'
	option port '853'
	option type 'tls'
	option enabled '0'

config dns_servers
	option group 'fallback'
	option type 'https'
	option ip '1.1.1.1/dns-query'
	option enabled '0'

config dns_servers
	option group 'fallback'
	option ip '1.1.1.1'
	option port '853'
	option type 'tls'
	option enabled '0'

config dns_servers
	option enabled '0'
	option group 'fallback'
	option ip '8.8.8.8'
	option port '853'
	option type 'tls'

config dns_servers
	option type 'udp'
	option group 'fallback'
	option ip '2001:4860:4860::8888'
	option port '53'
	option enabled '0'

config dns_servers
	option type 'udp'
	option group 'fallback'
	option ip '2001:4860:4860::8844'
	option port '53'
	option enabled '0'

config dns_servers
	option type 'udp'
	option group 'fallback'
	option ip '2001:da8::666'
	option port '53'
	option enabled '0'

config dns_servers
	option group 'fallback'
	option type 'https'
	option ip 'public.dns.iij.jp/dns-query'
	option enabled '0'

config dns_servers
	option group 'fallback'
	option type 'https'
	option ip 'jp.tiar.app/dns-query'
	option enabled '0'

config dns_servers
	option group 'fallback'
	option type 'https'
	option ip 'jp.tiarap.org/dns-query'
	option enabled '0'

config dns_servers
	option group 'fallback'
	option ip 'jp.tiar.app'
	option type 'tls'
	option enabled '0'

config dns_servers
	option group 'fallback'
	option ip 'dot.tiar.app'
	option type 'tls'
	option enabled '0'

config dns_servers
	option enabled '0'
	option group 'fallback'
	option type 'https'
	option ip 'doh.dnslify.com/dns-query'

config dns_servers
	option enabled '0'
	option group 'fallback'
	option ip 'dns.twnic.tw/dns-query'
	option type 'https'

config dns_servers
	option enabled '0'
	option group 'fallback'
	option ip 'dns.oszx.co/dns-query'
	option type 'https'

config dns_servers
	option enabled '0'
	option group 'fallback'
	option ip 'doh.applied-privacy.net/query'
	option type 'https'

config dns_servers
	option enabled '0'
	option group 'fallback'
	option ip 'dnsforge.de/dns-query'
	option type 'https'

config dns_servers
	option enabled '0'
	option group 'fallback'
	option ip 'doh.ffmuc.net/dns-query'
	option type 'https'

config dns_servers
	option enabled '0'
	option group 'fallback'
	option type 'https'
	option ip 'doh.mullvad.net/dns-query'

config authentication
	option enabled '1'
	option username 'Clash'
	option password 'socket合同http(s)代理密码'

config dns_servers
	option enabled '1'
	option group 'nameserver'
	option type 'udp'
	option ip '10.1.1.104'
	option interface 'Disable'
	option node_resolve '0'
	option specific_group 'Disable'

config dns_servers
	option enabled '1'
	option group 'fallback'
	option type 'udp'
	option ip '10.1.1.104'
	option interface 'Disable'
	option node_resolve '0'
	option specific_group 'Disable'

config dns_servers
	option enabled '1'
	option group 'default'
	option type 'udp'
	option ip '10.1.1.104'
	option interface 'Disable'
	option node_resolve '0'

config config_subscribe
	option enabled '1'
	option name 'skyvpn'
	option address '你的订阅地址'
	option sub_convert '1'
	option convert_address 'https://api.dler.io/sub'
	option template 'ACL4SSR 规则 Online Full'
	option emoji 'false'
	option udp 'false'
	option skip_cert_verify 'false'
	option sort 'false'
	option node_type 'false'
	option rule_provider 'false'
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计