Post Views: 10,211
อยาก ssh เข้า Windows Server เพื่อเข้าไปรัน PowerShell หรือรัน script cmd ต้องทำอย่างไร
(โดยปกติ Windows ปัจจุบันมี OpenSSH Client ติดตั้งมาโดย Default ถ้าใครต้องการแค่จะใช้ ssh/scp command ข้ามบนความนี้ได้เลยครับ)
Reference :
– https://docs.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse Installation of OpenSSH For Windows Server 2019 and Windows 10
Environment :
– Windows Server 2019 หรือ Windows 10 1809 ขึ้นไป
- วิธีการติดตั้ง OpenSSH Server ผ่าน GUI
เปิด Settings > Apps > Apps and Features > Manage Optional Features (Windows 10 ใช้คำว่า Optional Features)


- วิธีการติดตั้ง OpenSSH Server ผ่าน PowerShell (Run as Administrator)
– ตรวจสอบ Version ของ OpenSSH ที่จะลงก่อน
Get-WindowsCapability -Online | ? Name -like 'OpenSSH*'
– ทำการติดตั้งตาม Version ล่าสุด
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

- วิธีการสร้าง Service ให้ Start OpenSSH Server โดยอัตโนมัติเมื่อเปิดเครื่องผ่าน PowerShell Run as Administrator และทำการเปิด Firewall ให้เข้าถึงได้
Start-Service sshd
# OPTIONAL but recommended:
Set-Service -Name sshd -StartupType 'Automatic'
# Confirm the Firewall rule is configured. It should be created automatically by setup.
Get-NetFirewallRule -Name *ssh*
# There should be a firewall rule named "OpenSSH-Server-In-TCP", which should be enabled
# If the firewall does not exist, create one
New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22

- ทดสอบเข้าใช้งาน (สำหรับเครื่องที่ join office365 แนะนำให้เข้าผ่าน local account ครับ)
ssh [username]@[Windows Server IP]

- การใช้งานต้องใช้คำสั่งของ powershell นะครับ ไม่ใช่ shell script ของ linux ดังนั้นการกดออกต้องใช้คำสั่ง exit

ในแง่เดียวกันสามารถทำ ssh tunnel ไปมาระหว่าง Windows และ Linux ได้นะครับ แต่คงจะไม่ได้เขียน Blog ในส่วนนี้ครับ แต่จะไม่มี command ssh-copy-id ใน windows ให้ใช้คงต้อง ssh-keygen แล้ว copy key ไปสร้างในเครื่อง linux เองครับ