ssh命令详解-凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 51232
  • 博文数量: 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: [确定]

此时再从客户端远程登录,则会出现如下错误:,凯发app官方网站的解决方案,就是删除客户端的~/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

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

上一篇:raid磁盘阵列详解

下一篇:samba服务实验

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

ygril2013-08-01 08:01:41

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

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

|

2013-07-31 16:02:04

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

|
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图