Chinaunix首页 | 论坛 | 博客
  • 博客访问: 50446
  • 博文数量: 7
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 221
  • 用 户 组: 普通用户
  • 注册时间: 2013-03-25 12:31
文章分类
文章存档

2014年(1)

2013年(6)

我的朋友

分类: LINUX

2013-07-30 20:26:20


远程Shell应用程序
允许用户在远程机器上执行任意命令
让标准输出在本地
早期明文远程协议:telnet

SSH(Secure Shell,安全的命令解释器)
为客户机提供安全的Shell环境,用于远程管理
默认端口:TCP 22

SSH基于公钥加密(非对称加密)技术
数据加密传输
客户端和服务器的身份验证

公钥(Public Key)和私钥(Private Key)
公钥和私钥是成对生成的,这两个密钥互不相同,两个密钥可以互相加密和解密 
不能根据一个密钥而推算出另外一个密钥
公钥对外公开,私钥只有私钥的持有人才知道 
私钥应该由密钥的持有人妥善保管 
根据实现的功能不同,可以分为数据加密和数字签名

数据加密
发送方使用接收方的公钥加密数据
接收方使用自己的私钥解密数据

数据加密能保证所发送数据的机密性


数字签名

发送方
对原始数据执行HASH算法得到摘要值
发送方用自己私钥加密摘要值
将加密的摘要值与原始数据发送给接收方

接收方
用发送方公钥解密摘要值;同时对收到的原始数据同样执行HASH产生另一摘要值
将解密的摘要值与产生的摘要值对比




电子邮件数字签名加密过程

在这块使用对方的公钥加密 使用自己的密钥解压数据
 ||  ||





OpenSSH
官方站点:
主要软件包:openssh-server、openssh-clients
服务名:sshd
服务端主程序:/usr/sbin/sshd
客户端主程序:/usr/bin/ssh
服务端配置文件:/etc/ssh/sshd_config
客户端配置文件:/etc/ssh/ssh_config


/etc/ssh/sshd_config
重启sshd服务生效



SSH登录使用的用户名
服务器中的本地系统用户的帐号名

SSH登录的用户验证方式
密码验证:使用服务器中系统帐号对应的密码
密钥对验证:使用客户机中生成的公钥、私钥



SSH客户端文件通常无需修改


使用ssh命令远程登录
方式1: ssh 用户名@服务器地址
方式2: ssh -l 用户名 服务器地址(较少用)
方式3: ssh 服务器地址

客户机使用ssh第一次连接服务器时
将服务器上sshd守护进程的公钥复制到本地
存放到本地~/.ssh/known_hosts文件中
每行存放一台服务器的公钥
用来验证服务器的身份



当远程连接出现这个问题时,应该表示第一次连接,从10.0.0.149下载公钥文件,远程连接时,会从服务器端用私钥解开客户机加密的文件,第二次连接时,则不会出现
[root@emily Desktop]# ssh 10.0.0.149
The authenticity of host '10.0.0.149 (10.0.0.149)' can't be established.
RSA key fingerprint is 60:7f:49:ee:e9:2d:37:a5:ab:0f:d5:3b:cc:f6:09:66.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.149' (RSA) to the list of known hosts.
[root@vm .ssh]# ssh 10.0.0.149
The authenticity of host '10.0.0.149 (10.0.0.149)' can't be established.
RSA key fingerprint is 5a:8e:9a:33:a7:c8:20:87:a3:b1:3e:f7:01:3d:4b:fd.
Are you sure you want to continue connecting (yes/no)? ^C
1.方法1,修改配置文件ssh_config
[root@vm .ssh]# vim /etc/ssh/ssh_config 
35 # StrictHostKeyChecking no---》StrictHostKeyChecking yes
[root@vm .ssh]# ssh 10.0.0.149 重新连接,不需要输yes(前提它的~/sshd/known_hosts没有该ip地址的公钥)
Warning: Permanently added '10.0.0.149' (RSA) to the list of known hosts.
35 # StrictHostKeyChecking no

第二种方式,连接时,直接输入-o StrictHostKeyChecking=no
[root@vm ~]# ssh -o StrictHostKeyChecking=no 10.0.1.63
Warning: Permanently added '10.0.1.63' (RSA) to the list of known hosts.
root@10.0.1.63's password: 

[root@emily ~]# cat .ssh/known_hosts在客户端查看公钥
10.0.0.149 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAmTs1Y8uf2AQF7ivcyTJlMimIw5CoYnkAMEkt0lhZVEG8htGFF0D6y7v17oZ0/EQAaoFndV3dg+r/8y75DNvlFCn5IWjPjC7MeK7ovUu4O5mGXrP4y1DuB+6ghFMNf/GjNTyeIN42/NdpU8dLIS5bcDpMHWJXcS3t9WMLUZBuSBnAONIWtpKaD1L2nV1CO7gPBlijboavr54W4KfPNUValvy9uR4l0a4WB3VryVUxzaxRnH8aoP6W7+eRLreT/rKGcNnNYUBENArPdYbgIboeWX/aS6xF7PkJ6kT1CspIPuYHGHK9FWGWaEPGvYYMk1yJQP3g9euEmic8Aj58867HUQ==

在服务器端查看公钥文件,与客户端的公钥文件一样
[root@vm ssh]# cat /etc/ssh/ ssh_host_rsa_key.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAmTs1Y8uf2AQF7ivcyTJlMimIw5CoYnkAMEkt0lhZVEG8htGFF0D6y7v17oZ0/EQAaoFndV3dg+r/8y75DNvlFCn5IWjPjC7MeK7ovUu4O5mGXrP4y1DuB+6ghFMNf/GjNTyeIN42/NdpU8dLIS5bcDpMHWJXcS3t9WMLUZBuSBnAONIWtpKaD1L2nV1CO7gPBlijboavr54W4KfPNUValvy9uR4l0a4WB3VryVUxzaxRnH8aoP6W7+eRLreT/rKGcNnNYUBENArPdYbgIboeWX/aS6xF7PkJ6kT1CspIPuYHGHK9FWGWaEPGvYYMk1yJQP3g9euEmic8Aj58867HUQ== 


[root@emily ~]# ssh 10.0.0.149 再次连接的时候,就不会出现认证的问题了,直接输密码即可,但是比较慢
root@10.0.0.149's password: 

解决时间慢的方案:
在客户机上[root@emily Desktop]# vim /etc/ssh/ssh_config 
49 Host *
50 GSSAPIAuthentication yes--》将yes改为no


[root@vm ~]# vim /etc/ssh/sshd_config 修改服务器端的文件
122 UseDNS yes --》no 将注释取消

3.删除下面文件的dns,或采用正确的dns
[root@emily .ssh]# vim /etc/resolv.conf (前提服务器端没禁用UseDns)
#nameserver 9.8.8.8 注释错误的DNS


[root@emily Desktop]# ssh -v 10.0.0.149 如果不修改配置文件,用-v查看所有的认证过程,就知道要gssapi*认证
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: gssapi-keyex
debug1: No valid Key exchange context
debug1: Next authentication method: gssapi-with-mic
debug1: An invalid name was supplied
Cannot determine realm for numeric host address

debug1: An invalid name was supplied
Cannot determine realm for numeric host address

当客户端有公钥文件。而服务器端没有公钥文件则会出现该问题(删除~/ssh/*)
[root@emily .ssh]# ssh 10.0.0.149
Read from socket failed: Connection reset by peer

在服务器端删除sshd_host*
[root@vm ssh]# ll
总用量 156
-rw-------. 1 root root 125811 5月 16 2012 moduli
-rw-r--r--. 1 root root 2047 5月 16 2012 ssh_config
-rw-------. 1 root root 3872 5月 16 2012 sshd_config
-rw-------. 1 root root 668 7月 27 06:14 ssh_host_dsa_key
-rw-r--r--. 1 root root 590 7月 27 06:14 ssh_host_dsa_key.pub
-rw-------. 1 root root 963 7月 27 06:14 ssh_host_key
-rw-r--r--. 1 root root 627 7月 27 06:14 ssh_host_key.pub
-rw-------. 1 root root 1675 7月 27 06:14 ssh_host_rsa_key
-rw-r--r--. 1 root root 382 7月 27 06:14 ssh_host_rsa_key.pub
You have new mail in /var/spool/mail/root
[root@vm ssh]# rm -rf /etc/ssh/ssh_ho*
[root@vm ssh]# ll
总用量 132
-rw-------. 1 root root 125811 5月 16 2012 moduli
-rw-r--r--. 1 root root 2047 5月 16 2012 ssh_config
-rw-------. 1 root root 3872 5月 16 2012 sshd_config

[root@vm ssh]# service sshd restart 重新刷新服务
停止 sshd: [确定]
生成 SSH1 RSA 主机键: [确定]
生成 SSH2 RSA 主机键: [确定]
正在生成 SSH2 DSA 主机键: [确定]
正在启动 sshd: [确定]

此时再从客户端远程登录,则会出现如下错误:,解决方案,就是删除客户端的~/ssh/known_hosts即可





========================================================================

公钥验证服务器的具体细节

1从服务器端下载公钥文件,用某种算法生成随机字符


2.将客户端的随机字符copy传到服务器端,服务器用私钥解密,也生成随机字符


3.将服务器端的随机字符和客户机的随机字符相比较,相同即可 



SSH的公钥认证流程



SSH公钥认证的基本实现步骤
1.在客户机创建密钥对
ssh-keygen -t rsa 指定rsa算法
设置保护私钥文件的密码(为了方便可以不设置)
在~/.ssh下生成两个文件
id_rsa是用户的私钥
id_rsa.pub是用户的公钥
公钥验证客户端的具体细节


SSH公钥认证的基本实现步骤
2.将公钥文件上传至服务器
上传到服务器~/.ssh/authorized_keys文件
文件中每行保存一个用户的公钥
scp ~/.ssh/id_rsa.pub root@10.10.10.1:~/.ssh/authorized_keys
或者
Ssh-copy-id -i ~/.ssh/id_ras.pub root@10.10.10.1



1.服务端生成私钥,公钥,将公钥copy到服务器端




2.在服务器端生成公钥(随机字符),发送到客户机



3. 客户机使用私钥解开公钥(随机字符),生成随机字符



4.服务器将收到的随机字符与自己的随机字符想比对,相同,则下次登录就不用输密码了





运行服务器中的图形程序是SSH的重要应用
ssh命令需要在图形界面虚拟终端下运行
命令格式:ssh -X username@sshserver 

[root@emily .ssh]# ssh-keygen 用ssh-keygen生成公钥和私钥
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:
52:51:0e:4f:a8:98:7e:4e:45:4d:d6:6b:02:d2:a7:c1 root@emily.sxkeji.com
The key's randomart image is:
+--[ RSA 2048]----+
| oo==. |
| . EBo . |
| o +.=o . |
| o ..o . o |
| . ..S o |
| . o. |
| + |
| . |
| |
+-----------------+

[root@emily .ssh]# scp /root/.ssh/id_rsa.pub 10.0.0.149:/root/.ssh/authorized_keys 从公钥copy到服务器下,但多个用户copy时公钥到服务器端时,则会覆盖原有文件
root@10.0.0.149's password: 
id_rsa.pub 100% 403 0.4KB/s 00:00 
[root@emily .ssh]# ssh 10.0.0.149 下次远程登录就不用密码了
Last login: Sat Jul 27 16:49:57 2013 from 10.0.1.63

[root@tom3 ~]# ssh-keygen 在主机通tom3生成公钥私钥
Generating public/private rsa key pair. 
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
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:
34:f5:af:91:fd:72:22:6b:45:75:7c:45:cc:ea:a8:fa root@tom3.localdomain


[root@tom3 ~]# ssh-copy-id -i .ssh/id_rsa.pub 10.0.0.149 将公钥copy到服务器10.0.0.149上
[root@tom2 .ssh]# ssh 10.0.0.149 下次远程登录也不用密码了
Last login: Sat Jul 27 18:49:57 2013 from 10.0.1.63

使用ssh-copy-id不用担心会覆盖服务器端的~/.ssh/authorized_keys文件,它只会追加在末尾一行而已

[root@emily ~]# ssh-keygen 前提已经将公钥copy了一次到10.0.1.52,再次重新生成公钥私钥
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:
2c:21:0c:15:4d:fd:f3:d0:16:ee:c3:57:06:98:3a:29 root@emily.sxkeji.com
The key's randomart image is:
+--[ RSA 2048]----+
| ..o+.. o |
| o . . + . |
| o . . = . . |
| . oE B + o|
| . S. O o |
| . = . |
| o |
| |
| |
+-----------------+
[root@emily ~]# ssh-copy-id 10.0.1.52 如果在redhat6.3以上,是可以不用接公钥名称,它会默认copy过去的
root@10.0.1.52's password: 
Now try logging into the machine, with "ssh '10.0.1.52'", and check in:

.ssh/authorized_keys

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

[root@emily ~]# ssh 10.0.1.52 当再次登录时,则还是要输密码,因为连续copy本身的公钥到服务器端,服务端在较短的时间内还是使用的以前的公钥,所以再次登录还是需要密码
Agent admitted failure to sign using the key.
root@10.0.1.52's password: 
Permission denied, please try again.
root@10.0.1.52's password: 
Last login: Tue Jul 30 08:26:33 2013 from 10.0.1.63

解决方法:
[root@emily software]# ssh-add 从新使用这个命令加载新的公钥到服务器,或是重启服务器短的机器也行
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)
[root@emily software]# ssh 10.0.1.52 验证效果
Last login: Tue Jul 30 12:53:40 2013 from 10.0.1.63

阅读(9004) | 评论(3) | 转发(0) |
0

上一篇:RAID磁盘阵列详解

下一篇:samba服务实验

给主人留下些什么吧!~~

YGril2013-08-01 08:01:41

shinobiyan_cu:感谢楼主科普 但没有图片可惜了

因为网速 太差,图片就没有了,下次有时间补上

回复 | 举报

shinobiyan_cu2013-07-31 16:02:04

感谢楼主科普 但没有图片可惜了