天天看点

openssh

常见的远程登录工具

telnet(远程登录协议,端口23) 明文,可以抓包获得用户名与密码。一般只用于测试

ssh(secure shell,应用层协议,端口22)通信过程及认证过程加密且主机认证

dropbear 嵌入式系统专用的SSH服务器端和客户端工具

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

V1:基于CRC-32作MAC,无法防范中间人***

V2:双方主机协议选择安全的MAC方式,基于DH算法做密钥交换,基于RSA或DSA算法实现身份认证

基于口令认证

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

是基于C/S架构工作client/server(另外还有B/S架构browser)

服务器端sshd,配置文件/etc/ssh/sshd_config

客户端ssh ,配置文件/etc/ssh/ssh_config

ssh-keygen //密钥生成器

ssh-copy-id //将公钥传输至远程服务端

scp //跨主机安全复制工具

以当前用户身份创建远程交互式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.

[email protected]'s password:

Last login: Mon Jan 7 06:19:18 2019 from 192.168.161.1

[root@cy ~]#

以其他用户身份在选定主机上连接到远程<code>shell</code>

[root@cygames ~]# ssh [email protected]

[email protected]'s password:

[chen@cy ~]$

以远程用户身份(remoteuser)在远程主机(remotehost)上通过将输出返回到本地显示器的方式来执行单一命令

[root@cygames ~]# ssh 192.168.161.100 '/usr/sbin/ip a s ens33'

2: ens33: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; 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

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

[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 [email protected] //使用 ssh-copy-id 将公钥复制到远程系统上的正确位置

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"

/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

Number of key(s) added: 1

Now try logging into the machine, with: "ssh '[email protected]'"

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

继续阅读