คุณสามารถใช้ Official OpenSSH Package สำหรับ Windows จัดทำ Secure File Transfer ระหว่าง Client และ Windows Server ด้วย Protocol SFTP (Secure FTP) ได้ง่ายๆ ในบทความนี้จะแสดงวิธีใช้ Win32-OpenSSH รัน SFTP Server บน Windows 10 หรือ Windows Server 2016/2012 R2
สารบัญ
- SFTP คืออะไร?
- SFTP Implementation ใน Windows
- ติดตั้ง Win32 OpenSSH บน Windows 10 1803+/Windows Server 2019
- ติดตั้ง Win32 OpenSSH บน Windows Server 2016/2012 R2
- ทดสอบ SFTP Connection ด้วย WinSCP
- วิธี Uninstall Win32 OpenSSH
SFTP คืออะไร?
SFTP (Secure File Transfer Protocol, Secure FTP หรือ SSH FTP) เป็น Extension ของ SSH Protocol เป็นมาตรฐานในโลก UNIX/Linux จากมุมมอง User คล้าย FTP แต่จริงๆ เป็น Protocol ต่างหากสิ้นเชิง ไม่เกี่ยวกับ FTP เลย ข้อมูลถ่ายโอนระหว่าง Client และ Server ผ่าน SSH Tunnel (TCP Port 22)
ข้อดีหลักของ SFTP:
- Files และ Commands ถ่ายโอนภายใน Secure SSH Session
- ใช้ Connection เดียวส่งทั้ง Files และ Commands
- รองรับ Symbolic Links, Interrupt/Resume Transfer, File Delete Functions ฯลฯ
- ใน Channel ที่ FTP ช้าหรือล้มเหลว SFTP Connection มักเร็วและน่าเชื่อถือกว่า
- Authenticate ด้วย SSH Keys ได้
อย่าสับสน SFTP กับ FTPS Protocol — FTPS คือ FTP ธรรมดา + SSL Certificate ส่วน SFTP คือ Protocol ส่ง FTP Data และ Commands ภายใน SSH Session
SFTP Implementation ใน Windows
ประวัติศาสตร์ Windows OS ไม่มี Built-in Tools รัน Secure SFTP Server ใช้ Open-source หรือ Proprietary Solutions เช่น Core FTP, FileZilla, CYGWIN, OpenSSH, FTP Shell, IPSwitch ฯลฯ แต่หลายปีก่อน Microsoft เปิดตัว Win32-OpenSSH Port สำหรับ Win32 โปรเจกต์ชื่อ Win32-OpenSSH
มาดูการตั้งค่า SFTP Server บน Windows 10 หรือ Windows Server 2019/2016/2012 R2 ด้วย Package Win32 OpenSSH
ติดตั้ง Win32 OpenSSH บน Windows 10 1803+/Windows Server 2019
ใน Windows 10 Build 1803 ขึ้นไป และ Windows Server 2019 OpenSSH Package รวมอยู่ใน OS แล้วเป็น Feature on Demand (FoD) เช่น RSAT
ติดตั้ง OpenSSH Server บน Windows 10 และ Windows Server 2019 ด้วย PowerShell Cmdlet:
Add-WindowsCapability -Online -Name OpenSSH.Server*
หรือ DISM: dism /Online /Add-Capability /CapabilityName:OpenSSH.Server~~~~0.0.1.0
หรือจาก Windows 10 GUI (Settings → Apps → Optional Features → Add a feature → Open SSH Server → Install)
- OpenSSH Executables อยู่ที่:
c:\windows\system32\OpenSSH\ - Config File sshd_config อยู่ที่:
C:\ProgramData\ssh(สร้างหลัง Start sshd Service ครั้งแรก) - Log File:
c:\windows\system32\OpenSSH\logs\sshd.log - Authorized_keys File และ Keys เก็บที่:
%USERPROFILE%\.ssh\
ตอนนี้ SSH เข้า Windows 10 ได้เหมือน Linux แล้ว
ติดตั้ง Win32 OpenSSH บน Windows Server 2016/2012 R2
Windows 10 รุ่นก่อน และ Windows Server 2016/2012 R2 ต้องดาวน์โหลดติดตั้ง OpenSSH จาก GitHub (https://github.com/PowerShell/Win32-OpenSSH/releases) ดาวน์โหลด Package Windows x64: OpenSSH-Win64.zip (3.5 MB)
- Extract Archive ไป Target Directory: C:\OpenSSH-Win
- เปิด Elevated PowerShell CLI เข้า OpenSSH Folder:
Cd C:\OpenSSH-Win - เพิ่ม Path ไป Environment Variable (System Properties → Advanced → Environment Variables → Edit Path System Variable → Add Path ไป OpenSSH Folder)
- ติดตั้ง OpenSSH Server:
.\install-sshd.ps1(ควรขึ้นข้อความเขียว "sshd and ssh-agent services successfully installed") - Generate SSH Keys สำหรับ Server (จำเป็นสำหรับ Start sshd Service):
ssh-keygen.exe –A
ssh-keygen: generating new host keys: RSA DSA ECDSA ED25519 - Enable Autostart SSHD Service และ Start ด้วย PowerShell Service Management:
Set-Service -Name sshd -StartupType 'Automatic'
Start-Service sshd - Restart Computer:
Restart-Computer - เปิด TCP Port 22 ใน Windows Firewall สำหรับ Incoming SSH Traffic:
New-NetFirewallRule -Protocol TCP -LocalPort 22 -Direction Inbound -Action Allow -DisplayName SSH
Note: คำสั่งนี้ไม่ทำงานใน Old Desktop Windows ใช้:netsh advfirewall firewall add rule name='SSH Port' dir=in action=allow protocol=TCP localport=22 - เปิด SSHD Config File (C:\ProgramData\SSH\sshd_config) ใน Text Editor หา Directive Subsystem sftp ควรระบุ sftp-server.exe ไว้ที่นี่
พารามิเตอร์เพิ่มเติมใน sshd_config:
AllowGroups corp\sftp_users # อนุญาตเชื่อมต่อ OpenSSH เฉพาะ User ใน Domain Group นี้
AuthenticationMethods password # เปิด Password Authentication (SSH Keys ไม่ได้ใช้)
ForceCommand internal-sftp
ChrootDirectory C:\inetpub\wwwroot\ftpfolder # Default Directory สำหรับ SFTP Users
ทดสอบ SFTP Connection ด้วย WinSCP
ลองเชื่อมต่อ SSH Server ที่สร้างด้วย SFTP Protocol ใช้ Free WinSCP Client
ใน Connection Config Window เลือก SFTP Protocol ระบุ Server Name และ Credentials ของ Windows Account ที่ใช้เชื่อมต่อ (ตั้งค่า Key Authentication ได้)
ครั้งแรกที่เชื่อมต่อ จะมี Notification Host Key ไม่เจอใน Local Cache
หากตั้งค่าถูกต้อง Client จะเชื่อมต่อ SFTP Server แสดงรายการไฟล์ใน User Home Directory (Default คือ Directory ของ User Profile)
ใช้ File Manager Interface คล้าย Total Commander คัดลอกไฟล์ระหว่าง Server และ Client ได้ ไฟล์ถ่ายโอนด้วย Secure SFTP
วิธี Uninstall Win32 OpenSSH
Uninstall Win32 OpenSSH ออกจากระบบอย่างถูกต้อง:
- รัน Elevated PowerShell Session
- Stop SSHD Service:
Stop-Service sshd - Uninstall SSHD Service:
.\uninstall-sshlsa.ps1
sshd successfully uninstalled ssh-agent successfully uninstalled