ติดตั้ง Syncthing ซิงค์ไฟล์อัตโนมัติบน VPS
Syncthing คือเครื่องมือ peer-to-peer ที่ใช้สำหรับซิงค์ไฟล์ระหว่างอุปกรณ์หลายเครื่องโดยไม่พึ่งพาบริการ cloud storage ของบุคคลที่สาม การเชื่อมต่อระหว่างอุปกรณ์เข้ารหัสด้วย TLS เสมอ และมีเฉพาะอุปกรณ์ที่คุณอนุมัติ (trusted device) เท่านั้นที่ sync ด้วยกันได้ การตั้ง VPS ให้เป็น Syncthing relay node หรือ sync hub ช่วยให้คุณมี always-on sync point ที่ยอดเยี่ยมสำหรับอุปกรณ์ที่อยู่นอกเครือข่ายหรือมีปัญหา NAT traversal
ตรวจสอบความเข้ากันได้ของระบบ
# ตรวจสอบ OS
uname -a
# ตรวจสอบ architecture
dpkg --print-architecture
# Syncthing รองรับ: amd64, i386, arm64, armv7l, armv6l
ดาวน์โหลดและติดตั้ง Syncthing
Syncthing มีให้ในหลายแพคเกจดิสทริบิวชั่น ที่นี่เราจะใช้ตัวเลขเลขฐาน 64 บน Linux Ubuntu/Debian:
# สร้าง directory สำหรับ Syncthing
mkdir -p ~/syncthing-install && cd ~/syncthing-install
# ดาวน์โหลด Syncthing เวอร์ชันล่าสุด (สำหรับ amd64)
wget https://github.com/syncthing/syncthing/releases/download/v1.27.0/syncthing-linux-amd64-v1.27.0.tar.gz
# แตกไฟล์
tar xzf syncthing-linux-amd64-v1.27.0.tar.gz
# ย้ายไบนารี่ไปยัง /usr/local/bin
sudo cp syncthing-linux-amd64-v1.27.0/syncthing /usr/local/bin/
# ให้สิทธิ์เรียกใช้งาน
sudo chmod +x /usr/local/bin/syncthing
# ตรวจสอบเวอร์ชัน
syncthing --version
สร้าง User ระบบสำหรับ Syncthing
# สร้าง user ใหม่ที่ไม่มี shell
sudo useradd -r -s /bin/false -m -d /var/lib/syncthing syncthing
# ตรวจสอบ user ถูกสร้างแล้ว
id syncthing
# สร้าง directory สำหรับ config ของ Syncthing
sudo mkdir -p /var/lib/syncthing
sudo chown syncthing:syncthing /var/lib/syncthing
sudo chmod 700 /var/lib/syncthing
ตั้งค่า Systemd Service
เพื่อให้ Syncthing ทำงานอัตโนมัติและรีสตาร์ท เราต้องสร้าง systemd unit file:
sudo nano /etc/systemd/system/[email protected]
วาง content ต่อไปนี้:
[Unit]
Description=Syncthing - Open Source Continuous File Synchronization for %i
After=network.target
Wants=syncthing-inotify@%i.service
[Service]
Type=notify
User=%i
Group=%i
ProtectSystem=full
PrivateTmp=true
SystemCallFilter=@system-service
SystemCallFilter=~@privileged @resources
StandardOutput=journal
StandardError=journal
Restart=on-failure
RestartForceExitStatus=3 4
RestartSec=5
SuccessExitStatus=3 4
ExecStart=/usr/local/bin/syncthing -no-browser -gui-address=127.0.0.1:8384
[Install]
WantedBy=multi-user.target
# เปิด service
sudo systemctl daemon-reload
# เปิดใช้งาน Syncthing สำหรับ user syncthing
sudo systemctl enable syncthing@syncthing
# เริ่มต้น
sudo systemctl start syncthing@syncthing
# ตรวจสอบสถานะ
sudo systemctl status syncthing@syncthing
เข้าถึง Web GUI
Syncthing มาพร้อมกับ Web GUI ที่ทำงานบน port 8384:
# ตรวจสอบว่า GUI ทำงานอยู่
curl -s http://localhost:8384 | head -10
# หรือจากเบราว์เซอร์บนเครื่องอื่น
# ใช้ SSH tunnel เพื่อเข้าถึง GUI อย่างปลอดภัย
ssh -L 8384:localhost:8384 user@your-vps-ip
# แล้วเข้า http://localhost:8384 ใน browser
เพิ่มอุปกรณ์และสร้างโฟลเดอร์ซิงค์
หลังจากเข้า Web GUI ครั้งแรก คุณจะเห็น Device ID ของคุณ:
# ดู Device ID จากบรรทัดคำสั่ง (ถ้าหากจำเป็น)
sudo -u syncthing syncthing --device-id
เพื่อเชื่อมต่ออุปกรณ์อื่น (เช่น laptop หรือโทรศัพท์) ให้ทำตามขั้นตอนต่อไปนี้:
# 1. ในแต่ละอุปกรณ์ ติดตั้ง Syncthing และค้นหา Device ID ของมัน
# 2. ในเว็บ GUI ของ VPS
# - คลิก "Add Remote Device"
# - วาง Device ID ของอุปกรณ์อื่น
# - บันทึก
# 3. บนอุปกรณ์อื่นนั้น
# - ยอมรับการเชื่อมต่อจาก VPS
# - เลือกโฟลเดอร์ที่จะแชร์
# 4. คลิก "Share" ในเว็บ GUI ของ VPS เพื่อตั้งค่าการซิงค์โฟลเดอร์
ตั้งค่า Firewall (ถ้าหากจำเป็น)
# Syncthing ใช้ port เหล่านี้:
# 22000/tcp - device to device communication
# 22000/udp - same
# 21064/udp - discovery
# ถ้าใช้ UFW
sudo ufw allow 22000/tcp
sudo ufw allow 22000/udp
sudo ufw allow 21064/udp
# ตรวจสอบ firewall rules
sudo ufw status
ตรวจสอบว่า Syncthing ทำงานถูกต้อง
# ตรวจสอบ service status
sudo systemctl status syncthing@syncthing
# ดู logs
sudo journalctl -u syncthing@syncthing -n 50 --no-pager
# ตรวจสอบว่า GUI ตอบสนอง
curl -s http://localhost:8384 | grep -i syncthing | head -1
# ตรวจสอบ port ที่เปิด
sudo ss -tuln | grep 8384
sudo systemctl restart syncthing@syncthing แล้วตรวจสอบ logs ว่าแสดงข้อผิดพลาดใด
แก้ปัญหาที่พบบ่อย
Error: "Accepting connection from untrusted device"
คุณลองเชื่อมต่อจากอุปกรณ์ที่ไม่ได้อนุมัติ ไปยัง Web GUI ของ VPS และตรวจสอบ Device ID ที่ขอเชื่อมต่อ ถ้าเป็นอุปกรณ์ของคุณ ให้คลิก "Allow" ใน pop-up ของ GUI
# ตรวจสอบ logs เพื่อเห็นอุปกรณ์ที่พยายามเชื่อมต่อ
sudo journalctl -u syncthing@syncthing | grep "untrusted"
Error: "Folder contents differ" หรือ Conflict Files
เกิดขึ้นเมื่อไฟล์เดียวกันแก้ไขพร้อมกันในอุปกรณ์ที่แตกต่างกัน Syncthing จะสร้างไฟล์ ".sync-conflict" สำเร็จ
# ดูไฟล์ conflict
find /var/lib/syncthing -name "*.sync-conflict*"
# แก้ไขโดยตัดสินใจว่าเวอร์ชันไหนที่ต้องการ แล้วลบไฟล์ conflict
# หรือตั้งค่า "File Versioning" ใน GUI เพื่อเก็บ versions เดิม
Error: "Connection refused" หรือ Devices not discovering
อุปกรณ์ไม่พบกันเนื่องจาก firewall หรือ NAT issues ตรวจสอบว่า port ถูกเปิดและ relay server กำลังทำงาน
# ตรวจสอบการเชื่อมต่อจาก localhost
curl -s http://localhost:8384/rest/system/status | grep -i relayStatus
# ตรวจสอบโดยทั่วไป logs ดูสัญญาณเตือนเกี่ยวกับการค้นหา
sudo journalctl -u syncthing@syncthing | grep -i "discovery\|relay"
Web GUI ไม่สามารถเข้าถึงจากเครื่องอื่น
โดยค่าเริ่มต้น GUI เพียงแต่เข้าถึงได้จาก localhost เท่านั้น เพื่อเข้าถึงจากเครื่องอื่น ให้ใช้ SSH tunnel:
# บนเครื่องคลায์เอนต์
ssh -L 8384:127.0.0.1:8384 user@your-vps
# หรือแก้ไข config.xml เพื่อรับฟังบน 0.0.0.0 (ไม่ปลอดภัย)
sudo nano /var/lib/syncthing/.config/syncthing/config.xml
# หา <gui enabled="true" tls="false"><address>127.0.0.1:8384</address>
# เปลี่ยน 127.0.0.1 เป็น 0.0.0.0 (ระวัง: ท้องถิ่นเท่านั้น ไม่ใช่ internet-facing)
Syncthing ใช้ CPU สูงหรือ Disk I/O ช้า
อาจเป็นเพราะการซิงค์ไฟล์จำนวนมาก หรือ inotify limit ต่ำเกินไป
# ดูจำนวนไฟล์ที่ประมวลผล
sudo journalctl -u syncthing@syncthing | grep -i "scan\|syncing"
# เพิ่ม inotify limit สำหรับ user syncthing
sudo -u syncthing sh -c 'echo "fs.inotify.max_user_watches = 524288" | sudo tee -a /etc/sysctl.conf'
sudo sysctl -p
# ในเว็บ GUI เปิด Settings > Options แล้วลด Rescan Interval ถ้าจำเป็น
คำถามที่พบบ่อย
Syncthing ปลอดภัยไหม?
ใช่ Syncthing เข้ารหัสการสื่อสารทุกอุปกรณ์ด้วย TLS และเชื่อมต่อได้เฉพาะอุปกรณ์ที่คุณอนุมัติ Device ID ให้เท่านั้น โปรเจกต์ Syncthing เคยผ่านการตรวจสอบความปลอดภัยโดยบุคคลภายนอกมาแล้ว (audit โดย Cure53 ปี 2018) แต่ไฟล์ที่ sync จะยังถูกเก็บเป็น plain file บนแต่ละอุปกรณ์ปลายทางตามปกติ ไม่ใช่ zero-knowledge encryption เว้นแต่จะเปิดใช้โหมด "Untrusted Device" ที่เข้ารหัสไฟล์ก่อนเก็บจริง ๆ
Syncthing จะทำให้อินเทอร์เน็ต bandwidth สูงไหม?
ขึ้นอยู่กับจำนวนไฟล์และความถี่ของการเปลี่ยนแปลง หากคุณเพียงแต่ซิงค์เอกสาร configuration ไฟล์ขนาดเล็ก bandwidth ที่ใช้จะน้อยมาก หากซิงค์วิดีโอหรือ database ขนาดใหญ่ คุณอาจต้องติดตามการใช้งาน bandwidth เพิ่มเติม สามารถตั้งค่า Folder Type ในแต่ละโฟลเดอร์เป็น "Send Only" หรือ "Receive Only" แทน "Send & Receive" เพื่อควบคุมทิศทางการซิงค์และลด traffic ที่ไม่จำเป็น
สามารถใช้ Syncthing บนหลาย user account ได้ไหม?
ใช่ แต่แต่ละ user account จะต้องมี service unit เป็นของตัวเอง เช่น syncthing@user1 และ syncthing@user2 แต่ละ service จะมีการตั้งค่าและ config ของตัวเองแยกกัน