参考文章
http://www.cnblogs.com/Impulse/p/3974480.html #公钥私钥解释
http://www.cnblogs.com/hukey/p/6248468.html #SSH登录方式
http://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html
假设A服务器需要登陆到B服务器
- 在A服务器执行ssh-keygen命令生成公钥和私钥
- 在A 服务器执行ssh-copy-id user@host命令把A的公钥复制到B服务器的$HOME/.ssh/authorized_keys文件中
在A服务器生成公钥和私钥
~/.ssh # ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
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:fXQ6jYk6D7FihPEjRdIQ1bD9bXJ9daH8nLyxHzrkyII root@d9b873171c1a
The key's randomart image is:
+---[RSA 2048]----+
| +=+o . |
| o.o. . . .|
| . o . .o. o|
| = o + B+ +|
| o + S = X o*.|
| o . + = o .+|
| o * . + + |
| . E = o o. o|
| o .. .|
+----[SHA256]-----+
~/.ssh #
~/.ssh # ls -la
total 16
drwx------ 1 root root 38 Sep 26 10:57 .
drwx------ 1 root root 124 Oct 12 14:22 ..
-rw------- 4 root root 399 Sep 26 10:42 authorized_keys
-rw------- 1 root root 1679 Oct 12 14:27 id_rsa
-rw-r--r-- 1 root root 399 Oct 12 14:27 id_rsa.pub
-rw-r--r-- 4 root root 348 Sep 26 10:42 known_hosts
~/.ssh #
把A服务器的公钥给B服务器
~/.ssh # ssh-copy-id dataagg@192.168.100.196
/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
expr: warning: '^ERROR: ': using '^' as the first character
of a basic regular expression is not portable; it is ignored
/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
dataagg@192.168.100.196's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'dataagg@192.168.100.196'"
and check to make sure that only the key(s) you wanted were added.
~/.ssh #
# B服务器dataagg账户下面的认证文件
[root@localhost .ssh]# ls -la
总用量 4
drwx------. 2 dataagg dataagg 29 10月 12 14:30 .
drwx------. 3 dataagg dataagg 74 10月 12 14:30 ..
-rw-------. 1 dataagg dataagg 399 10月 12 14:30 authorized_keys
[root@localhost .ssh]#
验证是否能免密登陆
~/.ssh # ssh dataagg@192.168.100.196
[dataagg@localhost ~]$ #登陆成功