一、基础环境
服务端操作系统:Microsoft Windows Server 2019 数据中心版
服务端软件版本:OpenSSH_for_Windows_8.6p1, LibreSSL 3.3.3
服务端的IP地址:192.168.1.100
客户端操作系统:Microsoft Windows 10 专业版 10.0.19042
客户端软件版本:OpenSSH_for_Windows_8.1p1, LibreSSL 3.0.2
二、操作步骤
1、【生成证书】在服务端操作:
C:\Users\YangQun>ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\YangQun/.ssh/id_rsa):<按回车键取默认值>
Enter passphrase (empty for no passphrase):<按回车键取默认值>
Enter same passphrase again:<按回车键取默认值>
Your identification has been saved in C:\Users\YangQun/.ssh/id_rsa.
Your public key has been saved in C:\Users\YangQun/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:7zY9nauZr1t+3VqP8DjZPfvs+2idt6xxy06apuWRHR8 yangqun@YangQun-PC
The key's randomart image is:
+---[RSA 4096]----+
| |
| |
| |
| |
| S E |
| . o +|
| ...O.B*|
| .o BX#BX|
| ..oX&@&#|
+----[SHA256]-----+
C:\Users\YangQun>
该命令执行后,可以在 C:\Users\YangQun/.ssh/ 目录下会找到两个文件,其中 id_rsa 为私钥(相当于钥匙),id_rsa.pub为公钥(相当于锁)。
2、【添加信任】在服务端操作,将 id_rsa.pub 的内容添加到 PROGRAMDATA/ssh/administrators_authorized_keys 中。(具体文件需要在 ssh_config 中最后两行查看)
3、将 id_rsa 复制到客户端任意位置,执行以下以下命令进行远程登录。
ssh -i .\id_rsa -l administrator 192.168.1.100
#或者
ssh -i .\id_rsa [email protected]
可以直接连接,不用输入密码了。
4、通过以下命令就可以直接将本地文件复制到服务器上面了。
scp -i .\id_rsa 文本文档.txt [email protected]:D:\
注意:scp 没有-l 这个选项,只能使用@将用户帐户和主机名隔开。如果端口号不是默认的22,需要使用 -P 来指定端口号,P必须为大写。