Computer >> Hướng Dẫn Máy Tính >  >> Hệ Thống >> Máy Chủ Windows

Cấu hình xác thực SSH Key-Based trên Windows 10/Server 2019

Bài viết cấu hình SSH authentication với RSA key trên Windows để truy cập an toàn remote server/máy tính. Hướng dẫn tạo RSA key (chứng chỉ) trên Windows và cấu hình built-in OpenSSH server trên Windows 10/Windows Server 2019 cho key-based authentication (cho phép authenticate remote host không cần mật khẩu).

Nội dung chính

  • Tạo SSH (RSA) key trên Windows
  • Cấu hình OpenSSH Server Windows authenticate bằng SSH Key
  • Login Windows dùng SSH Key dưới Local Admin

Tạo SSH (RSA) key trên Windows

Phải tạo 2 RSA key (public và private) trên client machine dùng kết nối đến remote Windows server chạy OpenSSH. Private key lưu phía client (không chia sẻ!), public key thêm vào file authorized_keys trên SSH server. Để tạo RSA key trên Windows client, phải cài OpenSSH client.

Trên Windows 10 1809 (và mới hơn) và Windows Server 2019, OpenSSH client cài như feature riêng: Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0

Trên Windows cũ cài port Win32-OpenSSH từ GitHub (xem ví dụ bài setting up SFTP (SSH FTP) server on Windows).

Chạy PowerShell standard (non-privileged) tạo cặp RSA 2048 key: ssh-keygen

Nhắc nhập password bảo vệ private key. Nếu đặt password, mỗi lần dùng key cho SSH authentication phải nhập. Ví dụ không nhập passphrase (không khuyến khích).

Ssh-keygen tạo thư mục .ssh trong profile user Windows hiện tại (C:\Users\your_username) đặt 2 file:

  • id_rsa — private key
  • id_rsa.pub — public key

Sau khi tạo RSA key, thêm private key vào dịch vụ SSH Agent, cho phép quản lý private key tiện lợi và dùng cho authentication. SSH Agent lưu private key và cung cấp trong security context của user hiện tại. Chạy ssh-agent service và cấu hình autostart bằng lệnh PowerShell quản lý dịch vụ: set-service ssh-agent StartupType 'Automatic'
Start-Service ssh-agent

Thêm private key vào database ssh-agent: ssh-add "C:\Users\youruser\.ssh\id_rsa" hoặc ssh-add.exe $ENV:UserProfile\.ssh\id_rsa

Cấu hình OpenSSH Server Windows authenticate bằng SSH Key

Copy public key vừa tạo trên client sang SSH server (ví dụ remote computer chạy Windows 10 1903 đã cấu hình OpenSSH).

Đã thảo luận chi tiết cấu hình OpenSSH server Windows ở bài trước.

Copy file id_rsa.pub vào thư mục .ssh trong profile user dùng kết nối SSH server. Ví dụ user admin trên Windows 10, copy key vào C:\Users\admin\.ssh\authorized_keys.

Copy public key lên SSH server dùng SCP: scp C:\Users\youruser\.ssh\id_rsa.pub admin@192.168.1.15:c:\users\admin\.ssh\authorized_keys

Bây giờ kết nối Windows SSH server không cần mật khẩu. Nếu không set password (passphrase) cho private key, tự động kết nối remote Windows host.

Kết nối remote host dùng native SSH client: ssh (username)@(SSH server name or IP address) Ví dụ: ssh admin@192.168.1.15 (kết nối SSH server IP 192.168.1.15 dưới account admin. SSH Agent tự dùng private key đã lưu để authenticate).

Nếu không muốn dùng ssh-agent service quản lý SSH key, chỉ định đường dẫn file private key cho SSH authentication: ssh admin@192.168.1.15 -i "C:\Users\youruser\.ssh\id_rsa"

Nếu không kết nối được SSH server bằng RSA key và vẫn nhắc nhập mật khẩu, có thể do tài khoản thử kết nối là member của local server administrators group (group SID S-1-5-32-544). Sẽ thảo luận sau.

Login Windows dùng SSH Key dưới Local Admin

OpenSSH dùng cài đặt key-based access đặc biệt cho user có đặc quyền Windows local administrator.

Trước hết, dùng file key C:\ProgramData\ssh\administrators_authorized_keys thay vì file authorized_keys trong user profile. Phải thêm SSH key vào file text này (cho bảo mật, chỉ group Administrators và SYSTEM có quyền đọc file này).

Để dùng authorized_keys file từ user profile và không chuyển data public key sang administrators_authorized_keys, comment dòng liên quan trong file config OpenSSH (C:\ProgramData\ssh\sshd_config).

Comment các dòng: #Match Group administrators
# AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys

Cho phép truy cập Windows dùng RSA key trong sshd_config: PubkeyAuthentication yes

Tắt ssh password login: PasswordAuthentication no

Đừng quên restart dịch vụ sshd sau khi lưu thay đổi sshd_config: restart-service sshd

Điều quan trọng khác. Phiên OpenSSH trước phải cấp quyền đọc cho NT Service\sshd trên file authorized_keys. Làm một trong sau:

  • Cài module OpenSSHUtils: Install-Module -Force OpenSSHUtils -Scope AllUsers. Đổi quyền file: Repair-AuthorizedKeyPermission -FilePath C:\Users\admin\.ssh\authorized_keys
  • Đổi NTFS permission file dùng module NTFSSecurity hoặc icacls;
  • Hoặc tắt StrictModes trong sshd_config. Mặc định mode này bật và ngăn key-based authentication nếu public/private key không được bảo vệ tốt. Uncomment #StrictModes yes, đổi thành StrictModes no.

Đã cấu hình xong SSH authentication trên Windows dùng public RSA key (chứng chỉ). Bây giờ dùng phương thức authentication này truy cập an toàn remote server, tự động forward port SSH tunnel, chạy script và automation tasks khác.