Bạn có thể dùng gói OpenSSH chính thức cho Windows để tổ chức truyền file an toàn giữa client và Windows server qua giao thức SFTP (Secure FTP). Bài viết hướng dẫn dùng Win32-OpenSSH chạy SFTP server trên Windows 10 hoặc Windows Server 2016/2012 R2.
Nội dung chính
- SFTP là gì?
- Triển khai SFTP trên Windows
- Cài Win32 OpenSSH trên Windows 10 1803+/Windows Server 2019
- Cài Win32 OpenSSH trên Windows Server 2016/2012 R2
- Test kết nối SFTP dùng WinSCP
- Gỡ cài đặt Win32 OpenSSH
SFTP là gì?
SFTP (Secure File Transfer Protocol, Secure FTP hoặc SSH FTP) là mở rộng của giao thức SSH, là tiêu chuẩn trong thế giới UNIX/Linux. Từ góc nhìn người dùng, nó tương tự FTP, nhưng thực tế là giao thức hoàn toàn khác, không liên quan gì đến FTP. Dữ liệu truyền giữa client và server qua SSH tunnel (TCP port 22).
Ưu điểm chính của SFTP:
- File và lệnh truyền trong phiên SSH bảo mật;
- Dùng một kết nối gửi cả file và lệnh;
- Hỗ trợ symbolic links, interrupt/resume transfer, xóa file, v.v.;
- Thường thì ở kênh nơi FTP chậm hoặc thất bại, kết nối SFTP nhanh và tin cậy hơn;
- Khả năng xác thực bằng SSH key.
Không nhầm lẫn SFTP và FTPS. FTPS chỉ là FTP đơn giản với chứng chỉ SSL, SFTP là giao thức gửi dữ liệu và lệnh FTP bên trong phiên SSH.
Triển khai SFTP trên Windows
Lịch sử, Windows OS không cung cấp công cụ built-in chạy SFTP server bảo mật. Dùng giải pháp open-source hoặc thương mại: Core FTP, FileZilla, CYGWIN, OpenSSH, FTP Shell, IPSwitch, v.v. Tuy nhiên, vài năm trước Microsoft phát hành bản port OpenSSH cho Win32. Dự án gọi Win32-OpenSSH.
Xem cấu hình SFTP server chạy Windows 10 hoặc Windows Server 2019/2016/2012 R2 dùng gói Win32 OpenSSH.
Cài Win32 OpenSSH trên Windows 10 1803+/Windows Server 2019
Trên Windows 10 build 1803 trở lên và Windows Server 2019, gói OpenSSH đã có sẵn trong OS dạng Feature on Demand (FoD) như RSAT.
Cài OpenSSH server trên Windows 10 và Windows Server 2019 bằng cmdlet PowerShell: Add-WindowsCapability -Online -Name OpenSSH.Server*
Hoặc DISM: dism /Online /Add-Capability /CapabilityName:OpenSSH.Server~~~~0.0.1.0
Cũng thêm OpenSSH server từ GUI Windows 10 (Settings → Apps → Optional Features → Add a feature → Open SSH Server → Install).
- File thực thi OpenSSH tại:
c:\windows\system32\OpenSSH\; - File config sshd_config tại:
C:\ProgramData\ssh(thư mục tạo sau lần start đầu tiên của dịch vụ sshd); - Log file:
c:\windows\system32\OpenSSH\logs\sshd.log; - File authorized_keys và key lưu tại:
%USERPROFILE%\.ssh\.
Bây giờ có thể kết nối Windows 10 qua SSH như Linux.
Cài Win32 OpenSSH trên Windows Server 2016/2012 R2
Trên Windows 10 cũ và Windows Server 2016/2012 R2, phải tải và cài OpenSSH từ GitHub (https://github.com/PowerShell/Win32-OpenSSH/releases). Tải gói Windows x64: OpenSSH-Win64.zip (3,5 MB).
- Giải nén archive vào thư mục đích: C:\OpenSSH-Win;
- Mở PowerShell admin và chuyển thư mục OpenSSH:
Cd C:\OpenSSH-Win - Thêm đường dẫn thư mục OpenSSH vào biến môi trường Path (System Properties → Advanced tab → Environment Variables → Chọn và edit biến hệ thống Path → Thêm đường dẫn thư mục OpenSSH);
- Cài OpenSSH server:
.\install-sshd.ps1(thông báo xanh "sshd and ssh-agent services successfully installed"); - Tạo SSH key cho server (cần để start dịch vụ sshd):
ssh-keygen.exe –A(output: ssh-keygen: generating new host keys: RSA DSA ECDSA ED25519); - Bật autostart dịch vụ SSHD và start nó bằng lệnh PowerShell quản lý dịch vụ:
Set-Service -Name sshd -StartupType 'Automatic'vàStart-Service sshd - Restart máy tính:
Restart-Computer - Dùng PowerShell mở TCP port 22 Windows Firewall cho traffic SSH inbound:
New-NetFirewallRule -Protocol TCP -LocalPort 22 -Direction Inbound -Action Allow -DisplayName SSH. Lưu ý. Lệnh trước không chạy trên Windows desktop cũ. Dùng lệnh:netsh advfirewall firewall add rule name='SSH Port' dir=in action=allow protocol=TCP localport=22 - Mở file config SSHD (C:\ProgramData\SSH\sshd_config) trong text editor. Tìm và kiểm tra giá trị directive Subsystem sftp. File sftp-server.exe phải được chỉ định ở đây.
Có thể cấu hình thêm tham số sau trong file sshd_config:
AllowGroups corp\sftp_users # cho phép kết nối OpenSSH chỉ cho user trong domain group này
AuthenticationMethods password # bật xác thực mật khẩu (không dùng SSH key)
ForceCommand internal-sftp
ChrootDirectory C:\inetpub\wwwroot\ftpfolder # thư mục mặc định cho user SFTP
Test kết nối SFTP dùng WinSCP
Thử kết nối SSH server vừa tạo qua giao thức SFTP. Dùng client miễn phí WinSCP.
Cửa sổ cấu hình kết nối, chọn SFTP làm giao thức truyền file, nhập tên server và credential tài khoản Windows dùng kết nối (cũng có thể cấu hình key authentication).
Lần đầu kết nối, hiện thông báo host key không tìm thấy trong local cache. Nếu cấu hình đúng, client kết nối SFTP server và hiển thị danh sách file trong thư mục home user (mặc định là thư mục profile user).
Dùng giao diện file manager quen thuộc (như Total Commander), copy file giữa server và client. File truyền qua SFTP bảo mật.
Gỡ cài đặt Win32 OpenSSH
Để gỡ Win32 OpenSSH đúng cách:
- Mở PowerShell session quyền admin;
- Dừng dịch vụ SSHD:
Stop-Service sshd - Gỡ dịch vụ sshd:
.\uninstall-sshlsa.ps1sshd successfully uninstalled ssh-agent successfully uninstalled