1.使用 ssh-keygen 生成公钥和私钥
[root@localhost ~]# ssh-keygen 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:mgyB4mVBPdMaPPbYZbXDJzycMZ6nOmTT9RsetKFtDOY root@localhost.localdomain The key's randomart image is: +---[RSA 2048]----+ | .oo . .+ | | ..O . o= * | |. .oo X o % * o | |..o + o . @ B o| | . . S+ o E O | | o oo o o +| | + o o | | . | | | +----[SHA256]-----+
2.查看/root/.ssh/目录,发现多了两个文件
[root@localhost ~]# ll .ssh/ total 12 -rw------- 1 root root 1679 Aug 1 00:46 id_rsa -rw-r--r-- 1 root root 408 Aug 1 00:46 id_rsa.pub
3.使用 ssh-copy-id 命令发送公钥至指定主机
[root@localhost ~]# ssh-copy-id -i /root/.ssh/ id_rsa id_rsa.pub known_hosts [root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub 192.168.249.132 /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.249.132's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '192.168.249.132'" and check to make sure that only the key(s) you wanted were added.
4.现在尝试 ssh -l IP 进行免密登陆
[root@localhost ~]# ssh -l root 192.168.249.132 Last login: Thu Aug 1 00:35:11 2019 from 192.168.249.135
大功告成。