@xdev-asia/xdev-knowledge-mcp 1.0.53 → 1.0.55
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/content/blog/ai/con-duong-tro-thanh-ai-solution-architect.md +826 -0
- package/content/pages/ve-toi.md +62 -0
- package/content/series/devsecops/vyos-tu-co-ban-den-nang-cao/chapters/01-vyos-tu-co-ban-den-nang-cao/lessons/01-bai-1-gioi-thieu-vyos-va-cai-dat.md +324 -0
- package/content/series/devsecops/vyos-tu-co-ban-den-nang-cao/chapters/01-vyos-tu-co-ban-den-nang-cao/lessons/02-bai-2-cau-hinh-interface-va-ip-co-ban.md +324 -0
- package/content/series/devsecops/vyos-tu-co-ban-den-nang-cao/chapters/01-vyos-tu-co-ban-den-nang-cao/lessons/03-bai-3-nat-source-nat-destination-nat-va-masquerade.md +321 -0
- package/content/series/devsecops/vyos-tu-co-ban-den-nang-cao/chapters/01-vyos-tu-co-ban-den-nang-cao/lessons/04-bai-4-firewall-co-ban-rules-chains-va-groups.md +386 -0
- package/content/series/devsecops/vyos-tu-co-ban-den-nang-cao/chapters/01-vyos-tu-co-ban-den-nang-cao/lessons/05-bai-5-zone-based-firewall.md +451 -0
- package/content/series/devsecops/vyos-tu-co-ban-den-nang-cao/chapters/01-vyos-tu-co-ban-den-nang-cao/lessons/06-bai-6-dhcp-server-dns-forwarding-va-ntp.md +105 -0
- package/content/series/devsecops/vyos-tu-co-ban-den-nang-cao/chapters/01-vyos-tu-co-ban-den-nang-cao/lessons/07-bai-7-vlans-bonding-va-bridge.md +94 -0
- package/content/series/devsecops/vyos-tu-co-ban-den-nang-cao/chapters/01-vyos-tu-co-ban-den-nang-cao/lessons/08-bai-8-static-routing-va-policy-based-routing.md +83 -0
- package/content/series/devsecops/vyos-tu-co-ban-den-nang-cao/chapters/01-vyos-tu-co-ban-den-nang-cao/lessons/09-bai-9-dynamic-routing-ospf.md +79 -0
- package/content/series/devsecops/vyos-tu-co-ban-den-nang-cao/chapters/01-vyos-tu-co-ban-den-nang-cao/lessons/10-bai-10-dynamic-routing-bgp.md +82 -0
- package/content/series/devsecops/vyos-tu-co-ban-den-nang-cao/chapters/01-vyos-tu-co-ban-den-nang-cao/lessons/11-bai-11-vpn-wireguard-va-openvpn.md +85 -0
- package/content/series/devsecops/vyos-tu-co-ban-den-nang-cao/chapters/01-vyos-tu-co-ban-den-nang-cao/lessons/12-bai-12-vpn-ipsec-site-to-site.md +87 -0
- package/content/series/devsecops/vyos-tu-co-ban-den-nang-cao/chapters/01-vyos-tu-co-ban-den-nang-cao/lessons/13-bai-13-high-availability-vrrp-va-conntrack-sync.md +61 -0
- package/content/series/devsecops/vyos-tu-co-ban-den-nang-cao/chapters/01-vyos-tu-co-ban-den-nang-cao/lessons/14-bai-14-wan-load-balancing-qos-va-monitoring.md +62 -0
- package/content/series/devsecops/vyos-tu-co-ban-den-nang-cao/chapters/01-vyos-tu-co-ban-den-nang-cao/lessons/15-bai-15-containers-automation-va-production-best-practices.md +80 -0
- package/content/series/devsecops/vyos-tu-co-ban-den-nang-cao/index.md +410 -0
- package/package.json +1 -1
- package/content/pages/gioi-thieu.md +0 -23
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: 019d65ef-d36f-773e-bf0a-9e30df834552
|
|
3
|
+
title: 'Bài 2: Cấu hình Interface và IP cơ bản'
|
|
4
|
+
slug: bai-2-cau-hinh-interface-va-ip-co-ban
|
|
5
|
+
description: >-
|
|
6
|
+
Hướng dẫn cấu hình interfaces ethernet, IP tĩnh, DHCP client, quản lý users,
|
|
7
|
+
hostname, timezone, NTP, DNS và backup/restore config trên VyOS.
|
|
8
|
+
duration_minutes: 120
|
|
9
|
+
is_free: true
|
|
10
|
+
video_url: null
|
|
11
|
+
sort_order: 2
|
|
12
|
+
section_title: "VyOS từ Cơ bản đến Nâng cao"
|
|
13
|
+
course:
|
|
14
|
+
id: 019d65ef-d36f-773e-bf0a-9e2efc5e19df
|
|
15
|
+
title: VyOS từ Cơ bản đến Nâng cao
|
|
16
|
+
slug: vyos-tu-co-ban-den-nang-cao
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
<h2>Cấu hình Ethernet Interfaces</h2>
|
|
20
|
+
|
|
21
|
+
<p>Trong VyOS, mỗi interface vật lý hoặc ảo được quản lý qua configuration tree tại node <code>interfaces</code>. Ethernet interface thường có tên <code>eth0</code>, <code>eth1</code>,... tương ứng với thứ tự NIC được kernel detect.</p>
|
|
22
|
+
|
|
23
|
+
<h3>Gán IP tĩnh cho interface</h3>
|
|
24
|
+
|
|
25
|
+
<pre><code class="language-bash">configure
|
|
26
|
+
|
|
27
|
+
# Gán IP cho eth0 (WAN)
|
|
28
|
+
set interfaces ethernet eth0 address 203.0.113.10/24
|
|
29
|
+
set interfaces ethernet eth0 description 'WAN - Internet'
|
|
30
|
+
|
|
31
|
+
# Gán IP cho eth1 (LAN)
|
|
32
|
+
set interfaces ethernet eth1 address 192.168.1.1/24
|
|
33
|
+
set interfaces ethernet eth1 description 'LAN - Internal'
|
|
34
|
+
|
|
35
|
+
# Xem thay đổi trước khi commit
|
|
36
|
+
compare
|
|
37
|
+
|
|
38
|
+
commit
|
|
39
|
+
save</code></pre>
|
|
40
|
+
|
|
41
|
+
<p>Một interface có thể có <strong>nhiều địa chỉ IP</strong> (secondary addresses):</p>
|
|
42
|
+
|
|
43
|
+
<pre><code class="language-bash">set interfaces ethernet eth1 address 192.168.1.1/24
|
|
44
|
+
set interfaces ethernet eth1 address 10.0.0.1/24</code></pre>
|
|
45
|
+
|
|
46
|
+
<h3>Cấu hình DHCP Client</h3>
|
|
47
|
+
|
|
48
|
+
<p>Khi interface WAN nhận IP từ ISP qua DHCP:</p>
|
|
49
|
+
|
|
50
|
+
<pre><code class="language-bash">set interfaces ethernet eth0 address dhcp
|
|
51
|
+
|
|
52
|
+
# Xem IP được cấp
|
|
53
|
+
show interfaces ethernet eth0</code></pre>
|
|
54
|
+
|
|
55
|
+
<h3>Disable/Enable Interface</h3>
|
|
56
|
+
|
|
57
|
+
<pre><code class="language-bash"># Disable interface
|
|
58
|
+
set interfaces ethernet eth2 disable
|
|
59
|
+
|
|
60
|
+
# Enable lại (xóa disable flag)
|
|
61
|
+
delete interfaces ethernet eth2 disable
|
|
62
|
+
|
|
63
|
+
commit</code></pre>
|
|
64
|
+
|
|
65
|
+
<h3>Xem trạng thái interfaces</h3>
|
|
66
|
+
|
|
67
|
+
<pre><code class="language-bash"># Operational mode
|
|
68
|
+
show interfaces
|
|
69
|
+
|
|
70
|
+
# Output mẫu:
|
|
71
|
+
# Codes: S - State, L - Link, u - Up, D - Down
|
|
72
|
+
# Interface IP Address S/L Description
|
|
73
|
+
# --------- ---------- --- -----------
|
|
74
|
+
# eth0 203.0.113.10/24 u/u WAN - Internet
|
|
75
|
+
# eth1 192.168.1.1/24 u/u LAN - Internal
|
|
76
|
+
# lo 127.0.0.1/8 u/u
|
|
77
|
+
|
|
78
|
+
# Xem chi tiết một interface
|
|
79
|
+
show interfaces ethernet eth0 brief</code></pre>
|
|
80
|
+
|
|
81
|
+
<h2>Quản lý Users và Authentication</h2>
|
|
82
|
+
|
|
83
|
+
<h3>Tạo user mới</h3>
|
|
84
|
+
|
|
85
|
+
<pre><code class="language-bash">configure
|
|
86
|
+
|
|
87
|
+
# Tạo user admin với plaintext password (VyOS sẽ tự hash)
|
|
88
|
+
set system login user admin authentication plaintext-password 'MyStr0ngP@ss!'
|
|
89
|
+
set system login user admin level admin
|
|
90
|
+
|
|
91
|
+
# Hoặc dùng encrypted password (đã hash sẵn)
|
|
92
|
+
# Tạo hash: openssl passwd -6 'MyStr0ngP@ss!'
|
|
93
|
+
set system login user admin authentication encrypted-password '$6$rounds=...'
|
|
94
|
+
|
|
95
|
+
commit
|
|
96
|
+
save</code></pre>
|
|
97
|
+
|
|
98
|
+
<blockquote>
|
|
99
|
+
<p><strong>Bảo mật</strong>: Sau khi commit, VyOS tự động chuyển plaintext-password thành encrypted-password trong config. Plaintext password không được lưu trữ.</p>
|
|
100
|
+
</blockquote>
|
|
101
|
+
|
|
102
|
+
<h3>Cấu hình SSH Key Authentication</h3>
|
|
103
|
+
|
|
104
|
+
<pre><code class="language-bash"># Thêm SSH public key cho user admin
|
|
105
|
+
set system login user admin authentication public-keys mykey@workstation type ssh-rsa
|
|
106
|
+
set system login user admin authentication public-keys mykey@workstation key 'AAAAB3NzaC1yc2EAAAA...'
|
|
107
|
+
|
|
108
|
+
commit
|
|
109
|
+
save</code></pre>
|
|
110
|
+
|
|
111
|
+
<h3>Xóa user</h3>
|
|
112
|
+
|
|
113
|
+
<pre><code class="language-bash">delete system login user old-admin
|
|
114
|
+
commit
|
|
115
|
+
save</code></pre>
|
|
116
|
+
|
|
117
|
+
<h2>Cấu hình System cơ bản</h2>
|
|
118
|
+
|
|
119
|
+
<h3>Hostname</h3>
|
|
120
|
+
|
|
121
|
+
<pre><code class="language-bash">configure
|
|
122
|
+
set system host-name vyos-gw01
|
|
123
|
+
set system domain-name lab.local
|
|
124
|
+
commit
|
|
125
|
+
save</code></pre>
|
|
126
|
+
|
|
127
|
+
<h3>Timezone</h3>
|
|
128
|
+
|
|
129
|
+
<pre><code class="language-bash"># Xem danh sách timezone
|
|
130
|
+
# Nhấn Tab sau 'set system time-zone'
|
|
131
|
+
set system time-zone Asia/Ho_Chi_Minh
|
|
132
|
+
commit
|
|
133
|
+
save</code></pre>
|
|
134
|
+
|
|
135
|
+
<h3>NTP — Đồng bộ thời gian</h3>
|
|
136
|
+
|
|
137
|
+
<pre><code class="language-bash"># Cấu hình NTP servers
|
|
138
|
+
set service ntp server pool.ntp.org
|
|
139
|
+
set service ntp server time.google.com
|
|
140
|
+
|
|
141
|
+
# Hoặc cấu hình chi tiết
|
|
142
|
+
set service ntp server 0.pool.ntp.org prefer
|
|
143
|
+
set service ntp server 1.pool.ntp.org
|
|
144
|
+
|
|
145
|
+
# Xem trạng thái NTP
|
|
146
|
+
show ntp
|
|
147
|
+
|
|
148
|
+
commit
|
|
149
|
+
save</code></pre>
|
|
150
|
+
|
|
151
|
+
<h3>System DNS (Name Server)</h3>
|
|
152
|
+
|
|
153
|
+
<p>Cấu hình DNS servers mà VyOS sẽ sử dụng để resolve tên miền:</p>
|
|
154
|
+
|
|
155
|
+
<pre><code class="language-bash"># DNS cho chính router
|
|
156
|
+
set system name-server 8.8.8.8
|
|
157
|
+
set system name-server 1.1.1.1
|
|
158
|
+
|
|
159
|
+
commit
|
|
160
|
+
save
|
|
161
|
+
|
|
162
|
+
# Kiểm tra
|
|
163
|
+
ping google.com</code></pre>
|
|
164
|
+
|
|
165
|
+
<h2>Bật SSH Service</h2>
|
|
166
|
+
|
|
167
|
+
<pre><code class="language-bash">configure
|
|
168
|
+
|
|
169
|
+
# Bật SSH trên port mặc định 22
|
|
170
|
+
set service ssh port 22
|
|
171
|
+
|
|
172
|
+
# Hoặc đổi port cho bảo mật
|
|
173
|
+
set service ssh port 2222
|
|
174
|
+
|
|
175
|
+
# Disable password login (chỉ cho key-based)
|
|
176
|
+
set service ssh disable-password-authentication
|
|
177
|
+
|
|
178
|
+
# Giới hạn SSH chỉ listen trên LAN interface
|
|
179
|
+
set service ssh listen-address 192.168.1.1
|
|
180
|
+
|
|
181
|
+
commit
|
|
182
|
+
save</code></pre>
|
|
183
|
+
|
|
184
|
+
<h2>Default Gateway và Static Routes</h2>
|
|
185
|
+
|
|
186
|
+
<pre><code class="language-bash">configure
|
|
187
|
+
|
|
188
|
+
# Đặt default gateway (cho WAN IP tĩnh)
|
|
189
|
+
set protocols static route 0.0.0.0/0 next-hop 203.0.113.1
|
|
190
|
+
|
|
191
|
+
# Thêm static route cho mạng khác
|
|
192
|
+
set protocols static route 10.10.0.0/16 next-hop 192.168.1.254
|
|
193
|
+
|
|
194
|
+
# Xem routing table
|
|
195
|
+
show ip route
|
|
196
|
+
|
|
197
|
+
commit
|
|
198
|
+
save</code></pre>
|
|
199
|
+
|
|
200
|
+
<h2>Backup và Restore Config</h2>
|
|
201
|
+
|
|
202
|
+
<h3>Save / Load config</h3>
|
|
203
|
+
|
|
204
|
+
<pre><code class="language-bash"># Save config hiện tại (mặc định lưu vào /config/config.boot)
|
|
205
|
+
save
|
|
206
|
+
|
|
207
|
+
# Save ra file cụ thể
|
|
208
|
+
save /config/backup-2026-04-07.config
|
|
209
|
+
|
|
210
|
+
# Load config từ file
|
|
211
|
+
load /config/backup-2026-04-07.config
|
|
212
|
+
# Xem thay đổi
|
|
213
|
+
compare
|
|
214
|
+
# Nếu OK thì commit
|
|
215
|
+
commit
|
|
216
|
+
save</code></pre>
|
|
217
|
+
|
|
218
|
+
<h3>Save config ra remote server</h3>
|
|
219
|
+
|
|
220
|
+
<pre><code class="language-bash"># Save qua SCP
|
|
221
|
+
save scp://user@backup-server/path/to/vyos-backup.config
|
|
222
|
+
|
|
223
|
+
# Save qua TFTP
|
|
224
|
+
save tftp://tftp-server/vyos-backup.config
|
|
225
|
+
|
|
226
|
+
# Save qua FTP
|
|
227
|
+
save ftp://user:password@ftp-server/vyos-backup.config</code></pre>
|
|
228
|
+
|
|
229
|
+
<h3>Rollback config</h3>
|
|
230
|
+
|
|
231
|
+
<p>VyOS lưu lịch sử các lần commit. Bạn có thể rollback:</p>
|
|
232
|
+
|
|
233
|
+
<pre><code class="language-bash"># Xem lịch sử commit
|
|
234
|
+
show system commit
|
|
235
|
+
|
|
236
|
+
# Rollback về revision trước
|
|
237
|
+
rollback 1
|
|
238
|
+
compare
|
|
239
|
+
commit
|
|
240
|
+
save</code></pre>
|
|
241
|
+
|
|
242
|
+
<h2>Lab thực hành: Setup Router 2 Interfaces WAN + LAN</h2>
|
|
243
|
+
|
|
244
|
+
<p>Topology:</p>
|
|
245
|
+
|
|
246
|
+
<pre><code class="language-bash">Internet --- [eth0 WAN: DHCP] VyOS Router [eth1 LAN: 192.168.100.1/24] --- LAN Clients</code></pre>
|
|
247
|
+
|
|
248
|
+
<h3>Bước 1: Cấu hình interfaces</h3>
|
|
249
|
+
|
|
250
|
+
<pre><code class="language-bash">configure
|
|
251
|
+
|
|
252
|
+
# WAN - nhận IP từ ISP
|
|
253
|
+
set interfaces ethernet eth0 description 'WAN'
|
|
254
|
+
set interfaces ethernet eth0 address dhcp
|
|
255
|
+
|
|
256
|
+
# LAN - IP tĩnh
|
|
257
|
+
set interfaces ethernet eth1 description 'LAN'
|
|
258
|
+
set interfaces ethernet eth1 address 192.168.100.1/24
|
|
259
|
+
|
|
260
|
+
commit</code></pre>
|
|
261
|
+
|
|
262
|
+
<h3>Bước 2: Cấu hình system</h3>
|
|
263
|
+
|
|
264
|
+
<pre><code class="language-bash"># Hostname
|
|
265
|
+
set system host-name vyos-home
|
|
266
|
+
|
|
267
|
+
# DNS
|
|
268
|
+
set system name-server 8.8.8.8
|
|
269
|
+
set system name-server 1.1.1.1
|
|
270
|
+
|
|
271
|
+
# Timezone
|
|
272
|
+
set system time-zone Asia/Ho_Chi_Minh
|
|
273
|
+
|
|
274
|
+
# NTP
|
|
275
|
+
set service ntp server pool.ntp.org
|
|
276
|
+
|
|
277
|
+
# SSH
|
|
278
|
+
set service ssh port 22
|
|
279
|
+
|
|
280
|
+
commit</code></pre>
|
|
281
|
+
|
|
282
|
+
<h3>Bước 3: Tạo user admin riêng</h3>
|
|
283
|
+
|
|
284
|
+
<pre><code class="language-bash"># Tạo admin user
|
|
285
|
+
set system login user admin authentication plaintext-password 'SecureP@ss2026!'
|
|
286
|
+
set system login user admin level admin
|
|
287
|
+
|
|
288
|
+
# Xóa default password của user vyos (đổi password)
|
|
289
|
+
set system login user vyos authentication plaintext-password 'NewVyOSP@ss!'
|
|
290
|
+
|
|
291
|
+
commit
|
|
292
|
+
save</code></pre>
|
|
293
|
+
|
|
294
|
+
<h3>Bước 4: Kiểm tra</h3>
|
|
295
|
+
|
|
296
|
+
<pre><code class="language-bash">exit
|
|
297
|
+
|
|
298
|
+
# Kiểm tra interfaces
|
|
299
|
+
show interfaces
|
|
300
|
+
|
|
301
|
+
# Kiểm tra DNS resolution
|
|
302
|
+
ping google.com
|
|
303
|
+
|
|
304
|
+
# Kiểm tra NTP
|
|
305
|
+
show ntp
|
|
306
|
+
|
|
307
|
+
# Kiểm tra config đã save
|
|
308
|
+
show configuration</code></pre>
|
|
309
|
+
|
|
310
|
+
<h2>Tổng kết</h2>
|
|
311
|
+
|
|
312
|
+
<p>Trong bài này, bạn đã học được:</p>
|
|
313
|
+
|
|
314
|
+
<ul>
|
|
315
|
+
<li>Cách cấu hình ethernet interfaces: IP tĩnh, DHCP client, multiple addresses</li>
|
|
316
|
+
<li>Quản lý users: tạo, xóa, đặt password, thêm SSH keys</li>
|
|
317
|
+
<li>Thiết lập system basics: hostname, timezone, NTP, DNS</li>
|
|
318
|
+
<li>Bật và bảo mật SSH service</li>
|
|
319
|
+
<li>Cấu hình default gateway và static routes</li>
|
|
320
|
+
<li>Backup, restore và rollback configuration</li>
|
|
321
|
+
<li>Lab thực hành: Setup router 2 interfaces WAN + LAN hoàn chỉnh</li>
|
|
322
|
+
</ul>
|
|
323
|
+
|
|
324
|
+
<p>Bài tiếp theo sẽ hướng dẫn cấu hình NAT — cho phép LAN clients truy cập Internet qua VyOS router.</p>
|
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: 019d65ef-d36f-773e-bf0a-9e31fc512de3
|
|
3
|
+
title: 'Bài 3: NAT — Source NAT, Destination NAT và Masquerade'
|
|
4
|
+
slug: bai-3-nat-source-nat-destination-nat-va-masquerade
|
|
5
|
+
description: >-
|
|
6
|
+
Hướng dẫn chi tiết về NAT trên VyOS: Source NAT, masquerade cho LAN,
|
|
7
|
+
Destination NAT (port forwarding), 1:1 NAT, NPTv6, rule ordering và troubleshooting.
|
|
8
|
+
duration_minutes: 140
|
|
9
|
+
is_free: true
|
|
10
|
+
video_url: null
|
|
11
|
+
sort_order: 3
|
|
12
|
+
section_title: "VyOS từ Cơ bản đến Nâng cao"
|
|
13
|
+
course:
|
|
14
|
+
id: 019d65ef-d36f-773e-bf0a-9e2efc5e19df
|
|
15
|
+
title: VyOS từ Cơ bản đến Nâng cao
|
|
16
|
+
slug: vyos-tu-co-ban-den-nang-cao
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
<h2>NAT là gì?</h2>
|
|
20
|
+
|
|
21
|
+
<p><strong>Network Address Translation (NAT)</strong> là kỹ thuật thay đổi địa chỉ IP nguồn hoặc đích của gói tin khi đi qua router. NAT là thành phần thiết yếu trong mọi hệ thống mạng — cho phép nhiều thiết bị LAN chia sẻ một địa chỉ IP public để truy cập Internet.</p>
|
|
22
|
+
|
|
23
|
+
<p>VyOS hỗ trợ đầy đủ các loại NAT thông qua nftables backend:</p>
|
|
24
|
+
|
|
25
|
+
<ul>
|
|
26
|
+
<li><strong>Source NAT (SNAT)</strong>: Thay đổi IP nguồn — dùng cho LAN truy cập Internet</li>
|
|
27
|
+
<li><strong>Masquerade</strong>: Dạng đặc biệt của SNAT, tự động dùng IP của outbound interface</li>
|
|
28
|
+
<li><strong>Destination NAT (DNAT)</strong>: Thay đổi IP đích — dùng cho port forwarding</li>
|
|
29
|
+
<li><strong>1:1 NAT</strong>: Map 1 IP public sang 1 IP private và ngược lại</li>
|
|
30
|
+
<li><strong>NPTv6</strong>: Network Prefix Translation cho IPv6</li>
|
|
31
|
+
</ul>
|
|
32
|
+
|
|
33
|
+
<h2>Source NAT — Cho LAN ra Internet</h2>
|
|
34
|
+
|
|
35
|
+
<h3>Topology cơ bản</h3>
|
|
36
|
+
|
|
37
|
+
<pre><code class="language-bash">LAN Clients VyOS Router Internet
|
|
38
|
+
192.168.1.0/24 --- [eth1: 192.168.1.1] [eth0: 203.0.113.10] --- ISP Gateway
|
|
39
|
+
Source NAT: 192.168.1.x → 203.0.113.10</code></pre>
|
|
40
|
+
|
|
41
|
+
<h3>Cấu hình Source NAT với IP tĩnh</h3>
|
|
42
|
+
|
|
43
|
+
<p>Khi WAN interface có IP tĩnh, sử dụng Source NAT với <code>translation address</code>:</p>
|
|
44
|
+
|
|
45
|
+
<pre><code class="language-bash">configure
|
|
46
|
+
|
|
47
|
+
set nat source rule 100 outbound-interface name 'eth0'
|
|
48
|
+
set nat source rule 100 source address '192.168.1.0/24'
|
|
49
|
+
set nat source rule 100 translation address '203.0.113.10'
|
|
50
|
+
|
|
51
|
+
commit
|
|
52
|
+
save</code></pre>
|
|
53
|
+
|
|
54
|
+
<p>Giải thích từng dòng:</p>
|
|
55
|
+
|
|
56
|
+
<ul>
|
|
57
|
+
<li><code>rule 100</code>: Số thứ tự rule (1–999), VyOS xử lý từ nhỏ đến lớn</li>
|
|
58
|
+
<li><code>outbound-interface name 'eth0'</code>: Áp dụng cho traffic đi ra interface eth0 (WAN)</li>
|
|
59
|
+
<li><code>source address</code>: Traffic từ subnet LAN 192.168.1.0/24</li>
|
|
60
|
+
<li><code>translation address</code>: Thay IP nguồn thành IP WAN tĩnh</li>
|
|
61
|
+
</ul>
|
|
62
|
+
|
|
63
|
+
<h3>Masquerade — SNAT tự động</h3>
|
|
64
|
+
|
|
65
|
+
<p>Khi WAN nhận IP qua DHCP (IP thay đổi), dùng <strong>masquerade</strong> thay vì chỉ định IP cụ thể:</p>
|
|
66
|
+
|
|
67
|
+
<pre><code class="language-bash">configure
|
|
68
|
+
|
|
69
|
+
set nat source rule 100 outbound-interface name 'eth0'
|
|
70
|
+
set nat source rule 100 source address '192.168.1.0/24'
|
|
71
|
+
set nat source rule 100 translation address masquerade
|
|
72
|
+
|
|
73
|
+
commit
|
|
74
|
+
save</code></pre>
|
|
75
|
+
|
|
76
|
+
<blockquote>
|
|
77
|
+
<p><strong>Khi nào dùng masquerade vs SNAT?</strong> Dùng masquerade khi WAN IP là DHCP (thay đổi). Dùng SNAT với IP cụ thể khi WAN IP tĩnh — hiệu suất tốt hơn một chút vì không cần lookup IP mỗi packet.</p>
|
|
78
|
+
</blockquote>
|
|
79
|
+
|
|
80
|
+
<h2>Destination NAT — Port Forwarding</h2>
|
|
81
|
+
|
|
82
|
+
<p>Destination NAT cho phép chuyển tiếp traffic từ Internet vào server nội bộ. Ví dụ: forward port 80/443 từ WAN vào web server 192.168.1.100.</p>
|
|
83
|
+
|
|
84
|
+
<pre><code class="language-bash">configure
|
|
85
|
+
|
|
86
|
+
# Forward HTTP (port 80) vào web server
|
|
87
|
+
set nat destination rule 10 description 'HTTP to Web Server'
|
|
88
|
+
set nat destination rule 10 inbound-interface name 'eth0'
|
|
89
|
+
set nat destination rule 10 protocol 'tcp'
|
|
90
|
+
set nat destination rule 10 destination port '80'
|
|
91
|
+
set nat destination rule 10 translation address '192.168.1.100'
|
|
92
|
+
|
|
93
|
+
# Forward HTTPS (port 443) vào web server
|
|
94
|
+
set nat destination rule 20 description 'HTTPS to Web Server'
|
|
95
|
+
set nat destination rule 20 inbound-interface name 'eth0'
|
|
96
|
+
set nat destination rule 20 protocol 'tcp'
|
|
97
|
+
set nat destination rule 20 destination port '443'
|
|
98
|
+
set nat destination rule 20 translation address '192.168.1.100'
|
|
99
|
+
|
|
100
|
+
commit
|
|
101
|
+
save</code></pre>
|
|
102
|
+
|
|
103
|
+
<h3>Port Forwarding với đổi port</h3>
|
|
104
|
+
|
|
105
|
+
<p>Forward traffic đến port khác trên server nội bộ:</p>
|
|
106
|
+
|
|
107
|
+
<pre><code class="language-bash"># Forward port 8080 trên WAN vào port 80 trên server nội bộ
|
|
108
|
+
set nat destination rule 30 description 'Alt HTTP to Web Server'
|
|
109
|
+
set nat destination rule 30 inbound-interface name 'eth0'
|
|
110
|
+
set nat destination rule 30 protocol 'tcp'
|
|
111
|
+
set nat destination rule 30 destination port '8080'
|
|
112
|
+
set nat destination rule 30 translation address '192.168.1.100'
|
|
113
|
+
set nat destination rule 30 translation port '80'
|
|
114
|
+
|
|
115
|
+
commit
|
|
116
|
+
save</code></pre>
|
|
117
|
+
|
|
118
|
+
<h3>Forward nhiều ports cùng lúc</h3>
|
|
119
|
+
|
|
120
|
+
<pre><code class="language-bash"># Forward port range
|
|
121
|
+
set nat destination rule 40 description 'Game Server Ports'
|
|
122
|
+
set nat destination rule 40 inbound-interface name 'eth0'
|
|
123
|
+
set nat destination rule 40 protocol 'udp'
|
|
124
|
+
set nat destination rule 40 destination port '27015-27020'
|
|
125
|
+
set nat destination rule 40 translation address '192.168.1.200'
|
|
126
|
+
|
|
127
|
+
commit
|
|
128
|
+
save</code></pre>
|
|
129
|
+
|
|
130
|
+
<h2>1:1 NAT</h2>
|
|
131
|
+
|
|
132
|
+
<p>1:1 NAT map một IP public sang một IP private — cả chiều inbound lẫn outbound. Hữu ích khi bạn có nhiều IP public và muốn mỗi server có IP public riêng.</p>
|
|
133
|
+
|
|
134
|
+
<pre><code class="language-bash">configure
|
|
135
|
+
|
|
136
|
+
# Source NAT: server 192.168.1.100 đi ra với IP 203.0.113.20
|
|
137
|
+
set nat source rule 200 outbound-interface name 'eth0'
|
|
138
|
+
set nat source rule 200 source address '192.168.1.100'
|
|
139
|
+
set nat source rule 200 translation address '203.0.113.20'
|
|
140
|
+
|
|
141
|
+
# Destination NAT: traffic vào 203.0.113.20 chuyển đến 192.168.1.100
|
|
142
|
+
set nat destination rule 200 inbound-interface name 'eth0'
|
|
143
|
+
set nat destination rule 200 destination address '203.0.113.20'
|
|
144
|
+
set nat destination rule 200 translation address '192.168.1.100'
|
|
145
|
+
|
|
146
|
+
commit
|
|
147
|
+
save</code></pre>
|
|
148
|
+
|
|
149
|
+
<blockquote>
|
|
150
|
+
<p><strong>Lưu ý</strong>: Để 1:1 NAT hoạt động, bạn cần đảm bảo IP 203.0.113.20 được route đến VyOS router (thường ISP cần cấu hình hoặc bạn dùng proxy ARP).</p>
|
|
151
|
+
</blockquote>
|
|
152
|
+
|
|
153
|
+
<h2>NPTv6 — Network Prefix Translation cho IPv6</h2>
|
|
154
|
+
|
|
155
|
+
<p>Trong IPv6, không sử dụng NAT truyền thống. Thay vào đó, VyOS hỗ trợ <strong>NPTv6</strong> để translate prefix:</p>
|
|
156
|
+
|
|
157
|
+
<pre><code class="language-bash">configure
|
|
158
|
+
|
|
159
|
+
set nat nptv6 rule 10 description 'NPTv6 for LAN'
|
|
160
|
+
set nat nptv6 rule 10 outbound-interface name 'eth0'
|
|
161
|
+
set nat nptv6 rule 10 source prefix 'fd00::/64'
|
|
162
|
+
set nat nptv6 rule 10 translation prefix '2001:db8:1::/64'
|
|
163
|
+
|
|
164
|
+
commit
|
|
165
|
+
save</code></pre>
|
|
166
|
+
|
|
167
|
+
<h2>NAT Rule Ordering</h2>
|
|
168
|
+
|
|
169
|
+
<p>Thứ tự xử lý NAT rules rất quan trọng:</p>
|
|
170
|
+
|
|
171
|
+
<ul>
|
|
172
|
+
<li>VyOS xử lý rules theo <strong>số thứ tự tăng dần</strong> (rule 10 trước rule 100)</li>
|
|
173
|
+
<li>Rule đầu tiên match sẽ được áp dụng, các rule sau bị bỏ qua</li>
|
|
174
|
+
<li>Nên đánh số cách nhau (10, 20, 30...) để dễ chèn rule mới</li>
|
|
175
|
+
<li>Destination NAT được xử lý <strong>trước</strong> routing decision</li>
|
|
176
|
+
<li>Source NAT được xử lý <strong>sau</strong> routing decision</li>
|
|
177
|
+
</ul>
|
|
178
|
+
|
|
179
|
+
<pre><code class="language-bash"># Thứ tự xử lý packet trên VyOS:
|
|
180
|
+
#
|
|
181
|
+
# Incoming Packet
|
|
182
|
+
# ↓
|
|
183
|
+
# Destination NAT (DNAT) ← thay đổi IP đích
|
|
184
|
+
# ↓
|
|
185
|
+
# Routing Decision ← quyết định forward hay local
|
|
186
|
+
# ↓
|
|
187
|
+
# Firewall Rules ← filter traffic
|
|
188
|
+
# ↓
|
|
189
|
+
# Source NAT (SNAT) ← thay đổi IP nguồn
|
|
190
|
+
# ↓
|
|
191
|
+
# Outgoing Packet</code></pre>
|
|
192
|
+
|
|
193
|
+
<h2>Exclude Traffic từ NAT</h2>
|
|
194
|
+
|
|
195
|
+
<p>Đôi khi bạn muốn một số traffic không bị NAT (ví dụ: VPN traffic):</p>
|
|
196
|
+
|
|
197
|
+
<pre><code class="language-bash"># Exclude VPN subnet từ Source NAT
|
|
198
|
+
set nat source rule 50 outbound-interface name 'eth0'
|
|
199
|
+
set nat source rule 50 source address '192.168.1.0/24'
|
|
200
|
+
set nat source rule 50 destination address '10.10.0.0/16'
|
|
201
|
+
set nat source rule 50 exclude
|
|
202
|
+
|
|
203
|
+
# Rule masquerade chung (rule 100, xử lý sau rule 50)
|
|
204
|
+
set nat source rule 100 outbound-interface name 'eth0'
|
|
205
|
+
set nat source rule 100 source address '192.168.1.0/24'
|
|
206
|
+
set nat source rule 100 translation address masquerade
|
|
207
|
+
|
|
208
|
+
commit
|
|
209
|
+
save</code></pre>
|
|
210
|
+
|
|
211
|
+
<h2>Troubleshooting NAT</h2>
|
|
212
|
+
|
|
213
|
+
<h3>Kiểm tra NAT rules</h3>
|
|
214
|
+
|
|
215
|
+
<pre><code class="language-bash"># Xem NAT rules đang hoạt động
|
|
216
|
+
show nat source rules
|
|
217
|
+
show nat destination rules
|
|
218
|
+
|
|
219
|
+
# Xem NAT translations đang active
|
|
220
|
+
show nat source translations
|
|
221
|
+
show nat destination translations
|
|
222
|
+
|
|
223
|
+
# Xem NAT statistics
|
|
224
|
+
show nat source statistics
|
|
225
|
+
show nat destination statistics</code></pre>
|
|
226
|
+
|
|
227
|
+
<h3>Các lỗi thường gặp</h3>
|
|
228
|
+
|
|
229
|
+
<ul>
|
|
230
|
+
<li><strong>LAN không ra Internet</strong>: Kiểm tra Source NAT rule, outbound-interface đúng chưa, source address đúng subnet chưa</li>
|
|
231
|
+
<li><strong>Port forwarding không hoạt động</strong>: Kiểm tra firewall có cho phép traffic đến port đó không, inbound-interface đúng chưa</li>
|
|
232
|
+
<li><strong>Rule ordering sai</strong>: Rule exclude phải có số nhỏ hơn rule NAT chung</li>
|
|
233
|
+
</ul>
|
|
234
|
+
|
|
235
|
+
<pre><code class="language-bash"># Debug NAT
|
|
236
|
+
monitor log | match nat
|
|
237
|
+
# Hoặc xem conntrack
|
|
238
|
+
show conntrack table ipv4</code></pre>
|
|
239
|
+
|
|
240
|
+
<h2>Lab thực hành: NAT hoàn chỉnh</h2>
|
|
241
|
+
|
|
242
|
+
<p>Topology lab:</p>
|
|
243
|
+
|
|
244
|
+
<pre><code class="language-bash">Internet
|
|
245
|
+
|
|
|
246
|
+
[eth0: DHCP] VyOS Router [eth1: 192.168.100.1/24]
|
|
247
|
+
|
|
|
248
|
+
+---------------+---------------+
|
|
249
|
+
| | |
|
|
250
|
+
PC Client Web Server Game Server
|
|
251
|
+
192.168.100.10 192.168.100.100 192.168.100.200</code></pre>
|
|
252
|
+
|
|
253
|
+
<h3>Bước 1: Masquerade cho LAN</h3>
|
|
254
|
+
|
|
255
|
+
<pre><code class="language-bash">configure
|
|
256
|
+
|
|
257
|
+
# Source NAT masquerade cho toàn bộ LAN
|
|
258
|
+
set nat source rule 100 outbound-interface name 'eth0'
|
|
259
|
+
set nat source rule 100 source address '192.168.100.0/24'
|
|
260
|
+
set nat source rule 100 translation address masquerade
|
|
261
|
+
|
|
262
|
+
commit</code></pre>
|
|
263
|
+
|
|
264
|
+
<h3>Bước 2: Port forward cho Web Server</h3>
|
|
265
|
+
|
|
266
|
+
<pre><code class="language-bash"># Forward HTTP/HTTPS vào web server
|
|
267
|
+
set nat destination rule 10 description 'HTTP Port Forward'
|
|
268
|
+
set nat destination rule 10 inbound-interface name 'eth0'
|
|
269
|
+
set nat destination rule 10 protocol 'tcp'
|
|
270
|
+
set nat destination rule 10 destination port '80'
|
|
271
|
+
set nat destination rule 10 translation address '192.168.100.100'
|
|
272
|
+
|
|
273
|
+
set nat destination rule 20 description 'HTTPS Port Forward'
|
|
274
|
+
set nat destination rule 20 inbound-interface name 'eth0'
|
|
275
|
+
set nat destination rule 20 protocol 'tcp'
|
|
276
|
+
set nat destination rule 20 destination port '443'
|
|
277
|
+
set nat destination rule 20 translation address '192.168.100.100'
|
|
278
|
+
|
|
279
|
+
commit</code></pre>
|
|
280
|
+
|
|
281
|
+
<h3>Bước 3: Port forward cho Game Server</h3>
|
|
282
|
+
|
|
283
|
+
<pre><code class="language-bash"># Forward game ports
|
|
284
|
+
set nat destination rule 30 description 'Game Server UDP'
|
|
285
|
+
set nat destination rule 30 inbound-interface name 'eth0'
|
|
286
|
+
set nat destination rule 30 protocol 'udp'
|
|
287
|
+
set nat destination rule 30 destination port '27015-27020'
|
|
288
|
+
set nat destination rule 30 translation address '192.168.100.200'
|
|
289
|
+
|
|
290
|
+
commit
|
|
291
|
+
save</code></pre>
|
|
292
|
+
|
|
293
|
+
<h3>Bước 4: Kiểm tra</h3>
|
|
294
|
+
|
|
295
|
+
<pre><code class="language-bash">exit
|
|
296
|
+
|
|
297
|
+
# Xem tất cả NAT rules
|
|
298
|
+
show nat source rules
|
|
299
|
+
show nat destination rules
|
|
300
|
+
|
|
301
|
+
# Từ PC Client, test Internet
|
|
302
|
+
# ping 8.8.8.8 (trên client)
|
|
303
|
+
|
|
304
|
+
# Xem NAT translations
|
|
305
|
+
show nat source translations</code></pre>
|
|
306
|
+
|
|
307
|
+
<h2>Tổng kết</h2>
|
|
308
|
+
|
|
309
|
+
<p>Trong bài này, bạn đã nắm được:</p>
|
|
310
|
+
|
|
311
|
+
<ul>
|
|
312
|
+
<li><strong>Source NAT</strong>: Cho phép LAN truy cập Internet bằng cách thay IP nguồn</li>
|
|
313
|
+
<li><strong>Masquerade</strong>: SNAT tự động — phù hợp khi WAN IP là DHCP</li>
|
|
314
|
+
<li><strong>Destination NAT</strong>: Port forwarding từ Internet vào server nội bộ</li>
|
|
315
|
+
<li><strong>1:1 NAT</strong>: Map hai chiều giữa IP public và IP private</li>
|
|
316
|
+
<li><strong>NPTv6</strong>: Prefix translation cho IPv6</li>
|
|
317
|
+
<li>NAT rule ordering và cách exclude traffic khỏi NAT</li>
|
|
318
|
+
<li>Troubleshooting NAT bằng show commands và conntrack</li>
|
|
319
|
+
</ul>
|
|
320
|
+
|
|
321
|
+
<p>Bài tiếp theo sẽ đi vào <strong>Firewall</strong> — bước quan trọng nhất để bảo vệ mạng. NAT và Firewall luôn đi đôi với nhau trên VyOS.</p>
|