Mount Directory จาก VPS ด้วย SSHFS บน Linux/Mac
SSHFS (SSH Filesystem) ทำให้ mount folder จาก VPS มาใน local machine เหมือน network drive traffic ทั้งหมด encrypt ผ่าน SSH อัตโนมัติ ใช้ประโยชน์ได้หลายอย่าง เช่น แก้ไขไฟล์บน VPS ด้วย VS Code local, ดูรูปภาพบน server โดยตรง หรือ copy ไฟล์ระหว่าง server กับ local ด้วย drag-and-drop
ติดตั้ง SSHFS
# Ubuntu/Debian
sudo apt install sshfs -y
# macOS (ต้องการ macFUSE ก่อน)
brew install macfuse sshfs
# หรือ download macFUSE จาก https://osxfuse.github.io
# ตรวจ
sshfs --version
Mount VPS Folder ด้วย SSHFS
# สร้าง mount point
mkdir -p ~/vps-files
# Mount ด้วย SSH key
sshfs ubuntu@VPS_IP:/var/www/html ~/vps-files \
-o IdentityFile=~/.ssh/id_rsa \
-o reconnect \
-o ServerAliveInterval=15 \
-o ServerAliveCountMax=3
# Mount ด้วย password (ถ้าไม่ใช้ key)
sshfs ubuntu@VPS_IP:/home/ubuntu ~/vps-files
# ตรวจว่า mount สำเร็จ
df -h | grep vps-files
ls ~/vps-files
Unmount เมื่อเสร็จแล้ว
# Linux
fusermount -u ~/vps-files
# macOS
umount ~/vps-files
# หรือ
diskutil unmount ~/vps-files
Mount อัตโนมัติด้วย /etc/fstab (Linux)
sudo nano /etc/fstab
# เพิ่มบรรทัด:
ubuntu@VPS_IP:/var/www/html /home/user/vps-files fuse.sshfs noauto,user,_netdev,reconnect,IdentityFile=/home/user/.ssh/id_rsa,allow_other 0 0
# Mount ตาม fstab
mount ~/vps-files
ใช้กับ VS Code Remote File
# หลัง mount แล้ว เปิด VS Code
code ~/vps-files
# VS Code จะเห็น files บน VPS เหมือน local project
# แก้ไขและ save ไฟล์จะ update บน VPS ทันที
# หรือใช้ Terminal ใน VS Code ทำงานบน local
# ในขณะที่ไฟล์ sync ผ่าน SSHFS
-o cache=yes -o kernel_cache เพื่อเปิด local cache ทำให้ read ไฟล์เดิมซ้ำๆ เร็วขึ้น แต่ถ้าหลายคนแก้ไฟล์พร้อมกันควรปิด cache เพื่อให้เห็น version ล่าสุดเสมอ
ปรับ Option ให้เสถียรและเร็วขึ้น
SSHFS แบบ default มักหลุดเมื่อเน็ตสะดุดและช้ากับไฟล์เยอะ เพิ่ม option เหล่านี้ตอน mount เพื่อให้ทนทานและ responsive ขึ้น:
sshfs user@vps:/var/www ~/mnt/vps \
-o reconnect \
-o ServerAliveInterval=15 \
-o ServerAliveCountMax=3 \
-o cache=yes \
-o kernel_cache \
-o compression=yes \
-o idmap=user
reconnect+ServerAliveInterval— กู้ connection อัตโนมัติเมื่อเน็ตหลุดชั่วคราว ไม่ต้อง unmount/mount ใหม่cache=yes,kernel_cache— cache attribute/directory ลดการ round-trip ทำให้lsในโฟลเดอร์ไฟล์เยอะเร็วขึ้นชัดcompression=yes— ช่วยเมื่อ bandwidth จำกัด (เช่น mount ข้ามประเทศ) แต่กิน CPU เพิ่ม
rsync sync ลงเครื่องแล้วทำงาน local จะเร็วกว่ามากตรวจสอบว่า SSHFS ทำงานถูกต้อง
# ตรวจ mount point
mount | grep sshfs
df -h | grep vps-files
# ทดสอบ read/write
echo "test" > ~/vps-files/sshfs-test.txt
cat ~/vps-files/sshfs-test.txt
# ตรวจบน VPS ว่าไฟล์มีจริง
ssh ubuntu@VPS_IP "cat /var/www/html/sshfs-test.txt"
-o Ciphers=aes128-ctr เพื่อใช้ cipher เบาขึ้น หรือ -o compression=no ถ้า network เร็วอยู่แล้ว compression อาจเพิ่ม CPU overhead ไม่คุ้ม
แก้ปัญหาที่พบบ่อย
Error: "Transport endpoint is not connected"
SSH connection หลุด ต้อง unmount และ mount ใหม่:
fusermount -uz ~/vps-files # force unmount
sshfs ubuntu@VPS_IP:/path ~/vps-files -o reconnect
Permission denied เมื่อเขียนไฟล์
User บน VPS ไม่มีสิทธิ์เขียนใน folder นั้น ตรวจ ownership:
ssh ubuntu@VPS_IP "ls -la /var/www/html"
ssh ubuntu@VPS_IP "sudo chown ubuntu:ubuntu /var/www/html"
SSHFS ช้ามาก (latency สูง)
SSHFS ไม่เหมาะกับ VPS ที่อยู่ไกล latency สูง ใช้ rsync sync batch file แทน หรือเลือก VPS ที่ใกล้ geography เพื่อลด latency
macOS: "Permission denied" หลัง macFUSE
ต้องให้ permission macFUSE ใน System Preferences > Security & Privacy > General หลัง install ครั้งแรก:
sudo /Library/Filesystems/macfuse.fs/Contents/Resources/load_macfuse
คำถามที่พบบ่อย
SSHFS กับ rsync ต่างกันอย่างไร?
SSHFS mount filesystem แบบ real-time ทุก operation ส่งผ่าน network ทันที rsync copy/sync ไฟล์เป็น batch ครั้งละหลายไฟล์ SSHFS เหมาะสำหรับ interactive editing rsync เหมาะสำหรับ backup หรือ deploy ที่ต้องการ efficiency สูง
ใช้ SSHFS บน Windows ได้ไหม?
ได้ผ่าน WinFSP + SSHFS-Win ติดตั้งทั้งคู่แล้วเปิด \\sshfs\user@HOST\path\ ใน File Explorer หรือใช้ WSL2 แล้วรัน SSHFS ใน Linux environment
SSHFS ปลอดภัยไหมเทียบกับ FTP?
ปลอดภัยกว่า FTP มาก เพราะ encrypt ทุก byte ด้วย SSH ไม่มี plaintext credential ส่งผ่าน network ต่างจาก FTP ที่ username/password และ file content วิ่งเป็น plaintext