บทความนี้ตั้งค่า SSH Authentication ด้วย RSA Keys บน Windows เพื่อเข้าถึง Remote Servers/Computers อย่างปลอดภัย แสดงวิธี Generate RSA Keys (Certificates) บน Windows และตั้งค่า Built-in OpenSSH Server บน Windows 10/Windows Server 2019 สำหรับ Key-based Authentication (Login เข้า Remote Hosts โดยไม่ต้อง Password)
สารบัญ
- Generating SSH (RSA) Keys บน Windows
- ตั้งค่า OpenSSH Server บน Windows Authenticate ด้วย SSH Keys
- Login Windows ด้วย SSH Key ภายใต้ Local Admin
Generating SSH (RSA) Keys บน Windows
ต้อง Generate RSA Keys 2 คู่ (Public และ Private) บน Client Computer ที่จะใช้ Connect ไป Remote Windows Server ที่รัน OpenSSH Private Key เก็บฝั่ง Client (อย่าให้ใคร!) Public Key ใส่ใน File authorized_keys บน SSH Server
Generate RSA Keys บน Windows Client ต้อง Install OpenSSH Client ก่อน
ใน Windows 10 1809 (ขึ้นไป) และ Windows Server 2019 OpenSSH Client ติดตั้งเป็น Feature แยก: Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
Windows รุ่นก่อน Install Win32-OpenSSH Port จาก GitHub (ดูตัวอย่างในบทความ Setup SFTP (SSH FTP) Server on Windows)
รัน PowerShell Session ธรรมดา (Non-privileged) Generate RSA 2048 Key Pair: ssh-keygen
จะถาม Password ป้องกัน Private Key หากระบุ Password จะต้องใส่ทุกครั้งที่ใช้ Key นี้ Auth SSH ตัวอย่างไม่ใส่ Passphrase (ไม่แนะนำ)
ssh-keygen สร้าง Directory .ssh ใน Profile ของ Current Windows User (C:\Users\your_username) วาง 2 Files:
- id_rsa — Private Key
- id_rsa.pub — Public Key
หลังสร้าง RSA Keys แล้ว เพิ่ม Private Key เข้า SSH Agent Service ที่จัดการ Private Keys ใช้ Auth ได้สะดวก SSH Agent เก็บ Private Keys ให้ใน Security Context ของ Current User รัน ssh-agent Service ตั้ง Auto Startup:
set-service ssh-agent StartupType 'Automatic'
Start-Service ssh-agent
Add Private Key เข้า SSH-Agent Database: ssh-add "C:\Users\youruser\.ssh\id_rsa" หรือ ssh-add.exe $ENV:UserProfile\.ssh\id_rsa
ตั้งค่า OpenSSH Server บน Windows Authenticate ด้วย SSH Keys
Copy Public Key ที่ Generate บน Client ไป SSH Server (ตัวอย่าง Remote Computer รัน Windows 10 1903 มี OpenSSH Configured)
เราอธิบายการตั้งค่า OpenSSH Server ใน Windows เรียบร้อยแล้วในบทความก่อนหน้า
Copy File id_rsa.pub ไป Directory .ssh ใน Profile ของ User ที่จะใช้ Connect เข้า SSH Server ตัวอย่างมี User admin ใน Windows 10 ต้อง Copy Key ไป C:\Users\admin\.ssh\authorized_keys
Copy Public Key ไป SSH Server ด้วย SCP: scp C:\Users\youruser\.ssh\id_rsa.pub admin@192.168.1.15:c:\users\admin\.ssh\authorized_keys
ตอนนี้ Connect เข้า Windows SSH Server โดยไม่ต้อง Password หากไม่ได้ตั้ง Password (Passphrase) สำหรับ Private Key จะ Connect เข้า Remote Windows Host อัตโนมัติ
Connect เข้า Remote Host ด้วย Native SSH Client: ssh (username)@(SSH server name or IP address) ตัวอย่าง: ssh admin@192.168.1.15 (หมายถึง Connect เข้า SSH Server IP 192.168.1.15 ด้วย Account admin SSH Agent จะลองใช้ Private Key ที่บันทึกไว้ Auth อัตโนมัติ)
หากไม่ต้องการใช้ ssh-agent Service จัดการ SSH Keys ระบุ Path ไป Private Key File สำหรับ SSH Authentication: ssh admin@192.168.1.15 -i "C:\Users\youruser\.ssh\id_rsa"
หาก Connect เข้า SSH Server ด้วย RSA Key ไม่ได้ ยังถูกถาม Password น่าจะเป็นเพราะ User Account ที่พยายาม Connect เป็น Member ของ Local Server Administrators Group (Group SID: S-1-5-32-544) จะอธิบายภายหลัง
Login Windows ด้วย SSH Key ภายใต้ Local Admin
OpenSSH ใช้ Key-based Access Settings พิเศษสำหรับ Users ที่มี Windows Local Administrator Privileges
ก่อนอื่น ใช้ Key File C:\ProgramData\ssh\administrators_authorized_keys แทน File authorized_keys ใน User Profile ต้อง Add SSH Key ลงใน Text File นี้ (เพื่อ Security เฉพาะ Administrators Group และ SYSTEM เท่านั้นที่ควรมี Permission อ่าน File นี้)
เพื่อใช้ authorized_keys File จาก User Profile แทนที่จะย้าย Public Key Data ไป administrators_authorized_keys File สามารถ Comment บรรทัดที่เกี่ยวข้องใน OpenSSH Config File (C:\ProgramData\ssh\sshd_config)
Comment บรรทัดเหล่านี้:
#Match Group administrators
# AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys
Allow Access Windows ด้วย RSA Keys ใน sshd_config: PubkeyAuthentication yes
Disable SSH Password Login: PasswordAuthentication no
Restart sshd Service หลัง Save Changes ใน sshd_config: restart-service sshd
สิ่งสำคัญอีกอย่าง: OpenSSH รุ่นก่อน ต้อง Grant NT Service\sshd Read Permissions บน authorized_keys File
ทำได้โดย:
- Install OpenSSHUtils Module:
Install-Module -Force OpenSSHUtils -Scope AllUsersเปลี่ยน File Permissions:Repair-AuthorizedKeyPermission -FilePath C:\Users\admin\.ssh\authorized_keys - เปลี่ยน NTFS Permissions ด้วย NTFSSecurity Module หรือ icacls
- หรือ Disable StrictModes ใน sshd_config Default Enabled ป้องกัน Key-based Auth หาก Public/Private Keys ไม่ Protected ให้เพียงพอ Uncomment
#StrictModes yesเปลี่ยนเป็นStrictModes no
ตอนนี้ตั้งค่า SSH Authentication บน Windows ด้วย Public RSA Key (Certificate) เสร็จแล้ว ใช้ Authentication Method นี้เข้าถึง Remote Servers อย่างปลอดภัย Forward Ports ใน SSH Tunnel อัตโนมัติ รัน Scripts และ Automation Tasks อื่นๆ ได้