Đồng bộ WSL gốc (nâng cao)
⭐ Chương quan trọng nhất nếu bạn dùng WSL. Real performance numbers từ kinh nghiệm thực.
Vấn đề: Syncthing trên Windows đọc file WSL chậm
Phần tiêu đề “Vấn đề: Syncthing trên Windows đọc file WSL chậm”Nếu bạn add folder Windows Syncthing với path \\wsl.localhost\Ubuntu\home\<user>\projects (cross-OS), bạn sẽ gặp:
Folder: wsl-projectsTotal scanned: 1.07 GB / 5 phútRate: ~33 MB/phút 🐌24 GB folder ~= 12 giờ initial scan. Daily incremental sync rất chậm. File watcher unreliable.
Root cause: 9P protocol bridge
Phần tiêu đề “Root cause: 9P protocol bridge”Windows Syncthing process ↓ Đọc file qua 9P bridge (Windows ↔ WSL VM) ← BOTTLENECK ↓ WSL ext4 filesystem9P Plan 9 protocol là cách Windows access WSL filesystem. Slow vì:
- Each file read = round-trip Windows → WSL VM → ext4 → response
- Per-file overhead lớn (~ms each) cho many small files
- Hash 24GB qua 9P = unbearable
Giải pháp: Cài Syncthing TRONG WSL Ubuntu
Phần tiêu đề “Giải pháp: Cài Syncthing TRONG WSL Ubuntu”Trước: Windows Syncthing → 9P bridge → WSL ext4 (33 MB/min)
Sau: WSL Syncthing native → ext4 (3952 MB/min) — 120x fasterTrade-off: 2 Syncthing instances per machine (1 Windows + 1 WSL).
Tổng quan kiến trúc
Phần tiêu đề “Tổng quan kiến trúc”PC machine Laptop machine┌─────────────────────────────┐ ┌─────────────────────────────┐│ Windows │ │ Windows ││ ├─ Syncthing :8384 (BEP 22000)│ │ ├─ Syncthing :8384 ││ │ ├─ ~/.claude/ │ ←────→ │ │ ├─ ~/.claude/ ││ │ └─ Documents/ │ │ │ └─ Documents/ ││ │ │ │ │ ││ └─ WSL Ubuntu │ │ └─ WSL Ubuntu ││ └─ Syncthing :8385 │ │ └─ Syncthing :8385 ││ (BEP 22001) │ ←────→ │ (BEP 22001) ││ └─ ~/projects/ │ │ └─ ~/projects/ │└─────────────────────────────┘ └─────────────────────────────┘4 Device IDs total (2 instances × 2 machines).
Bước 1: Bật mirrored networking cho WSL
Phần tiêu đề “Bước 1: Bật mirrored networking cho WSL”Without mirrored mode, WSL VM có private NAT IP (172.x.x.x) — laptop’s WSL không reach PC’s WSL được.
Edit C:\Users\<user>\.wslconfig trên cả 2 máy:
[wsl2]networkingMode=mirroredfirewall=truednsTunneling=trueautoProxy=true
[experimental]hostAddressLoopback=trueRestart WSL:
wsl --shutdown# Wait 5swsl bash -c "hostname -I"# Expect: shows host LAN IP (vd 192.168.1.14)Bước 2: Cài Syncthing trong WSL Ubuntu
Phần tiêu đề “Bước 2: Cài Syncthing trong WSL Ubuntu”# Inside WSL Ubuntusudo apt updatesudo apt install -y syncthingBước 3: Cấu hình port khác Windows
Phần tiêu đề “Bước 3: Cấu hình port khác Windows”Vấn đề: WSL share host network → port 22000 (BEP) + 8384 (GUI) đã dùng bởi Windows Syncthing → conflict.
Solution: change WSL Syncthing ports.
# Generate config with separate home dirsyncthing generate --home ~/.config/syncthing-wsl
# Change portssed -i 's|<address>127.0.0.1:8384</address>|<address>127.0.0.1:8385</address>|' ~/.config/syncthing-wsl/config.xmlsed -i 's|<listenAddress>default</listenAddress>|<listenAddress>tcp://0.0.0.0:22001</listenAddress><listenAddress>quic://0.0.0.0:22001</listenAddress>|' ~/.config/syncthing-wsl/config.xmlsed -i 's|<localAnnouncePort>21027|<localAnnouncePort>21028|' ~/.config/syncthing-wsl/config.xml
# Verifygrep -E "listenAddress|<address>|localAnnouncePort" ~/.config/syncthing-wsl/config.xmlPort allocation table:
| Service | Windows Syncthing | WSL Syncthing |
|---|---|---|
| GUI Web UI | 8384 | 8385 |
| BEP sync (TCP+QUIC) | 22000 | 22001 |
| Local discovery | 21027 | 21028 |
Bước 4: Khởi động daemon Syncthing trong WSL
Phần tiêu đề “Bước 4: Khởi động daemon Syncthing trong WSL”nohup syncthing serve --home ~/.config/syncthing-wsl --no-browser > ~/syncthing-wsl.log 2>&1 &sleep 5
# Verifycurl -s -o /dev/null -w "Web UI: HTTP %{http_code}\n" http://127.0.0.1:8385# Expect: HTTP 200Bước 5: Lấy Device ID của WSL
Phần tiêu đề “Bước 5: Lấy Device ID của WSL”# From config.xmlgrep -oE '<device id="[^"]+" name="[^"]*"' ~/.config/syncthing-wsl/config.xml | head -1
# Output (example):# <device id="ABCD123-EFG456-IJKL789-..." name="My WSL"Note Device ID này — sẽ dùng cho pairing.
Bước 6: Tự khởi động Syncthing khi WSL boot
Phần tiêu đề “Bước 6: Tự khởi động Syncthing khi WSL boot”Default WSL Syncthing không tự start sau wsl --shutdown. Add to startup:
# Add to ~/.bashrc hoặc ~/.zshrccat >> ~/.bashrc << 'EOF'
# Auto-start Syncthing WSL daemon (skip if already running)if ! pgrep -f "syncthing.*syncthing-wsl" > /dev/null; then nohup syncthing serve --home ~/.config/syncthing-wsl --no-browser > ~/syncthing-wsl.log 2>&1 &fiEOFBước 7: Pair WSL Syncthing PC ↔ Laptop
Phần tiêu đề “Bước 7: Pair WSL Syncthing PC ↔ Laptop”Repeat Step 1-6 trên Laptop.
Sau đó pair:
-
Trên PC’s WSL Web UI
http://127.0.0.1:8385:- Add Remote Device → paste Laptop’s WSL Device ID
-
Trên Laptop’s WSL Web UI:
- Auto-detect notification → click Add Device, accept
-
Verify connection trong Web UI → Remote Devices section:
- Status: Connected
- Address: shows host LAN IP (vd
192.168.1.16:22001) - Type: TCP LAN hoặc QUIC
Bước 8: Thêm folder WSL để đồng bộ
Phần tiêu đề “Bước 8: Thêm folder WSL để đồng bộ”8.1 Trên PC’s WSL Web UI → Add Folder
Phần tiêu đề “8.1 Trên PC’s WSL Web UI → Add Folder”- Path:
/home/<user>/projects(native ext4) - Sharing: tick Laptop’s WSL device
- Ignore Patterns — critical cho code projects:
**/node_modules**/dist**/build**/.next**/out**/.venv**/venv**/__pycache__**/*.pyc**/coverage**/data**/postgres-data**/.docker**/*.log**/.cache**/tmp**/.env.local.DS_StoreThumbs.db8.2 Laptop’s WSL Web UI → Accept folder offer
Phần tiêu đề “8.2 Laptop’s WSL Web UI → Accept folder offer”- Path:
/home/<user>/projects(mirror PC)
8.3 Initial sync starts
Phần tiêu đề “8.3 Initial sync starts”Native ext4 reads = fast scan + transfer.
Số liệu tốc độ (đo thực tế)
Phần tiêu đề “Số liệu tốc độ (đo thực tế)”Em đã measure thực tế setup AIWS production:
| Metric | Windows reads WSL (9P) | WSL Native | Speedup |
|---|---|---|---|
| Initial scan rate | 33 MB/phút | 3952 MB/phút | 120x faster |
| 24 GB folder scan | ~12 giờ | ~6 phút | 120x |
| Sync rate (LAN) | 46 KiB/s | 80-100 MB/s | ~2000x |
| File watcher | Unreliable | Reliable | — |
Xử lý sự cố đồng bộ WSL
Phần tiêu đề “Xử lý sự cố đồng bộ WSL”Windows Syncthing đã dùng 22000. WSL phải dùng 22001 — xem Step 3.
Verify với:
ss -tlnp | grep -E ":22000|:22001|:8384|:8385"Check mirrored networking active:
ip addr show | grep "inet "Expect: thấy host LAN IP (192.168.x.x), KHÔNG phải 172.x (NAT).
Nếu vẫn 172.x → wsl --shutdown lại từ Windows, restart WSL.
Một số apps Windows có network conflict với mirrored mode. Test:
ping google.com từ WSL.
Workaround: tạm disable mirrored, dùng port forwarding:
netsh interface portproxy add v4tov4 listenport=22001 listenaddress=0.0.0.0 connectport=22001 connectaddress=<WSL_IP>Causes:
- Many small files (vd
.git/objects/) — overhead per-file lớn - Hash CPU bound (small VPS / weak CPU)
- Disk I/O slow (HDD instead of SSD)
Mitigation:
- Add ignore patterns rộng hơn
- Sync chỉ folders critical, skip rest
Bước tiếp theo
Phần tiêu đề “Bước tiếp theo”WSL native sync đã ready. Real-world use cases:
- Sync Claude Code state — conversations + skills
- Sync code projects — npm/pnpm projects
- AI agent prompts — autonomous setup