下面简单的做下实验:
一:准备2台服务器:
1 192.168.10.101 2 192.168.10.102
二:关键字解释
ssh-keygen : 产生公钥与私钥对
ssh-copy-id :将本机的公钥复制到远程机器的authorized_keys文件中
三:开始实验
第一步: 在192.168.10.101上使用ssh-keygen产生公钥私钥对
1 [root@controlm ~]# ssh-keygen 2 Generating public/private rsa key pair. 3 Enter file in which to save the key (/root/.ssh/id_rsa): 4 Enter passphrase (empty for no passphrase): 5 Enter same passphrase again: 6 Your identification has been saved in /root/.ssh/id_rsa. 7 Your public key has been saved in /root/.ssh/id_rsa.pub. 8 The key fingerprint is: 9 a4:ae:78:72:cc:94:34:2f:33:3f:a5:e5:20:51:d2:6c root@controlm 10 The key's randomart image is: 11 +--[ RSA 2048]----+ 12 | o | 13 | . E | 14 | + . | 15 | + o | 16 | . =. S | 17 | B.o o | 18 | + *.* | 19 | ..=.+ . | 20 | .+. . | 21 +-----------------+
第二步:用ssh-copy-id将公钥复制到远程机器1922.168.10.102中(注意::ssh-copy-id 是将key写到远程机器的 ~/ .ssh/authorized_key.文件中)
1 [root@controlm ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.10.102 2 /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed 3 /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 4 root@192.168.10.102's password: 这里输入你的登录密码 5 6 Number of key(s) added: 1 7 8 Now try logging into the machine, with: "ssh '192.168.10.102'" 9 and check to make sure that only the key(s) you wanted were added.
第三部: 登录到远程机器192.168.10.102不用输入密码
1 [root@controlm ~]# ssh 192.168.10.102 2 Last login: Wed Feb 27 11:32:23 2019 from 192.168.10.101 3 [root@docker ~]#