ssh使用笔记

  在集群管理和配置中有很多命令要在各个节点中发送(特别是Master->Worker),大家都不希望发送每一个命令时都输入一次密码,因此常常先配置实现Master无密码登录到所有的Worker节点。由于大多的Linux发行版本都预装有openssh-client,常用的方法就是使用ssh来实现免密码登录,下面以Ubuntu为例说下ssh的相关配置及注意事项

  网上有关ssh-keygen命令的介绍可找到很多,想了解更多可参考这篇博文(ssh-keygen命令的用法[转]

  在Master的终端中输入:ssh-keygen -t rsa

  会有以下输出:

Generating public/private rsa key pair.
Enter file in which to save the key (/home/liang/.ssh/id_rsa): //直接回车
Created directory '/home/liang/.ssh'.
Enter passphrase (empty for no passphrase): //直接回车
Enter same passphrase again: //直接回车
Your identification has been saved in /home/liang/.ssh/id_rsa.
Your public key has been saved in /home/liang/.ssh/id_rsa.pub.
The key fingerprint is:
c7:59:ac:56:42:73:73:f0:f1:69:f1:6f:45:4f:6c:f2 liang@liang-pc
The key's randomart image is:
+--[ RSA 2048]----+
|          o +.ooo|
|         . + +.=B|
|          . + .*=|
|         . *  . E|
|        S *     o|
|         o     . |
|                 |
|                 |
|                 |
+-----------------+

  此时在用户目录下生成.ssh文件夹(默认是隐藏的,可使用ctrl+h命令来 显示/隐藏 隐藏文件),将.ssh下的id_rsa文件内容copy到Worker节点用户目录下的.ssh/authorized_keys中(~/.ssh/authorized_keys),如果配置完还不能无密码访问,可修改下authorized_keys的权限试下,命令:chmod 600 authorized_keys。


ubuntu中安装使用sshkey产生key以后,使用ssh localhost测试一下,出现错误提示:connect to host localhost port 22:Connection refused错误,造成这个错误的原因是由于ssh-server没有安装或者没有启动。ubuntu中默认只安装openssh-client,可以使用sshkey等命令,让我误以为已经将openssh-server也安装了
1、使用命令ps -e | grep ssh,发现只有一个ssh-agent进程,没有sshd相关进程,说明ssh-server没有启动
2、使用命令/etc/init.d/ssh -start启动server进程,提示ssh不存在,才发现没有安装server
3、使用命令sudo apt-get install openssh-server命令安装,提示:
下列软件包有未满足的依赖关系:
 openssh-server : 依赖: openssh-client (= 1:5.9p1-5ubuntu1.4) 但是 1:6.0p1-3ubuntu1.2 正要被安装
说明ubuntu提供的openssh-client是6.0p1-3ubuntu1.2版本的,而提供的openssh-server是5.9p1-5ubuntu1.4版本的,所以会有以上提示,这点搞得太不好了(后来想想可能是我更换了软件源的事)
4、解决此类的问题的方法是让client和server的版本一致,我为了图方便直接在开源中国镜像站中下载了openssh-server_6.0p1-3ubuntu1.2_i386.deb,双击安装,问题解决。
 

  
一切就绪后可在终端上输入ssh localhost date测试下是不是配置正确
liang@liang-pc:~$ ssh localhost date
The authenticity of host 'localhost (127.0.0.1)' can't be established.
ECDSA key fingerprint is 8d:f7:ae:90:3c:40:91:86:b3:c8:5e:75:7b:53:b1:b2.
Are you sure you want to continue connecting (yes/no)? //输入yes,若直接回车会Host key verification failed.
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
2015年 01月 05日 星期一 14:55:29 CST
liang@liang-pc:~$ ssh localhost date//再次输入
2015年 01月 05日 星期一 14:55:33 CST
ssh-keygen -t rsa -C “any comment can be here”
当你创建ssh的时候

-t = The type of the key to generate
密钥的类型
-C = comment to identify the key
用于识别这个密钥的注释
So the Comment is for you only and you can put anything inside.
Many sites and software are using this comment as the key name.
所以这个注释你可以输入任何内容,很多网站和软件用这个注释作为密钥的名字

原文链接:https://blog.csdn.net/u011118321/java/article/details/78516303

原文地址:https://www.cnblogs.com/halu126/p/4203790.html