Liunx两台服务器实现相互SSH免密登录

1 篇文章 0 订阅

一、首先准备两台Linux虚拟机当作此次实验的两台服务器

服务器1:server        IPV4:192.168.110.136

服务器2:client         IPV4: 192.168.110.134

二、准备阶段

[root@server ~]# systemctl disable firewalld          #关闭防火墙

[root@server ~]# setenforce  0                                   #关闭seLiunx

[root@server ~]# getenforce

Permissive

[root@client ~]# systemctl disable firewalld          #关闭防火墙

[root@client ~]# setenforce  0                                   #关闭seLiunx

[root@client ~]# getenforce

Permissive

三、配置

server免密访问client

[root@server ~]# ssh-keygen -t rsa                                #此处为rsa密钥对
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:EtRb4lrkGMtSaa+JoG3bL2AfzUlJPbx114ZZyOmOf04 root@server
The key's randomart image is:
+---[RSA 3072]----+
|              .=     . B.  |
|          .* B o . B o  |
|          =.X B . o .   |
|          . . *.B     .    |
|       o . *.*S    o    |
|.       = o B.    . .    |
|       o = .       .  E  |
|          . +         ...  |
|            o.        o.  |
+----[SHA256]-----+

[root@server ~]# ll /root/.ssh/                                 #查看密钥匙目录
总用量 8
-rw-------. 1 root root 2590 10月 26 22:40 id_rsa               #私钥文件
-rw-r--r--. 1 root root  565 10月 26 22:40 id_rsa.pub         #公钥文件

[root@server ~]# ssh-copy-id root@192.168.110.134  #复制该公钥文件到服务端的该目录下
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.110.134 (192.168.110.134)' can't be established.
ED25519 key fingerprint is SHA256:rFpTMCYeFtLw16vHvNCtD7f+AutcLz68mnrlopUfino.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.110.134's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.110.134'"
and check to make sure that only the key(s) you wanted were added.

在client端查看文件

[root@client ~]# ll /root/.ssh/
总用量 4
-rw-------. 1 root root 565 10月 26 22:46 authorized_keys

[root@server ~]# ssh 192.168.110.134                 #使用SSH远程登录client

Register this system with Red Hat Insights: insights-client --register
Create an account or view all your systems at https://red.ht/insights-dashboard
Last login: Thu Oct 26 22:33:54 2023 from 192.168.110.1

[root@client ~]# 

免密登陆成功!!!

client免密访问server

[root@client ~]# ssh-keygen -t rsa                #此处为rsa密钥对
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:mmA+3Ih66Ue7yv1TBdvjbcsmmTKeK7U9rBEuW1NWOiU root@client
The key's randomart image is:
+---[RSA 3072]----+
|                            |
|        .                   |
|         +E o           |
|        . +=             |
|    o   So=o         |
|   =.+ +o+..o       |
|  .o=.=o=+ = .     |
| oo.o.o+++B +    |
|.o+oooo+== +    |
+----[SHA256]-----+

[root@client ~]# ll /root/.ssh/                #查看密钥匙目录
总用量 20
-rw-------. 1 root root  565 10月 26 22:46 authorized_keys  #server免密访问client生成的密钥
-rw-------. 1 root root 2590 10月 26 22:57 id_rsa                      #私钥文件
-rw-r--r--. 1 root root  565 10月 26 22:57 id_rsa.pub                 #公钥文件
-rw-------. 1 root root  843 10月 26 22:57 known_hosts              #记录服务端发来的公钥信息
-rw-r--r--. 1 root root   97 10月 26 22:57 known_hosts.old    #储之前连接过的 SSH 服务器的公钥

[root@client ~]# ssh-copy-id root@192.168.110.136    #复制该公钥文件到服务端的该目录下
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.110.136's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.110.136'"
and check to make sure that only the key(s) you wanted were added.

在server端查看文件

[root@server ~]# ll /root/.ssh/
总用量 20
-rw-------. 1 root root  565 10月 26 23:01 authorized_keys
-rw-------. 1 root root 2590 10月 26 22:40 id_rsa
-rw-r--r--. 1 root root  565 10月 26 22:40 id_rsa.pub
-rw-------. 1 root root  843 10月 26 22:46 known_hosts
-rw-r--r--. 1 root root   97 10月 26 22:46 known_hosts.old

[root@client ~]# ssh 192.168.110.136         #使用SSH远程登录server
Activate the web console with: systemctl enable --now cockpit.socket

Register this system with Red Hat Insights: insights-client --register
Create an account or view all your systems at https://red.ht/insights-dashboard
Last login: Thu Oct 26 22:57:34 2023 from 192.168.110.134

[root@server ~]# 

登陆成功!!!

实验结束实现服务器1server和服务器2client相互免密登录!

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Linux实现SSH免密登录,你可以按照以下步骤进行操作: 1. 生成SSH密钥对:在本地计算机上打开终端,并运行以下命令生成密钥对: ``` ssh-keygen -t rsa ``` 这将生成一个RSA密钥对,包括公钥和私钥。你可以选择使用默认的密钥保存路径和文件名,或者根据需要进行自定义。 2. 将公钥复制到目标服务器:将公钥复制到你希望免密登录的目标服务器上。可以使用以下命令将公钥复制到远程服务器: ``` ssh-copy-id username@remote_host ``` 替换 `username` 为你的目标服务器用户名,`remote_host` 为目标服务器的IP地址或域名。 如果 `ssh-copy-id` 命令不可用,你可以手动复制公钥文件的内容到目标服务器的 `~/.ssh/authorized_keys` 文件中。 3. 配置SSH服务器:确保目标服务器上的SSH服务器已正确配置以允许密钥登录。打开目标服务器上的SSH配置文件 `/etc/ssh/sshd_config`: ``` sudo vi /etc/ssh/sshd_config ``` 确保以下配置项被启用: ``` PubkeyAuthentication yes PasswordAuthentication no ``` 4. 重新启动SSH服务:保存并关闭SSH配置文件后,使用以下命令重新启动SSH服务: ``` sudo service ssh restart ``` 或者,如果你的系统使用 `systemd`,可以使用以下命令: ``` sudo systemctl restart sshd 5. 测试免密登录:现在,你可以尝试使用SSH连接到目标服务器,如果一切设置正确,你应该无需输入密码即可成功登录: ``` ssh username@remote_host ``` 替换 `username` 为你的目标服务器用户名,`remote_host` 为目标服务器的IP地址或域名。 这样,你就成功实现了在Linux上的SSH免密登录。请记得保护好私钥文件,不要泄露给他人,以确保系统的安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值