Mặc định Windows không có SSH server. Cài OpenSSH Server giúp bạn kết nối vào VPS Windows bằng lệnh SSH từ bất kỳ đâu, dùng SCP/SFTP để copy file, không cần mở RDP.
Yêu cầu: Windows 10 (1809+), Windows Server 2019/2022. Windows Server 2016 cần cài thủ công.
==== CÀI TRÊN WINDOWS 10/11 VÀ SERVER 2019/2022 ====
Cách 1: Cài qua Settings (Windows 10/11)
1. Vào Settings → Apps → Optional Features
2. Nhấn Add a feature
3. Tìm OpenSSH Server → Install
4. Đợi cài xong
Cách 2: Cài qua PowerShell (nhanh hơn)
# Kiểm tra OpenSSH có sẵn không
Get-WindowsCapability -Online | Where-Object Name -like "OpenSSH*"
# Cài OpenSSH Client (để dùng lệnh ssh, scp)
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
# Cài OpenSSH Server
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Khởi động và bật tự động:
# Khởi động SSH server
Start-Service sshd
# Tự khởi động khi reboot
Set-Service -Name sshd -StartupType Automatic
# Kiểm tra đang chạy
Get-Service sshd
Mở firewall port 22:
# Windows tự tạo rule khi cài, kiểm tra:
Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP"
# Nếu chưa có, tạo thủ công:
New-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -DisplayName "OpenSSH Server (sshd)" -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
Test kết nối:
# Từ máy khác, kết nối vào Windows VPS
ssh Administrator@103.157.204.101
# Nếu dùng port khác
ssh -p 2222 Administrator@103.157.204.101
==== CÀI TRÊN WINDOWS SERVER 2016 ====
Windows Server 2016 không có OpenSSH trong Optional Features, cần tải thủ công:
# Tải bản mới nhất từ GitHub
$url = "https://github.com/PowerShell/Win32-OpenSSH/releases/latest/download/OpenSSH-Win64.zip"
Invoke-WebRequest -Uri $url -OutFile "C:\OpenSSH-Win64.zip"
# Giải nén
Expand-Archive "C:\OpenSSH-Win64.zip" -DestinationPath "C:\Program Files\"
Rename-Item "C:\Program Files\OpenSSH-Win64" "OpenSSH"
# Cài service
cd "C:\Program Files\OpenSSH"
.\install-sshd.ps1
# Khởi động
Start-Service sshd
Set-Service -Name sshd -StartupType Automatic
Set-Service -Name ssh-agent -StartupType Automatic
# Mở firewall
New-NetFirewallRule -Name "OpenSSH" -DisplayName "OpenSSH Server" -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
==== CẤU HÌNH OPENSSH ====
File config chính:
C:\ProgramData\ssh\sshd_config
Đổi port SSH (tránh bị scan):
notepad C:\ProgramData\ssh\sshd_config
# Tìm dòng #Port 22, sửa thành:
Port 2222
# Mở port mới trên firewall
New-NetFirewallRule -DisplayName "SSH Custom Port" -Direction Inbound -Protocol TCP -LocalPort 2222 -Action Allow
# Restart SSH
Restart-Service sshd
Tắt đăng nhập bằng password (dùng SSH key):
# Trong sshd_config sửa:
PasswordAuthentication no
PubkeyAuthentication yes
Đặt shell mặc định là PowerShell:
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -PropertyType String -Force
==== DÙNG SSH KEY ====
Tạo SSH key trên máy client:
ssh-keygen -t ed25519 -C "my-windows-vps"
Copy public key lên Windows VPS:
# Trên máy client, copy nội dung public key:
cat ~/.ssh/id_ed25519.pub
# Trên Windows VPS, tạo file authorized_keys:
mkdir C:\Users\Administrator\.ssh
notepad C:\Users\Administrator\.ssh\authorized_keys
# Paste nội dung public key vào, lưu lại
Hoặc dùng lệnh từ máy client:
type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh Administrator@VPS_IP "mkdir -Force $env:USERPROFILE\.ssh; Add-Content $env:USERPROFILE\.ssh\authorized_keys -Value (Get-Content /dev/stdin)"
==== DÙNG SCP VÀ SFTP VỚI WINDOWS ====
Sau khi cài OpenSSH, dùng SCP/SFTP y hệt Linux:
# Upload file lên Windows VPS
scp file.txt Administrator@103.157.204.101:C:/Users/Administrator/
# Download file từ Windows VPS
scp Administrator@103.157.204.101:C:/Users/Administrator/file.txt ./
# Copy thư mục
scp -r ./myapp Administrator@103.157.204.101:C:/inetpub/wwwroot/
# SFTP interactive
sftp Administrator@103.157.204.101
Xem log SSH để debug:
Get-EventLog -LogName Application -Source "OpenSSH*" -Newest 20 | Format-List TimeGenerated, Message
Các lệnh quản lý SSH service:
Start-Service sshd # khởi động
Stop-Service sshd # dừng
Restart-Service sshd # restart
Get-Service sshd # xem trạng thái