centos7下配置ssh免密登录


防火墙配置: https://blog.csdn.net/zhaosongbin/article/details/87918343

安装所需要的两台服务器:

服务器A: 192.168.1.1
服务器B: 192.168.1.2

ssh免密登陆原理

方案一:

  1. 在主机A生产密钥对: ssh-keygen -t rsa, 会在.ssh目录下产生密钥文件
  2. 拷贝主机A的公钥到主机B: scp id_rsa.pub
  3. 将主机A的公钥加到主机B的授权列表.ssh/authorized_keys: cat id_rsa.pub >> authorized_keys
  4. 授权列表authorized_keys的权限必须是600,chmod 600 authorized_keys

方案二:
在虚拟机中执行命令: ssh-keygen -t rsa
执行完这个命令后,会生成两个文件id_rsa(私钥)、id_rsa.pub(公钥)
将公钥拷贝到要免登陆的机器上: ssh-copy-id localhost

在这里我们讲下方案二的详细配置

详细配置

我当前配置的是服务器B的root用户针对服务器A的访问进行免密
使用下方的命令来生成公钥,私钥

[root@localhost ~]# ssh-keygen -t 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:of/2iilRoRTDFcCiPmkzuwAj7b3K9IM2RiniMCLPcys root@
The key's randomart image is:
+---[RSA 2048]----+
|     o++o.       |
|    . +..        |
|   . o ...       |
| ..   ....       |
|+..o  ..S        |
|XoX.  ..         |
|BBo*.  ..        |
| =E.o..  +.      |
| o+B+o .o.oo.    |
+----[SHA256]-----+

[root@localhost ~]# 

我们在命令行打印日志中可以清晰的看到生成的文件在==/root/.ssh==文件夹内部
[root@localhost]# cd /root/.ssh/
[root@localhost .ssh]# ll
total 8
-rw-------. 1 root root 1679 Apr 12 17:13 id_rsa		###私钥
-rw-r--r--. 1 root root  397 Apr 12 17:13 id_rsa.pub	###公钥
[root@localhost .ssh]# 

传输服务器A的公钥id_rsa.pub到服务器B

[root@localhost .ssh]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.1.2
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.1.2(192.168.1.2)' can't be established.
ECDSA key fingerprint is SHA256:oQzbii6infcQqMZB9JKC4Cu8JpABvaRlx5NIkpAesA8.
ECDSA key fingerprint is MD5:96:0f:7a:a3:76:85:b7:07:2d:f2:ed:03:e0:f0:52:87.
Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or 'no': 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.1.2's password: 
Permission denied, please try again.
root@192.168.1.2's password: 
###输入用户密码
Number of key(s) added: 1
###输入用户密码
Now try logging into the machine, with:   "ssh '192.168.1.2'"
and check to make sure that only the key(s) you wanted were added.

[root@localhost .ssh]

现在让我们来测试下,使用scp命令传输文件,在服务器B的/home目录下就可以找到test.txt
[root@localhost  .ssh]# cd /home/
[root@localhost  home]# ll
total 1
-rw-r--r--. 1 root root 80157 Apr 12 17:50 test.txt

[root@localhost  home]# scp test.txt root@192.168.1.2:/home/
test.txt     										100%   78KB  23.7MB/s   00:00    

[root@localhost  home]# 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 鲸 设计师:meimeiellie 返回首页