@xdev-asia/xdev-knowledge-mcp 1.0.54 → 1.0.56

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.
Files changed (17) hide show
  1. 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 +333 -0
  2. 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 +325 -0
  3. 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 +322 -0
  4. 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
  5. 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
  6. 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 +108 -0
  7. 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 +96 -0
  8. 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 +85 -0
  9. 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 +81 -0
  10. 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 +84 -0
  11. 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 +87 -0
  12. 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 +89 -0
  13. 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 +63 -0
  14. 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 +64 -0
  15. 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 +82 -0
  16. package/content/series/devsecops/vyos-tu-co-ban-den-nang-cao/index.md +410 -0
  17. package/package.json +1 -1
@@ -0,0 +1,322 @@
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
+ <img src="/storage/uploads/2026/04/vyos-03-nat.png" alt="NAT — Source NAT, Destination NAT và Masquerade" style="display:block;margin:24px auto 32px auto;max-width:700px;width:100%;border-radius:18px;box-shadow:0 4px 32px #0002" loading="lazy" />
19
+
20
+ <h2>NAT là gì?</h2>
21
+
22
+ <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>
23
+
24
+ <p>VyOS hỗ trợ đầy đủ các loại NAT thông qua nftables backend:</p>
25
+
26
+ <ul>
27
+ <li><strong>Source NAT (SNAT)</strong>: Thay đổi IP nguồn — dùng cho LAN truy cập Internet</li>
28
+ <li><strong>Masquerade</strong>: Dạng đặc biệt của SNAT, tự động dùng IP của outbound interface</li>
29
+ <li><strong>Destination NAT (DNAT)</strong>: Thay đổi IP đích — dùng cho port forwarding</li>
30
+ <li><strong>1:1 NAT</strong>: Map 1 IP public sang 1 IP private và ngược lại</li>
31
+ <li><strong>NPTv6</strong>: Network Prefix Translation cho IPv6</li>
32
+ </ul>
33
+
34
+ <h2>Source NAT — Cho LAN ra Internet</h2>
35
+
36
+ <h3>Topology cơ bản</h3>
37
+
38
+ <pre><code class="language-bash">LAN Clients VyOS Router Internet
39
+ 192.168.1.0/24 --- [eth1: 192.168.1.1] [eth0: 203.0.113.10] --- ISP Gateway
40
+ Source NAT: 192.168.1.x → 203.0.113.10</code></pre>
41
+
42
+ <h3>Cấu hình Source NAT với IP tĩnh</h3>
43
+
44
+ <p>Khi WAN interface có IP tĩnh, sử dụng Source NAT với <code>translation address</code>:</p>
45
+
46
+ <pre><code class="language-bash">configure
47
+
48
+ set nat source rule 100 outbound-interface name 'eth0'
49
+ set nat source rule 100 source address '192.168.1.0/24'
50
+ set nat source rule 100 translation address '203.0.113.10'
51
+
52
+ commit
53
+ save</code></pre>
54
+
55
+ <p>Giải thích từng dòng:</p>
56
+
57
+ <ul>
58
+ <li><code>rule 100</code>: Số thứ tự rule (1–999), VyOS xử lý từ nhỏ đến lớn</li>
59
+ <li><code>outbound-interface name 'eth0'</code>: Áp dụng cho traffic đi ra interface eth0 (WAN)</li>
60
+ <li><code>source address</code>: Traffic từ subnet LAN 192.168.1.0/24</li>
61
+ <li><code>translation address</code>: Thay IP nguồn thành IP WAN tĩnh</li>
62
+ </ul>
63
+
64
+ <h3>Masquerade — SNAT tự động</h3>
65
+
66
+ <p>Khi WAN nhận IP qua DHCP (IP thay đổi), dùng <strong>masquerade</strong> thay vì chỉ định IP cụ thể:</p>
67
+
68
+ <pre><code class="language-bash">configure
69
+
70
+ set nat source rule 100 outbound-interface name 'eth0'
71
+ set nat source rule 100 source address '192.168.1.0/24'
72
+ set nat source rule 100 translation address masquerade
73
+
74
+ commit
75
+ save</code></pre>
76
+
77
+ <blockquote>
78
+ <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>
79
+ </blockquote>
80
+
81
+ <h2>Destination NAT — Port Forwarding</h2>
82
+
83
+ <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>
84
+
85
+ <pre><code class="language-bash">configure
86
+
87
+ # Forward HTTP (port 80) vào web server
88
+ set nat destination rule 10 description 'HTTP to Web Server'
89
+ set nat destination rule 10 inbound-interface name 'eth0'
90
+ set nat destination rule 10 protocol 'tcp'
91
+ set nat destination rule 10 destination port '80'
92
+ set nat destination rule 10 translation address '192.168.1.100'
93
+
94
+ # Forward HTTPS (port 443) vào web server
95
+ set nat destination rule 20 description 'HTTPS to Web Server'
96
+ set nat destination rule 20 inbound-interface name 'eth0'
97
+ set nat destination rule 20 protocol 'tcp'
98
+ set nat destination rule 20 destination port '443'
99
+ set nat destination rule 20 translation address '192.168.1.100'
100
+
101
+ commit
102
+ save</code></pre>
103
+
104
+ <h3>Port Forwarding với đổi port</h3>
105
+
106
+ <p>Forward traffic đến port khác trên server nội bộ:</p>
107
+
108
+ <pre><code class="language-bash"># Forward port 8080 trên WAN vào port 80 trên server nội bộ
109
+ set nat destination rule 30 description 'Alt HTTP to Web Server'
110
+ set nat destination rule 30 inbound-interface name 'eth0'
111
+ set nat destination rule 30 protocol 'tcp'
112
+ set nat destination rule 30 destination port '8080'
113
+ set nat destination rule 30 translation address '192.168.1.100'
114
+ set nat destination rule 30 translation port '80'
115
+
116
+ commit
117
+ save</code></pre>
118
+
119
+ <h3>Forward nhiều ports cùng lúc</h3>
120
+
121
+ <pre><code class="language-bash"># Forward port range
122
+ set nat destination rule 40 description 'Game Server Ports'
123
+ set nat destination rule 40 inbound-interface name 'eth0'
124
+ set nat destination rule 40 protocol 'udp'
125
+ set nat destination rule 40 destination port '27015-27020'
126
+ set nat destination rule 40 translation address '192.168.1.200'
127
+
128
+ commit
129
+ save</code></pre>
130
+
131
+ <h2>1:1 NAT</h2>
132
+
133
+ <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>
134
+
135
+ <pre><code class="language-bash">configure
136
+
137
+ # Source NAT: server 192.168.1.100 đi ra với IP 203.0.113.20
138
+ set nat source rule 200 outbound-interface name 'eth0'
139
+ set nat source rule 200 source address '192.168.1.100'
140
+ set nat source rule 200 translation address '203.0.113.20'
141
+
142
+ # Destination NAT: traffic vào 203.0.113.20 chuyển đến 192.168.1.100
143
+ set nat destination rule 200 inbound-interface name 'eth0'
144
+ set nat destination rule 200 destination address '203.0.113.20'
145
+ set nat destination rule 200 translation address '192.168.1.100'
146
+
147
+ commit
148
+ save</code></pre>
149
+
150
+ <blockquote>
151
+ <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>
152
+ </blockquote>
153
+
154
+ <h2>NPTv6 — Network Prefix Translation cho IPv6</h2>
155
+
156
+ <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>
157
+
158
+ <pre><code class="language-bash">configure
159
+
160
+ set nat nptv6 rule 10 description 'NPTv6 for LAN'
161
+ set nat nptv6 rule 10 outbound-interface name 'eth0'
162
+ set nat nptv6 rule 10 source prefix 'fd00::/64'
163
+ set nat nptv6 rule 10 translation prefix '2001:db8:1::/64'
164
+
165
+ commit
166
+ save</code></pre>
167
+
168
+ <h2>NAT Rule Ordering</h2>
169
+
170
+ <p>Thứ tự xử lý NAT rules rất quan trọng:</p>
171
+
172
+ <ul>
173
+ <li>VyOS xử lý rules theo <strong>số thứ tự tăng dần</strong> (rule 10 trước rule 100)</li>
174
+ <li>Rule đầu tiên match sẽ được áp dụng, các rule sau bị bỏ qua</li>
175
+ <li>Nên đánh số cách nhau (10, 20, 30...) để dễ chèn rule mới</li>
176
+ <li>Destination NAT được xử lý <strong>trước</strong> routing decision</li>
177
+ <li>Source NAT được xử lý <strong>sau</strong> routing decision</li>
178
+ </ul>
179
+
180
+ <pre><code class="language-bash"># Thứ tự xử lý packet trên VyOS:
181
+ #
182
+ # Incoming Packet
183
+ # ↓
184
+ # Destination NAT (DNAT) ← thay đổi IP đích
185
+ # ↓
186
+ # Routing Decision ← quyết định forward hay local
187
+ # ↓
188
+ # Firewall Rules ← filter traffic
189
+ # ↓
190
+ # Source NAT (SNAT) ← thay đổi IP nguồn
191
+ # ↓
192
+ # Outgoing Packet</code></pre>
193
+
194
+ <h2>Exclude Traffic từ NAT</h2>
195
+
196
+ <p>Đôi khi bạn muốn một số traffic không bị NAT (ví dụ: VPN traffic):</p>
197
+
198
+ <pre><code class="language-bash"># Exclude VPN subnet từ Source NAT
199
+ set nat source rule 50 outbound-interface name 'eth0'
200
+ set nat source rule 50 source address '192.168.1.0/24'
201
+ set nat source rule 50 destination address '10.10.0.0/16'
202
+ set nat source rule 50 exclude
203
+
204
+ # Rule masquerade chung (rule 100, xử lý sau rule 50)
205
+ set nat source rule 100 outbound-interface name 'eth0'
206
+ set nat source rule 100 source address '192.168.1.0/24'
207
+ set nat source rule 100 translation address masquerade
208
+
209
+ commit
210
+ save</code></pre>
211
+
212
+ <h2>Troubleshooting NAT</h2>
213
+
214
+ <h3>Kiểm tra NAT rules</h3>
215
+
216
+ <pre><code class="language-bash"># Xem NAT rules đang hoạt động
217
+ show nat source rules
218
+ show nat destination rules
219
+
220
+ # Xem NAT translations đang active
221
+ show nat source translations
222
+ show nat destination translations
223
+
224
+ # Xem NAT statistics
225
+ show nat source statistics
226
+ show nat destination statistics</code></pre>
227
+
228
+ <h3>Các lỗi thường gặp</h3>
229
+
230
+ <ul>
231
+ <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>
232
+ <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>
233
+ <li><strong>Rule ordering sai</strong>: Rule exclude phải có số nhỏ hơn rule NAT chung</li>
234
+ </ul>
235
+
236
+ <pre><code class="language-bash"># Debug NAT
237
+ monitor log | match nat
238
+ # Hoặc xem conntrack
239
+ show conntrack table ipv4</code></pre>
240
+
241
+ <h2>Lab thực hành: NAT hoàn chỉnh</h2>
242
+
243
+ <p>Topology lab:</p>
244
+
245
+ <pre><code class="language-bash">Internet
246
+ |
247
+ [eth0: DHCP] VyOS Router [eth1: 192.168.100.1/24]
248
+ |
249
+ +---------------+---------------+
250
+ | | |
251
+ PC Client Web Server Game Server
252
+ 192.168.100.10 192.168.100.100 192.168.100.200</code></pre>
253
+
254
+ <h3>Bước 1: Masquerade cho LAN</h3>
255
+
256
+ <pre><code class="language-bash">configure
257
+
258
+ # Source NAT masquerade cho toàn bộ LAN
259
+ set nat source rule 100 outbound-interface name 'eth0'
260
+ set nat source rule 100 source address '192.168.100.0/24'
261
+ set nat source rule 100 translation address masquerade
262
+
263
+ commit</code></pre>
264
+
265
+ <h3>Bước 2: Port forward cho Web Server</h3>
266
+
267
+ <pre><code class="language-bash"># Forward HTTP/HTTPS vào web server
268
+ set nat destination rule 10 description 'HTTP Port Forward'
269
+ set nat destination rule 10 inbound-interface name 'eth0'
270
+ set nat destination rule 10 protocol 'tcp'
271
+ set nat destination rule 10 destination port '80'
272
+ set nat destination rule 10 translation address '192.168.100.100'
273
+
274
+ set nat destination rule 20 description 'HTTPS Port Forward'
275
+ set nat destination rule 20 inbound-interface name 'eth0'
276
+ set nat destination rule 20 protocol 'tcp'
277
+ set nat destination rule 20 destination port '443'
278
+ set nat destination rule 20 translation address '192.168.100.100'
279
+
280
+ commit</code></pre>
281
+
282
+ <h3>Bước 3: Port forward cho Game Server</h3>
283
+
284
+ <pre><code class="language-bash"># Forward game ports
285
+ set nat destination rule 30 description 'Game Server UDP'
286
+ set nat destination rule 30 inbound-interface name 'eth0'
287
+ set nat destination rule 30 protocol 'udp'
288
+ set nat destination rule 30 destination port '27015-27020'
289
+ set nat destination rule 30 translation address '192.168.100.200'
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
+ # Xem tất cả NAT rules
299
+ show nat source rules
300
+ show nat destination rules
301
+
302
+ # Từ PC Client, test Internet
303
+ # ping 8.8.8.8 (trên client)
304
+
305
+ # Xem NAT translations
306
+ show nat source translations</code></pre>
307
+
308
+ <h2>Tổng kết</h2>
309
+
310
+ <p>Trong bài này, bạn đã nắm được:</p>
311
+
312
+ <ul>
313
+ <li><strong>Source NAT</strong>: Cho phép LAN truy cập Internet bằng cách thay IP nguồn</li>
314
+ <li><strong>Masquerade</strong>: SNAT tự động — phù hợp khi WAN IP là DHCP</li>
315
+ <li><strong>Destination NAT</strong>: Port forwarding từ Internet vào server nội bộ</li>
316
+ <li><strong>1:1 NAT</strong>: Map hai chiều giữa IP public và IP private</li>
317
+ <li><strong>NPTv6</strong>: Prefix translation cho IPv6</li>
318
+ <li>NAT rule ordering và cách exclude traffic khỏi NAT</li>
319
+ <li>Troubleshooting NAT bằng show commands và conntrack</li>
320
+ </ul>
321
+
322
+ <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>