Quick Start: VyOS chạy được trong 5 phút
Mình chạy homelab với Proxmox VE quản lý 12 VM và container — đây là playground để test mọi thứ trước khi đưa lên production. Sau vài lần cần giả lập môi trường mạng phức tạp — multi-VLAN, routing tĩnh/động, IPsec tunnel — mình quyết định thử VyOS. Không ngờ gắn bó từ đó đến giờ: cú pháp theo phong cách Juniper JunOS, gần thiết bị thật hơn bất kỳ virtual router nào mình từng đụng.
VyOS là fork của Vyatta — miễn phí, không giới hạn tính năng, có cả bản rolling release lẫn LTS. Tải ISO rolling (khuyên dùng cho lab) từ trang chủ, tạo VM với thông số tối thiểu:
- CPU: 1–2 vCPU
- RAM: 512MB (1GB nếu cần chạy nhiều VPN tunnel)
- Disk: 4GB
- Network: ít nhất 2 vNIC — một WAN (nối ra ngoài), một LAN (nối vào mạng nội bộ)
Tạo VM trên KVM bằng virt-install
virt-install \
--name vyos-lab \
--ram 1024 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/vyos-lab.qcow2,size=4 \
--cdrom /tmp/vyos-rolling.iso \
--network bridge=br0 \
--network bridge=br1 \
--os-variant debian11 \
--noautoconsole
Trên Proxmox thì làm qua GUI: tạo VM, gán 2 Network Device vào 2 bridge khác nhau (ví dụ vmbr0 cho WAN, vmbr1 cho LAN).
Cài đặt và config mạng ban đầu
Boot vào ISO, đăng nhập vyos/vyos, chạy lệnh cài:
install image
Làm theo wizard, reboot. Sau khi boot xong, vào configure mode và setup interface:
configure
set interfaces ethernet eth0 address dhcp
set interfaces ethernet eth0 description 'WAN'
set interfaces ethernet eth1 address 192.168.10.1/24
set interfaces ethernet eth1 description 'LAN'
set system name-server 8.8.8.8
set system name-server 8.8.4.4
commit
save
Quy tắc số một với VyOS: commit áp dụng config vào running state, save ghi vào startup config. Quên save rồi reboot là mất hết config — mình đã học điều này theo cách đắt nhất có thể.
Firewall Zone-Based: Tư duy theo luồng, không phải theo chain
VyOS không dùng mô hình INPUT/OUTPUT/FORWARD như iptables thuần. Thay vào đó, mỗi interface thuộc một zone, traffic đi giữa hai zone phải đi qua một ruleset được gán rõ ràng. Cách nghĩ này sạch hơn nhiều khi lab có nhiều segment mạng.
Mình dùng 3 zone: WAN, LAN và LOCAL (chính router VyOS).
configure
# === Ruleset: WAN -> LOCAL (chỉ cho phép SSH và return traffic) ===
set firewall name WAN-to-LOCAL default-action drop
set firewall name WAN-to-LOCAL rule 10 action accept
set firewall name WAN-to-LOCAL rule 10 state established enable
set firewall name WAN-to-LOCAL rule 10 state related enable
set firewall name WAN-to-LOCAL rule 20 action accept
set firewall name WAN-to-LOCAL rule 20 protocol tcp
set firewall name WAN-to-LOCAL rule 20 destination port 22
# === Ruleset: WAN -> LAN (chỉ return traffic, block new connections) ===
set firewall name WAN-to-LAN default-action drop
set firewall name WAN-to-LAN rule 10 action accept
set firewall name WAN-to-LAN rule 10 state established enable
set firewall name WAN-to-LAN rule 10 state related enable
# === Ruleset: LAN -> WAN (cho phép tất cả từ LAN ra ngoài) ===
set firewall name LAN-to-WAN default-action accept
# === Ruleset: LAN -> LOCAL (SSH vào router từ LAN) ===
set firewall name LAN-to-LOCAL default-action drop
set firewall name LAN-to-LOCAL rule 10 action accept
set firewall name LAN-to-LOCAL rule 10 state established enable
set firewall name LAN-to-LOCAL rule 10 state related enable
set firewall name LAN-to-LOCAL rule 20 action accept
set firewall name LAN-to-LOCAL rule 20 protocol tcp
set firewall name LAN-to-LOCAL rule 20 destination port 22
# === Gán interface vào zone ===
set zone-policy zone WAN interface eth0
set zone-policy zone LAN interface eth1
set zone-policy zone LOCAL local-zone
# === Áp ruleset giữa các zone ===
set zone-policy zone LOCAL from WAN firewall name WAN-to-LOCAL
set zone-policy zone LOCAL from LAN firewall name LAN-to-LOCAL
set zone-policy zone LAN from WAN firewall name WAN-to-LAN
set zone-policy zone WAN from LAN firewall name LAN-to-WAN
commit
save
NAT: Masquerade và Port Forwarding
Có firewall rồi nhưng máy trong LAN vẫn chưa ra được internet — thiếu NAT. VyOS gọi đây là Source NAT với translation masquerade.
configure
# Masquerade: hide toàn bộ LAN 192.168.10.0/24 sau IP WAN
set nat source rule 100 outbound-interface eth0
set nat source rule 100 source address 192.168.10.0/24
set nat source rule 100 translation address masquerade
commit
save
Port forwarding (Destination NAT) — expose web server LAN ra ngoài:
configure
# DNAT: port 80 WAN -> 192.168.10.100:80
set nat destination rule 10 inbound-interface eth0
set nat destination rule 10 protocol tcp
set nat destination rule 10 destination port 80
set nat destination rule 10 translation address 192.168.10.100
set nat destination rule 10 translation port 80
commit
save
VPN Site-to-Site với IPsec IKEv2
Phần này là lý do chính mình không quay lại pfSense cho lab nữa. Mình sẽ giả lập 2 site kết nối qua tunnel IPsec IKEv2 — cùng logic config bạn sẽ thấy trên Cisco ASA hay Juniper SRX ngoài thực tế.
Topology:
- VyOS-A: WAN
10.0.0.1, LAN192.168.10.0/24 - VyOS-B: WAN
10.0.0.2, LAN192.168.20.0/24
Config VyOS-A
configure
# IKE group (phase 1)
set vpn ipsec ike-group IKE-DEFAULT key-exchange ikev2
set vpn ipsec ike-group IKE-DEFAULT lifetime 28800
set vpn ipsec ike-group IKE-DEFAULT proposal 1 dh-group 14
set vpn ipsec ike-group IKE-DEFAULT proposal 1 encryption aes256
set vpn ipsec ike-group IKE-DEFAULT proposal 1 hash sha256
# ESP group (phase 2)
set vpn ipsec esp-group ESP-DEFAULT lifetime 3600
set vpn ipsec esp-group ESP-DEFAULT mode tunnel
set vpn ipsec esp-group ESP-DEFAULT proposal 1 encryption aes256
set vpn ipsec esp-group ESP-DEFAULT proposal 1 hash sha256
# Interface listen IPsec
set vpn ipsec ipsec-interfaces interface eth0
# Peer và tunnel
set vpn ipsec site-to-site peer 10.0.0.2 authentication mode pre-shared-secret
set vpn ipsec site-to-site peer 10.0.0.2 authentication pre-shared-secret 'Lab@Secret#2024'
set vpn ipsec site-to-site peer 10.0.0.2 ike-group IKE-DEFAULT
set vpn ipsec site-to-site peer 10.0.0.2 default-esp-group ESP-DEFAULT
set vpn ipsec site-to-site peer 10.0.0.2 local-address 10.0.0.1
set vpn ipsec site-to-site peer 10.0.0.2 tunnel 1 local prefix 192.168.10.0/24
set vpn ipsec site-to-site peer 10.0.0.2 tunnel 1 remote prefix 192.168.20.0/24
commit
save
Config VyOS-B (mirror ngược lại)
configure
set vpn ipsec ike-group IKE-DEFAULT key-exchange ikev2
set vpn ipsec ike-group IKE-DEFAULT lifetime 28800
set vpn ipsec ike-group IKE-DEFAULT proposal 1 dh-group 14
set vpn ipsec ike-group IKE-DEFAULT proposal 1 encryption aes256
set vpn ipsec ike-group IKE-DEFAULT proposal 1 hash sha256
set vpn ipsec esp-group ESP-DEFAULT lifetime 3600
set vpn ipsec esp-group ESP-DEFAULT mode tunnel
set vpn ipsec esp-group ESP-DEFAULT proposal 1 encryption aes256
set vpn ipsec esp-group ESP-DEFAULT proposal 1 hash sha256
set vpn ipsec ipsec-interfaces interface eth0
set vpn ipsec site-to-site peer 10.0.0.1 authentication mode pre-shared-secret
set vpn ipsec site-to-site peer 10.0.0.1 authentication pre-shared-secret 'Lab@Secret#2024'
set vpn ipsec site-to-site peer 10.0.0.1 ike-group IKE-DEFAULT
set vpn ipsec site-to-site peer 10.0.0.1 default-esp-group ESP-DEFAULT
set vpn ipsec site-to-site peer 10.0.0.1 local-address 10.0.0.2
set vpn ipsec site-to-site peer 10.0.0.1 tunnel 1 local prefix 192.168.20.0/24
set vpn ipsec site-to-site peer 10.0.0.1 tunnel 1 remote prefix 192.168.10.0/24
commit
save
Kiểm tra tunnel
# Xem trạng thái SA (Security Association)
show vpn ipsec sa
# Ping xuyên tunnel từ VyOS-A sang LAN bên B
ping 192.168.20.1 source-address 192.168.10.1 count 5
Output có ESTABLISHED kèm số bytes/packets đang chạy là tunnel đã up. Còn nếu chỉ thấy CONNECTING mãi không lên, hầu như chắc chắn là firewall đang chặn UDP 500 hoặc ESP (protocol 50) — kiểm tra cái đó trước.
Tips Thực Tế Khi Vận Hành VyOS
Backup config trước mọi thay đổi
# Lưu backup có timestamp
save /config/backup-$(date +%Y%m%d-%H%M).conf
# Copy ra ngoài
scp [email protected]:/config/backup-20240620-1430.conf ./
Rollback khi commit sai
VyOS lưu lịch sử commit — rollback rất dễ:
# Xem danh sách commit
show system commit
# Rollback về commit trước (index 1)
rollback 1
commit
save
Debug firewall và traffic
# Xem counter hit của từng rule
show firewall name WAN-to-LAN statistics
# Monitor traffic realtime
monitor traffic interface eth0
# Xem log firewall (nếu bật logging)
show log firewall name WAN-to-LAN
Một thứ mình học được sau lần bị lock out khỏi router: luôn bật monitor traffic trên một session SSH riêng trong lúc test firewall rule trên session kia. Nhìn thấy packet đi qua hay bị drop ngay lập tức, không phải đoán mò.
VyOS hợp cho lab cần BGP, OSPF, hoặc IPsec multi-tunnel — những thứ pfSense có nhưng cú pháp khác xa thiết bị thật. Nếu chỉ cần NAT đơn giản cho mạng nhỏ, pfSense/OPNsense vẫn thân thiện hơn nhiều. Nhưng nếu mục tiêu là xây muscle memory cho CLI network engineering, không có virtual router nào trên Proxmox/KVM luyện tay tốt hơn VyOS.

