Windows SSH免密连接Linux服务器

📅 2022-04-02 13:58 👁️ 654 💬 0

Windows SSH免密连接Linux服务器

我们在自己电脑上需要远程连接Linux服务器时,每次都需要输入 ssh @username ip -p port,还需要输入登录密码,过程复杂,因此准备在Windows上配置SSH免密连接Linux服务器

前提:电脑上已经存在一对SSH-Key,准备新建一对SSH-Key

1 创建SSH-Key

# 1.生产密钥 以rsa算法
C:\Users\Administrator\.ssh> ssh-keygen -t rsa -C "linux" 
Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\Administrator/.ssh/id_rsa): C:\Users\Administrator/.ssh/id_rsa_linux #2.填写新的地址
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in C:\Users\Administrator/.ssh/id_rsa_linux.
Your public key has been saved in C:\Users\Administrator/.ssh/id_rsa_linux.pub.
The key fingerprint is:
SHA256:QeTc5iTO6rhOTW1a5IfgcOw3rMPjhdCYpcrz2VPpbMg linux
The key's randomart image is:
+---[RSA 2048]----+
|       .o        |
|     . + .       |
|    . = * +      |
|     @ O B       |
|    = = S.o      |
| . . = Ooo       |
|  + ..X=.        |
|   + *E++        |
|   .*.+o         |
+----[SHA256]-----+
PS C:\Users\Administrator\.ssh>
 

image-20220402101618962

2 将公钥拷贝到Linux服务器上(拷贝或上传)

位置:/root/.ssh

image-20220402133431800

如果不存在 authorized_keys,则新建一个文件,将生成的公钥复制追加到authorized_keys中

还可以通过上传的方式,将公钥上传到服务器上:

  1. 使用 Git Bash ,在Windows ssh文件夹下右击打开 Git Bash 输入 ssh-copy-id root@ip -p prot

  2. 输入密码,后即可上传

3 Linux服务器修改SSH配置

位置:/etc/ssh/sshd_config

image-20220402133827103

PermitRootLogin yes
RSAAuthentication yes
PubkeyAuthentication yes

注意:修改玩后重启SSH服务:

  1. service ssh restart // centos 6的命令
  2. systemctl restart sshd // centos 7的命令

4.在Windows ssh文件下新建config文件

image-20220402135158873

内容如下:

Host name ## 自定义名称
  HostName ip  ## ip地址
  Port port ## 端口号
  User user ## 登录用户名
  IdentityFile ~/.ssh/id_rsa_linux ##使用的私钥

5.测试使用

命令行中输入:ssh 自定义名称 (与上述 Host 后面定义的名称一致)

image-20220402135540085

登录后才能查看或发表评论, 立即 登录
点击右上角即可分享
微信分享提示