@xdev-asia/xdev-knowledge-mcp 1.0.54 → 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.
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 +324 -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 +324 -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 +321 -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 +105 -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 +94 -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 +83 -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 +79 -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 +82 -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 +85 -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 +87 -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 +61 -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 +62 -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 +80 -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,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>