SSH client tích hợp sẵn xuất hiện trong Windows 10 và Windows Server 2019. Ssh.exe dùng để kết nối an toàn đến server Linux/UNIX, host VMWare ESXi và thiết bị khác thay cho Putty, MTPuTTY và SSH client thứ 3. Native Windows SSH client dựa trên port OpenSSH và pre-installed trong Windows từ build 1809.
Nội dung chính
- Cách bật (cài đặt) OpenSSH Client trên Windows 10
- Sử dụng Native SSH Client trên Windows 10
- Dùng SCP.exe truyền file đến/từ Windows Host qua SSH
Cách bật (cài đặt) OpenSSH Client trên Windows 10
OpenSSH client có trong Windows 10 Features on Demand (như RSAT). SSH client cài mặc định trên Windows Server 2019, Windows 10 1809 và build mới hơn.
Kiểm tra SSH client đã cài: Get-WindowsCapability -Online | ? Name -like 'OpenSSH.Client*'
Ví dụ OpenSSH client đã cài (State: Installed). Nếu chưa (State: Not Present), cài bằng:
- Lệnh PowerShell:
Add-WindowsCapability -Online -Name OpenSSH.Client* - DISM:
dism /Online /Add-Capability /CapabilityName:OpenSSH.Server~~~~0.0.1.0 - Settings → Apps → Optional features → Add a feature. Tìm OpenSSH client → Install.
File binary OpenSSH tại c:\Windows\System32\OpenSSH\:
ssh.exe— executable SSH client;scp.exe— tool copy file trong phiên SSH;ssh-keygen.exe— tool tạo RSA SSH authentication key;ssh-agent.exe— quản lý RSA key;ssh-add.exe— thêm key vào database SSH agent.
Cũng cài OpenSSH trên Windows cũ: tải và cài Win32-OpenSSH từ GitHub (xem ví dụ bài Configuring SSH FTP on Windows).
Sử dụng Native SSH Client trên Windows 10
Khởi động SSH client, chạy PowerShell hoặc cmd.exe prompt. Xem tùy chọn và cú pháp ssh.exe: ssh
usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec] [-D [bind_address:]port] [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11] [-i identity_file] [-J [user@]host[:port]] [-L address] [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address] [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]] destination [command]
Để kết nối remote server qua SSH: ssh username@host
Nếu SSH server chạy port khác TCP/22 chuẩn, chỉ định port: ssh username@host -p port
Ví dụ kết nối Linux host IP 192.168.1.102 as root: ssh root@192.168.1.102
Lần đầu kết nối, thấy yêu cầu thêm host key vào trusted list. Gõ yes Enter. Host key fingerprint thêm vào C:\Users\username\.ssh\known_hosts.
Nhập mật khẩu. Màn hình console remote Linux server mở (ví dụ CentOS trên remote server).
Dùng SSH kết nối cả *Nix OS và Windows. Bài trước hướng dẫn cấu hình OpenSSH server Windows 10 và kết nối từ Windows host dùng SSH client.
Nếu dùng SSH authentication với RSA key (xem ví dụ cấu hình SSH authentication dùng key trên Windows), chỉ định đường dẫn file private key trong SSH client: ssh root@192.168.1.102 -i "C:\Users\username\.ssh\id_rsa"
Cũng thêm private key vào SSH-Agent. Trước hết bật dịch vụ ssh-agent và cấu hình autostart: set-service ssh-agent StartupType 'Automatic'
Start-Service ssh-agent
Thêm private key vào database ssh-agent: ssh-add "C:\Users\username\.ssh\id_rsa"
Sau đó kết nối server qua SSH không cần chỉ định đường dẫn RSA key. Key tự động dùng. Kết nối an toàn không cần mật khẩu (nếu không bảo vệ RSA key bằng passphrase khác): ssh root@192.168.1.102
Một số tham số SSH hữu ích:
-C— nén traffic giữa client/server (hữu ích kết nối chậm/bất ổn)-v— hiển thị chi tiết mọi hành động SSH client-R/-L— forward port dùng SSH tunnel
Dùng SCP.exe truyền file đến/từ Windows Host qua SSH
Dùng tool scp.exe (phần gói SSH client Windows 10), copy file từ máy tính đến SSH server: scp.exe "E:\ISO\CentOS-8.1.x86_64.iso" root@192.168.1.202:/home
Copy toàn bộ nội dung thư mục đệ quy: scp -r E:\ISO\ root@192.168.1.202:/home
Ngược lại, truyền file từ remote server đến máy tính: scp.exe root@192.168.1.202:/home/CentOS-8.1.x86_64.iso c:\iso
Nếu cấu hình authentication bằng RSA key, không nhắc nhập mật khẩu truyền file. Hữu ích cấu hình auto scheduled file copying.
Vậy là bạn có thể kết nối SSH server trực tiếp từ Windows 10, copy file dùng scp không cần app/công cụ thứ 3.