Realize password-free login between multiple Linux hosts

In our intranet environment, we have to switch and log in to many different machines every day, and we have to manually enter the password every time; the long and hard-to-remember password is really a headache, so let’s explain how to avoid passwords between different hosts login.

Prepare two machines

A:192.168.56.18
B:192.168.56.6

Generate keys on A and B machines

[root@centos7_code1 ~]# ssh-keygen -t rsa                       #生成ssh私钥和公钥文件
	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:Se8XcMGievTvZeHcsGys+qWiQenSrwax61ewxi4lAEU root@centos7_code1
	The key's randomart image is:
	+---[RSA 2048]----+
	|   oE      ..    |
	|  .       . ..   |
	|   .    .....    |
	|    . ..++ o     |
	|     . *S+. . o  |
	|      =+*.o  * = |
	|      .O+.... @ .|
	|      o.+o...B   |
	|     ..+ooo==    |
	+----[SHA256]-----+
[root@centos7_code1 ~]# ls /root/.ssh/                          #可以看到私钥和公钥文件
	id_rsa  id_rsa.pub

Machine A password-free login to machine B

[root@centos7_code2 .ssh]# touch authorized_keys
然后在A机器上复制自己的公钥文件 id_rsa.pub到B机器的authorized_keys文件中
[root@centos7_code1 .ssh]# scp /root/.ssh/id_rsa.pub [email protected]:/root/.ssh/authorized_keys

再来到B机器上查看拷贝过来的文件内容
[root@centos7_code2 .ssh]# vim /root/.ssh/authorized_keys
	ssh-rsa 		AAAAB3NzaC1yc2EAAAADAQABAAABAQCw7nix6UTAZN5R2N3Nqi+eDqcdDj2Dgcnk1nVXgNQNmygdMtkA3WOrC/hvHqnAdf9LCxqeddyPPofe8bAY/gHnfTKBUuW/2vYNC3Xn6FQrttkCuUwsvPoFkdqmqmk21cU/RGhKzHO1Bu65IxDb9jr38h5eIwdYdBVjJNdIAENM4dafNN/ZmW9PCSXTdCCshqHtX/nDg4h4AP1PqvnHOB6miSCGXevZWbhrdFzsEj/bTo6mtqNc1wZ6XkKZ2CWi384KCxnMzipcntHUzEXiu0kDMc6pA+CLK7wdKm9pWpfRl+VBqwfdmmUgGffHW3FELu8WdiGX9G/Me7KtXBgnvckr root@centos7_code1

Notice:

If you need multiple machines to log in to this machine without password, just add the public key files of other machines to the bottom of the file

Test machine A password-free login machine B

[root@centos7_code1 .ssh]# ssh [email protected]
	Last login: Sun Jul 22 21:57:14 2018 from 192.168.56.2
[root@centos7_code2 ~]#

If machine A also needs to log in to other hosts without password, the same operation can be done in reverse

Guess you like

Origin blog.csdn.net/wkl1007/article/details/103511693