实现方式:公私钥对
这里设服务端IP地址为:192.164.124.3
1、使用ssh-keygen命令在当前用户根目录下的.ssh目中中生成公钥~/.ssh/id_rsa.pub
及私钥~/.ssh/is_rsa
也可以使用ssh-keygen -t dsa
或者直接用ssh-keygen,另外私钥的权限应当为600,公钥为644.【客户端操作】
[root@hsc ~]# ssh-keygen -t rsa
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:TI4EMq02oOpRjsrhD4yJ7pea4Idf8rtxrLSrvGIplhc root@hsc.example.com
The key's randomart image is:
+---[RSA 2048]----+
| o.. |
|. o.. |
|.. . . . |
|. +. . = |
|..+. . S |
|== E . |
|Xo*.o+ o |
|*@+== = |
|=*O*oB+ |
+----[SHA256]-----+
2、将公钥拷贝到目标服务端系统【也在客户端操作就行】ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.164.124.3
,这里也可以使用文件拷贝的指令scp,这个指令输入后需要密码scp ~/.ssh/id_rsa.pub root@192.164.124.3:~/.ssh/authorized_keys
。
[root@hsc ~]# ssh-copy-id root@192.164.124.3
/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.164.124.3's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.164.124.3'"
and check to make sure that only the key(s) you wanted were added.
3、免密登陆服务端功能实现
[root@hsc ~]# ssh 192.164.124.3
Last login: Wed Aug 7 00:28:14 2019 from 192-164-124-112.hdsl.highway.telekom.at
[root@villainc ~]#