码迷,mamicode.com
首页 > 其他好文 > 详细

openssh

时间:2019-01-08 12:24:45      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:专用   交互式   ref   copy   bdd   The   tab   can   替换   

常见的远程登录工具
telnet(远程登录协议,端口23) 明文,可以抓包获得用户名与密码。一般只用于测试
ssh(secure shell,应用层协议,端口22)通信过程及认证过程加密且主机认证
dropbear 嵌入式系统专用的SSH服务器端和客户端工具

OpenSSH是使用SSH透过计算机网络加密通讯的实现。用于在远程系统上安全运行shell。如果在可提供ssh服务的远程Linux系统中拥有用户帐户,则ssh是通常用来远程登录到该系统的命令。ssh命令也可用于在远程系统中运行命令。


ssh简介

ssh版本

V1:基于CRC-32作MAC,无法防范中间人***
V2:双方主机协议选择安全的MAC方式,基于DH算法做密钥交换,基于RSA或DSA算法实现身份认证

ssh认证两种方式

  • 基于口令认证
  • 基于密钥认证(不需要输密码)

openssh的 工作模式

是基于C/S架构工作client/server(另外还有B/S架构browser)
服务器端sshd,配置文件/etc/ssh/sshd_config
客户端ssh ,配置文件/etc/ssh/ssh_config
ssh-keygen //密钥生成器
ssh-copy-id //将公钥传输至远程服务端
scp //跨主机安全复制工具

Secure Shell 示例

以当前用户身份创建远程交互式shell,然后在结束时使用exit命令返回到之前的shell

[root@cygames ~]# ssh 192.168.161.100
The authenticity of host ‘192.168.161.100 (192.168.161.100)‘ can‘t be established.
ECDSA key fingerprint is SHA256:xj5/Qq8WV0G4/sdF7/hNgq76VBZzsTxnCc/FuptqmFI.
ECDSA key fingerprint is MD5:7b:f5:4c:cb:3e:75:fe:ac:bf:81:d4:dd:8c:6d:18:0f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.161.100‘ (ECDSA) to the list of known hosts.
root@192.168.161.100‘s password:
Last login: Mon Jan 7 06:19:18 2019 from 192.168.161.1
[root@cy ~]#

以其他用户身份在选定主机上连接到远程shell
[root@cygames ~]# ssh chen@192.168.161.100
chen@192.168.161.100‘s password:
[chen@cy ~]$

以远程用户身份(remoteuser)在远程主机(remotehost)上通过将输出返回到本地显示器的方式来执行单一命令
[root@cygames ~]# ssh 192.168.161.100 ‘/usr/sbin/ip a s ens33‘
root@192.168.161.100‘s password:
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:d9:31:5f brd ff:ff:ff:ff:ff:ff
inet 192.168.161.100/24 brd 192.168.161.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::7c5f:bec9:7ecd:41f7/64 scope link
valid_lft forever preferred_lft forever

ssh密钥认证

ssh通过公钥加密的方式保持通信安全。当某一ssh客户端连接到ssh服务器时,在该客户端登录之前,服务器会向其发送公钥副本。这可用于为通信渠道设置安全加密,并可验证客户端的服务器。
用户第一次使用ssh连接到特定服务器时,ssh命令可在用户的~/.ssh/known_hosts文件中存储该服务器的公钥。在此之后每当用户进行连接时,客户端都会通过对比~/.ssh/known_hosts文件中的服务器条目和服务器发送的公钥,确保从服务器获得相同的公钥。如果公钥不匹配,客户端会假定网络通信已遭劫持或服务器已被***,并且中断连接。

这意味着,如果服务器的公钥发生更改(由于硬盘出现故障导致公钥丢失,或者出于某些正当理由替换公钥),用户则需要更新其~/.ssh/known_hosts文件并删除旧的条目才能够进行登录。

root@cy ~]# cat ~/.ssh/known_hosts
192.168.161.177 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBMLHizRSDpHB+WORU4Qbdd8lghQeQzfzyGTvZU5oSthAvok9O97yRN4Hmt8y3WhGrfdfLbZlrcd/I+xmn1b025s=
[root@cy ~]# ls /etc/ssh/key
/etc/ssh/ssh_host_ecdsa_key /etc/ssh/ssh_host_ed25519_key.pub
/etc/ssh/ssh_host_ecdsa_key.pub /etc/ssh/ssh_host_rsa_key
/etc/ssh/ssh_host_ed25519_key /etc/ssh/ssh_host_rsa_key.pub

配置基于 SSH 密钥的身份验证

[root@cygames ~]# ssh-keygen -t rsa //使用 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:
SHA256:EDJOascdlJi/8SYKzeWJDyGGGaSQqou937JUFa8y5zM root@cygames
The key‘s randomart image is:
+---[RSA 2048]----+
|oo +++.. |
|= =o+.o o |
|o+o +.o . . |
|+.o.. +o . |
|.. + =+=S |
|. . =.==o |
|.o ..+ oE |
|o ..o.. o |
| .ooo. |
+----[SHA256]-----+
[root@cygames ~]# ssh-copy-id root@192.168.161.100 //使用 ssh-copy-id 将公钥复制到远程系统上的正确位置
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host ‘192.168.161.100 (192.168.161.100)‘ can‘t be established.
ECDSA key fingerprint is SHA256:xj5/Qq8WV0G4/sdF7/hNgq76VBZzsTxnCc/FuptqmFI.
ECDSA key fingerprint is MD5:7b:f5:4c:cb:3e:75:fe:ac:bf:81:d4:dd:8c:6d:18:0f.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.161.100‘s password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh ‘root@192.168.161.100‘"
and check to make sure that only the key(s) you wanted were added.

[root@cygames ~]# ssh 192.168.161.100 //无需命令直接登录
Last login: Mon Jan 7 06:26:35 2019
[root@cy ~]#

openssh

标签:专用   交互式   ref   copy   bdd   The   tab   can   替换   

原文地址:http://blog.51cto.com/14150877/2339948

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!