两台linux主机进行ssh免密登录

目录

        1、确认ssh服务开启

2、生成非对称密钥

3、将生成的公钥发给需要免密登录的设备

4、直接进行ssh连接

5、在第二台设备进行相同配置


1、确认ssh服务开启

[root@localhost ~]# systemctl status sshd
● sshd.service - OpenSSH server daemon
     Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor prese>
     Active: active (running) since Tue 2024-01-16 02:24:58 EST; 8min ago
 

2、生成非对称密钥

[root@localhost ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 123
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in 123
Your public key has been saved in 123.pub
The key fingerprint is:
SHA256:ubPXFPFVTiLsFamnt9lru0Gp8himU9iWGacvw4TmG2o root@localhost.localdomain
The key's randomart image is:
+---[RSA 3072]----+
|           .. oo+|
|            o.o+.|
|           . = ..|
|         . .+.o .|
|        S + *+ o |
|         = Oo +  |
|        =.=*.o = |
|       E.==+*.o.o|
|      ...+o.o..++|
+----[SHA256]-----+


 

3、将生成的公钥发给需要免密登录的设备

[root@localhost ~]# ssh-copy-id 192.168.87.134
/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.87.134's password:

Number of key(s) added: 1

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

4、直接进行ssh连接

[root@localhost ~]# ssh 192.168.87.134
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: Tue Jan 16 10:30:14 2024 from 192.168.87.1
[root@localhost ~]# ll .ssh/
总用量 4
-rw-------. 1 root root 580  1月 16 10:38 authorized_keys

5、在第二台设备进行相同配置

 

  • 18
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux主机之间实现SSH登录的过程如下: 1. 在本地主机上生成SSH钥对(公钥和私钥),如果已经存在钥对,请跳过此步骤。使用以下命令生成钥对: ```shell ssh-keygen -t rsa ``` 按照提示,选择钥存储位置和设置码(可选)。默认情况下,公钥和私钥将存储在`~/.ssh`目录中。 2. 将本地主机的公钥复制到远程主机上。使用以下命令将公钥复制到远程主机: ```shell ssh-copy-id user@remote_host ``` 将`user`替换为远程主机上的用户名,`remote_host`替换为远程主机的IP地址或域名。执行此命令后,您需要输入远程主机码。 如果`ssh-copy-id`命令不可用,您可以手动将公钥内容复制到远程主机的`~/.ssh/authorized_keys`文件中。使用以下命令将公钥内容复制到远程主机: ```shell cat ~/.ssh/id_rsa.pub | ssh user@remote_host "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys" ``` 同样,将`user`替换为远程主机上的用户名,`remote_host`替换为远程主机的IP地址或域名。 3. 验证登录是否成功。使用以下命令连接到远程主机: ```shell ssh user@remote_host ``` 这次连接不应再要求输入码,而是直接登录到远程主机。 完成上述步骤后,您应该能够在本地主机上无需码连接到远程主机。请注意,这需要在本地和远程主机之间具有网络连通性,并且远程主机必须允许使用SSH进行身份验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值