SSH通信和免密码登陆设置

1.SSH原理
SSH 为 Secure Shell 的缩写,安全Shell网络协议,用于计算机之间的加密登录,早期的计算机之间采用明文通信,通信信号被截获以后,内容即被截获一方掌握。1995年,芬兰学者Tatu Ylonen设计了SSH协议对登陆信息进行加密,称为互联网安全的标准解决方案。
SSH作为一种协议,有多重实现方案。目前Ubuntu的desktop系统会默认安装OpenSSH的client端,在Windows系统中,可通过开源软件PuTTY实现SSH通信。

2.SSH安装
参考我的BLOG:http://blog.csdn.net/ezhchai/article/details/52972064
在安装好Ubuntu的Desktop系统后,执行dpkg -l | grep ssh 进行查询,
可以看到系统默认只安装了openssh-client,没有server端。建议安装Server端,尤其是接受其他Client端登陆的设备,安装命令为:

sudo apt-get install openssh-server

3.SSH登陆
SSH主要用于远程登录。假定你要以用户名user,登录远程主机host,只要一条命令即可。可通过-p参数设定端口号(22是默认端口)。
ssh -p 22 user@host
在没有设置免密码登陆时,需要通过密码登陆。
如果你是第一次登录对方主机,系统会出现下面的提示:

$ ssh user@host
The authenticity of host 'host (12.18.429.21)' can't be established.
RSA key fingerprint is 98:2e:d7:e0:de:9f:ac:67:28:c2:42:2d:37:16:58:4d.
Are you sure you want to continue connecting (yes/no)?

提示无法确认host主机的真实性,只知道它的公钥指纹,还想继续连接吗?
所谓”公钥指纹”,是指对公钥(RSA算法公钥长度1024位)进行MD5计算,转换成128位的指纹,即
98:2e:d7:e0:de:9f:ac:67:28:c2:42:2d:37:16:58:4d,容易进行比较。由于用户无法知道远程主机的公钥指纹,需要主机主动提供,才能进行必比对。
经过风险衡量以后,用户决定接受这个远程主机的公钥,则需输入yes。

Are you sure you want to continue connecting (yes/no)? yes

系统会出现一句提示,表示host主机已经得到认可。

Warning: Permanently added 'host,12.18.429.21' (RSA) to the list of known hosts.

提示输入密码。

Password: (enter password)

如果密码正确,就可以登录了。
当远程主机的公钥被接受以后,会在文件$HOME/.ssh/known_hosts中保存。下次再连接这台主机,系统认出公钥已经保存在本地,跳过警告部分,直接提示输入密码。
每个SSH用户都有自己的known_hosts文件,系统在/etc/ssh/ssh_known_hosts,保存一些对所有用户都可信赖的远程主机公钥。

4.创建SSH key
如果经常输入密码会显得比较麻烦,可以通过让主机记录公钥来实现免密码登陆。这里首先要创建公钥。
命令为:ssh-keygen -t [rsa|dsa] –C [Comments]
-t = The type of the key to generate
密钥的类型,加密方式选 rsa|dsa均可以,默认dsa
-C = comment to identify the key
用于识别这个密钥的注释
命令实例为:

$ ssh-keygen -t rsa  
Generating public/private rsa key pair.  
Enter file in which to save the key (/home/ezhchai/.ssh/id_rsa):   
Created directory '/home/ezhchai/.ssh'.  
Enter passphrase (empty for no passphrase):   
Enter same passphrase again:   
Your identification has been saved in /home/ezhchai/.ssh/id_rsa.  
Your public key has been saved in /home/ezhchai/.ssh/id_rsa.pub.  
The key fingerprint is:  
39:f2:fc:70:ef:e9:bd:05:40:6e:64:b0:99:56:6e:01 ezhchai@Chai  
The key's randomart image is:  
+--[ RSA 2048]----+  
|          Eo*    |  
|           @ .   |  
|          = *    |  
|         o o .   |  
|      . S     .  |  
|       + .     . |  
|        + .     .|  
|         + . o . |  
|          .o= o. |  
+-----------------+  

进入“.ssh”会生成以下几个文件
authorized_keys:存放远程免密登录公钥,通过该文件记录多台机器的公钥;
  id_rsa:生成的私钥文件
  id_rsa.pub:生成的公钥文件
  know_hosts:已知的主机公钥清单
如果希望ssh公钥生效需满足至少下面两个条件:
ssh目录的权限必须是700
ssh/authorized_keys文件权限必须是600

5.拷贝公钥到远程主机
命令:ssh-copy-id -i ~/.ssh/id_rsa.pub <romte_ip>
-i参数可采用默认项。

$ ssh-copy-id git@192.168.10.113  
The authenticity of host '192.168.10.113 (192.168.10.113)' can't be established.  
RSA key fingerprint is f0:1c:05:40:d3:71:31:61:b6:ad:7c:c2:f0:85:3c:cf.  
Are you sure you want to continue connecting (yes/no)? yes  
Warning: Permanently added '10.124.84.20' (RSA) to the list of known hosts.  
git@192.168.10.113's password:   
Now try logging into the machine, with "ssh ' git@192.168.10.113'", and check in:  

  .ssh/authorized_keys  

to make sure we haven't added extra keys that you weren't expecting.

这样,再通过ssh命令,即可免密码实现主机登陆了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值