Tôi có các máy chủ có thể kết nối qua giao thức SSH, làm sao để map thư mục trên đó thành ổ đĩa trên Windows? Đây là giải pháp.

Để Windows map được các thư mục qua SSH, File System của Windows cần hỗ trợ SSHFS (SSH File System) giống như trên hệ thống Linux.
Các phần mềm cần thiết
- WinFsp – Windows File System Proxy
- SSHFS-Win – SSHFS for Windows
- SSHFS-Win Manager – A GUI for SSHFS-Win
Cài lần lượt các phần mềm theo thứ tự trên. Sau đó khởi động SSHFS-Win Manager
từ Start Menu để cấu hình kết nối đển thư mục Remote qua SSH.
Map ổ đĩa từ dòng lệnh
Bạn có thể dùng dòng lệnh từ cmd
để map một ổ SSH bất kỳ bằng dòng lệnh sshfs-win
của gói SSHFS-Win. Tham số được gợi ý như sau:
usage: sshfs-win cmd SSHFS_COMMAND_LINE SSHFS_COMMAND_LINE command line to pass to sshfs usage: sshfs-win svc PREFIX X: [LOCUSER] [SSHFS_OPTIONS] PREFIX Windows UNC prefix (note single backslash) \sshfs[.r|k]\[LOCUSER=]REMUSER@HOST[!PORT][\PATH] sshfs: remote home; sshfs.r: remote root sshfs.k: remote home with key authentication LOCUSER local user (DOMAIN+USERNAME) REMUSER remote user HOST remote host PORT remote port PATH remote path (relative to remote home or root) X: mount drive SSHFS_OPTIONS additional options to pass to SSHFS
Như vậy là có hai kiểu Map ổ đĩa là dùng sshfs-win cmd
hoặc sshfs svc
. Dùng sshfs-win cmd
chúng ta sẽ có một ổ đĩa giống ổ vật lý, còn dùng sshfs-win svc
chúng ta có một ổ đĩa mạng (Network Drive).
Map ổ đĩa SSH với sshfs-win svc
Với kiểu map ổ đĩa bằng sshfs-win svc
tương đương với việc map ổ đĩa qua giao diện Map network drive...
của Windows. Để sử dụng dòng lệnh, bạn hãy tham khảo file BATCH như sau.
Lưu ý:
- Tôi dùng
\\sshfs.k
đại diện cho ổ đĩa SSH đăng nhập bằng SSH key. Bạn có thể thay bằng\\sshfs.r
để dùng password hoặc\\sshfs
để jail trong thư mục user. - Đường dẫn của máy remote dùng forward slash chứ không dùng backslash.
- Đường dẫn đến SSH key mặc định là
%USERPROFILE%\.ssh\id_rsa
.
@echo off :: Local drive settings set LC_DRIVE=K: :: Remote SSH settings set RM_USER=icreativ set RM_HOST=example.com set RM_PORT=22 set RM_PATH=\path\to\directory set RM_PREFIX=\\sshfs.k\%RM_USER%@%RM_HOST%!%RM_PORT%%RM_PATH% echo %RM_PREFIX% is mapped as Drive %LC_DRIVE%... :: SSHFS-Win command location :: Download: https://github.com/billziss-gh/sshfs-win set APP_PATH="C:\Program Files\SSHFS-Win\bin\sshfs-win.exe" :: Execute the mount command %APP_PATH% svc %RM_PREFIX% %LC_DRIVE%
Tuy nhiên tôi lại hay gặp lỗi Permission khi dùng cách này nên không ưa lắm. Có lẽ bug này sẽ được fix một ngày nào đó.
Map ổ đĩa SSH với sshfs-win cmd
Với kiểu map ổ đĩa bằng sshfs-win cmd
, ta có nhiều lựa chọn hấp dẫn hơn. Ổ đĩa sẽ được map thành một ổ đĩa vật lý thay vì Network drive.
Tôi tạo ra một file BATCH như sau:
@echo off :: Local drive settings set LC_DRIVE=K: set LC_LABEL="Awesome Server" :: Remote SSH settings set RM_USER=icreativ set RM_HOST=example.com set RM_PORT=22 set RM_PATH=/path/to/directory set SSH_KEY=C:\\cygwin64\\home\\icreativ\\.ssh\\id_rsa :: SSHFS-Win command location :: Download: https://github.com/billziss-gh/sshfs-win set APP_PATH="C:\Program Files\SSHFS-Win\bin\sshfs-win.exe" :: Execute the mount command %APP_PATH% cmd %RM_USER%@%RM_HOST%:%RM_PATH% %LC_DRIVE% -p%RM_PORT% -oServerAliveInterval=60 -ovolname=%LC_LABEL% -oPreferredAuthentications=publickey -oIdentityFile=%SSH_KEY% -oidmap=user -ouid=-1 -ogid=-1 -oumask=000 -ocreate_umask=000 -omax_readahead=1GB -oallow_other -olarge_read -okernel_cache -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null
Nhược điểm khi dùng sshfs-win cmd
là bạn không được tắt cửa sổ cmd
chạy lệnh này, nếu không ổ đĩa sẽ bị Unmap. Bạn có thể tham khảo cách chạy file BAT không hiện cửa sổ command prompt.