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

一、首先准备两台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相互免密登录!

### 配置两台 Windows 计算机在局域网中实现 SSH 免密登录 为了实现在局域网中的两台 Windows 计算机之间通过 SSH 进行免密登录,可以按照以下方式完成配置: #### 1. 安装并启用 OpenSSH 组件 确保两台计算机均已安装 **OpenSSH Client** 和 **OpenSSH Server**。可以通过“设置” -> “应用” -> “可选功能”来添加这些组件。 - 对于服务器端(目标机器),需要特别关注 OpenSSH 的服务状态。 - 启动并配置 OpenSSH 服务: - 按下 `Win + R` 键组合,输入 `services.msc` 打开服务管理器。 - 查找以下两项服务并将其启动类型设为手动[^2]: - **OpenSSH Authentication Agent** - **OpenSSH SSH Server** ```powershell # 使用 PowerShell 命令也可以快速修改服务的状态和启动类型 Set-Service -Name ssh-agent -StartupType Manual Start-Service ssh-agent Set-Service -Name sshd -StartupType Manual Start-Service sshd ``` --- #### 2. 创建 SSH 密钥对 在客户端设备上生成一对用于身份验证的公私钥文件。打开 PowerShell 并运行以下命令: ```bash ssh-keygen -t rsa -b 4096 -C "your_email@example.com" ``` 此操作会提示保存位置,默认路径通常为 `%USERPROFILE%\.ssh\id_rsa` 及其对应的 `.pub` 文件。如果未指定密码,则该密钥不会受到额外保护。 注意:这里的 `-C` 参数只是作为标签标记,并不影响实际认证过程[^3]。 --- #### 3. 将公钥传输至远程主机 将本地用户的公钥复制到另一台 Windows 主机上的授权密钥列表里去。具体做法如下所示: 假设源 PC 上有用户 Alice 而目标系统存在 Bob 用户账户的话;那么就需要把前者产生的 id_rsa.pub 内容追加进后者家目录下的 .ssh/authorized_keys 文本之中[^3]。 可以直接利用下面这条指令自动完成推送工作: ```bash type %USERPROFILE%\.ssh\id_rsa.pub | ssh bob@target_machine "mkdir -p ~/.ssh && type >> ~/.ssh/authorized_keys" ``` 或者更推荐的方式是在 Linux 或类 Unix 环境中有专门工具支持的情况下采用标准形式调用: ```bash ssh-copy-id bob@target_machine ``` > 如果当前环境不支持直接使用 `ssh-copy-id` ,则需手工编辑目标系统的 authorized_keys 文件并将内容粘贴进去。 --- #### 4. 测试无密码访问 最后一步就是检验整个流程是否成功生效了。尝试从发起方再次连接过去看能否无需再输任何口令便能顺利进入对方 shell 中: ```bash ssh bob@target_machine ``` 如果没有被询问密码就表示已经实现了预期效果——即完成了基于密钥的身份校验机制构建[^2]。 --- ### 注意事项 - 确认防火墙允许 TCP/IP 协议第 22 号端口号通信。 - 若遇到权限错误,请检查目标主机上的文件夹以及文件是否有正确的读写属性分配给相应的使用者账号。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值