红联Linux门户
Linux帮助

Ubuntu设置SSH免密登录(不同于CentOS)

发布时间:2017-03-04 14:49:55来源:linux网站作者:程裕强
1.遇到的坑
之前一直在CentOS系统进行Hadoop开发,SSH免密登录配置过N次,今天在Ubuntu平台下设置免密登录遇到了坑:
kylin@uk0:~$ ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa
Generating public/private dsa key pair.
Created directory '/home/kylin/.ssh'.
Your identification has been saved in /home/kylin/.ssh/id_dsa.
Your public key has been saved in /home/kylin/.ssh/id_dsa.pub.
The key fingerprint is:
SHA256:Kp2T562S8CP1Ui7LJMDlFAMBqujJ8HyrhvsAfWHu8X8 kylin@uk0
The key's randomart image is:
+---[DSA 1024]----+
|..ooo            |
|.    o           |
|.   =            |
|oo * .           |
|= + =   S        |
|=o.+.+.+.        |
|.=o =+O=.        |
|.....*B=o.E      |
|.+o...o*+o.      |
+----[SHA256]-----+
kylin@uk0:~$ cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys
kylin@uk0:~$ chmod 0600 ~/.ssh/authorized_keys
免密登录本机时,竟然需要输入密码!
kylin@uk0:~$ ssh uk0
The authenticity of host 'uk0 (192.168.1.160)' can't be established.
ECDSA key fingerprint is SHA256:dOKnH/ES8JcWzGkdRwRzYGzDkBE8y7cJpRx/DNHhGVc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'uk0,192.168.1.160' (ECDSA) to the list of known hosts.
kylin@uk0's password: 
Welcome to Ubuntu 16.10 (GNU/Linux 4.8.0-22-generic x86_64)
* Documentation:  https://help.ubuntu.com
* Management:     https://landscape.canonical.com
* Support:        https://ubuntu.com/advantage
279 个可升级软件包。
107 个安全更新。
Last login: Fri Mar  3 15:20:29 2017 from 192.168.1.81
kylin@uk0:~$
 
2.解决办法
2.1 先删除之前的.ssh
kylin@uk0:~$ rm -rf .ssh
2.2 生成免密
与CentOS不同,需要使用ssh-keygen -t rsa命令生成,一直按Enter,按到最后。
kylin@uk0:~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/kylin/.ssh/id_rsa): 
Created directory '/home/kylin/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/kylin/.ssh/id_rsa.
Your public key has been saved in /home/kylin/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Onr5G6x8pcsmksQNuMECb2oOu7lKoOzGPh0+x6y0Pj4 kylin@uk0
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|.                |
|.o .             |
|. * .            |
|.+ + o  S        |
|*...o .o  .      |
|*+oo+.o.oo       |
|+*.E+*+++.       |
|O=+=Boo==o       |
+----[SHA256]-----+
kylin@uk0:~$ cat .ssh/id_rsa.pub >> .ssh/authorized_keys
2.3 免密登录本机
kylin@uk0:~$ ssh uk0
The authenticity of host 'uk0 (192.168.1.160)' can't be established.
ECDSA key fingerprint is SHA256:dOKnH/ES8JcWzGkdRwRzYGzDkBE8y7cJpRx/DNHhGVc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'uk0,192.168.1.160' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 16.10 (GNU/Linux 4.8.0-22-generic x86_64)
Last login: Fri Mar  3 16:11:16 2017 from 192.168.1.160
kylin@uk0:~$
 
3.集群免密登录
3.1 第2个节点生成免密
kylin@uk1:~$ rm -rf .ssh
kylin@uk1:~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/kylin/.ssh/id_rsa): 
Created directory '/home/kylin/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/kylin/.ssh/id_rsa.
Your public key has been saved in /home/kylin/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:pGAXPlmDGitJNPmUGXcS2ZZrQw581nTH7Y7zzUTU1Fs kylin@uk1
The key's randomart image is:
+---[RSA 2048]----+
| .o..===++. ...o+|
|  o.=o=*B... .. E|
| . +o+=B..     oo|
|  o.+o +=      .o|
|   .  ..S.     + |
|              o o|
|               =.|
|                +|
|                 |
+----[SHA256]-----+
kylin@uk1:~$ cat .ssh/id_rsa.pub >> .ssh/authorized_keys
kylin@uk1:~$ ssh uk1
The authenticity of host 'uk1 (192.168.1.161)' can't be established.
ECDSA key fingerprint is SHA256:dOKnH/ES8JcWzGkdRwRzYGzDkBE8y7cJpRx/DNHhGVc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'uk1,192.168.1.161' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 16.10 (GNU/Linux 4.8.0-22-generic x86_64)
Last login: Fri Mar  3 16:23:19 2017 from 192.168.1.161
kylin@uk1:~$
3.2 第3个节点生成免密
kylin@uk2:~$ rm -rf .ssh
kylin@uk2:~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/kylin/.ssh/id_rsa): 
Created directory '/home/kylin/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/kylin/.ssh/id_rsa.
Your public key has been saved in /home/kylin/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:/chD0D2CNj5iryn7Jue5lLxXzq3RAjwtG7Jrasz0KYk kylin@uk2
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|         o .     |
|        = o o    |
|       + = . .   |
|      + S +      |
|    .o * X.+     |
|   = o=.o+*.o    |
|  E O.**. o+.    |
|   .o%Xo  ..     |
+----[SHA256]-----+
kylin@uk2:~$ cat .ssh/id_rsa.pub >> .ssh/authorized_keys
kylin@uk2:~$ ssh uk2
The authenticity of host 'uk2 (192.168.1.162)' can't be established.
ECDSA key fingerprint is SHA256:dOKnH/ES8JcWzGkdRwRzYGzDkBE8y7cJpRx/DNHhGVc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'uk2,192.168.1.162' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 16.10 (GNU/Linux 4.8.0-22-generic x86_64)
* Documentation:  https://help.ubuntu.com
* Management:     https://landscape.canonical.com
* Support:        https://ubuntu.com/advantage
Last login: Fri Mar  3 16:03:44 2017 from 192.168.1.160
kylin@uk2:~$
3.3 分发公钥
kylin@uk1:~$ cat ~/.ssh/authorized_keys
Ubuntu设置SSH免密登录(不同于CentOS)
kylin@uk2:~$ cat ~/.ssh/authorized_keys
Ubuntu设置SSH免密登录(不同于CentOS)
合并公钥
kylin@uk0:~$ vi ~/.ssh/authorized_keys
Ubuntu设置SSH免密登录(不同于CentOS)
分发公钥
kylin@uk0:~$ scp .ssh/authorized_keys kylin@192.168.1.161:/home/kylin/.ssh
The authenticity of host '192.168.1.161 (192.168.1.161)' can't be established.
ECDSA key fingerprint is SHA256:dOKnH/ES8JcWzGkdRwRzYGzDkBE8y7cJpRx/DNHhGVc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.161' (ECDSA) to the list of known hosts.
kylin@192.168.1.161's password:
authorized_keys
100% 1173   692.4KB/s   00:00
kylin@uk0:~$ scp .ssh/authorized_keys kylin@192.168.1.162:/home/kylin/.ssh
The authenticity of host '192.168.1.162 (192.168.1.162)' can't be established.
ECDSA key fingerprint is SHA256:dOKnH/ES8JcWzGkdRwRzYGzDkBE8y7cJpRx/DNHhGVc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.162' (ECDSA) to the list of known hosts.
kylin@192.168.1.162's password: 
authorized_keys
100% 1173     1.1MB/s   00:00
kylin@uk0:~$
3.4 集群免密登录
kylin@uk0:~$ ssh uk1
The authenticity of host 'uk1 (192.168.1.161)' can't be established.
ECDSA key fingerprint is SHA256:dOKnH/ES8JcWzGkdRwRzYGzDkBE8y7cJpRx/DNHhGVc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'uk1' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 16.10 (GNU/Linux 4.8.0-22-generic x86_64)
* Documentation:  https://help.ubuntu.com
* Management:     https://landscape.canonical.com
* Support:        https://ubuntu.com/advantage
279 个可升级软件包。
107 个安全更新。
Last login: Fri Mar  3 16:24:21 2017 from 192.168.1.161
kylin@uk1:~$ 
kylin@uk1:~$ ssh uk2
The authenticity of host 'uk2 (192.168.1.162)' can't be established.
ECDSA key fingerprint is SHA256:dOKnH/ES8JcWzGkdRwRzYGzDkBE8y7cJpRx/DNHhGVc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'uk2,192.168.1.162' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 16.10 (GNU/Linux 4.8.0-22-generic x86_64)
* Documentation:  https://help.ubuntu.com
* Management:     https://landscape.canonical.com
* Support:        https://ubuntu.com/advantage
279 个可升级软件包。
107 个安全更新。
Last login: Fri Mar  3 16:25:05 2017 from 192.168.1.162
 
本文永久更新地址:http://www.linuxdiyf.com/linux/28882.html