SSH远程管理

发布于:2024-05-05 ⋅ 阅读:(40) ⋅ 点赞:(0)

SSH远程连接协议

SSH(Secure Shell)是一种安全通道协议,主要用来实现字符界面的远程登录、远程 复制等功能。SSH 协议对通信双方的数据传输进行了加密处理,其中包括用户登录时输入的用户口令,SSH 为建立在应用层和传输层基础上的安全协议。对数据进行压缩,加快传输速度。

优点:

  • 安全性: 数据传输是加密的,可以防止信息泄漏。

  • 身份验证:防止未经授权的用户访问远程系统。

  • 远程管理:可通过SSH协议登录远程服务器并执行命令,无需直接物理访问设备。

  • 端口转发:SSH支持 端口转发功能,可以安全地传输其他协议和应用程序。

  • 传输速度: 数据传输是压缩的,可以提高传输速度。

客户端存放公钥的位置

家目录下/.ssh/know_host

连接方式

ssh ip地址

原理:

客户端首先发起连接给服务器

服务器收到请求将自己的公钥以及会话id发给客户端

客户端收到服务器的公钥,以及会话id,会做一个运算

res=会话ID+客户端的公钥

再用服务器的公钥进行加密传给服务器

服务器收到后

先用都无端自己的私钥解密

已知会话ID

res-id=客户端的公钥

服务名 主程序 配置文件
opnessh-server /usr/sbin/sshd /etc/ssh/sshd_config
opnessh-clients /usr/bin/ssh /etc/ssh/ssh_config

密钥的存放文件

自己的 /etc/ssh

对面的

家目录/,ssh/know_host

ssh 选项 IP -p 端口

ssh加密通讯原理

1.对称加密

采用单钥密码系统的加密方法,同一个密钥可以同时用作信息的加密和解密,这种加密方法称为对称加密,由于其速度快,对称性加密通常在消息发送方需要加密大量数据时使用

特点

1、加密方和解密方使用同一个密钥;
2、加密解密的速度比较快,适合数据比较长时的使用;
3、密钥传输的过程不安全,且容易被破解,密钥管理也比较麻烦;

常用算法

DES、3DES、TDEA、Blowfish、RC2、RC4、RC5、IDEA、SKIPJACK等

优点

1.算法公开 2.计算量小 3.加密速度快 4.加密效率高

缺点

  1. 在数据传送前,发送方和接收方必须商定好秘钥,然后使双方都能保存好秘钥
  2. 其次如果一方的秘钥被泄露,那么加密信息也就不安全了
  3. 每对用户每次使用对称加密算法时,都需要使用其他人不知道的独一秘钥,这会使得收、发双方所拥有的钥匙数量巨大,密钥管理成为双方的负担

2.非对称加密

非对称加密算法需要两个密钥:公开密钥(publickey:简称公钥)和私有密钥(privatekey:简称私钥)。公钥与私钥是一对,如果用公钥对数据进行加密,只有用对应的私钥才能解密。因为加密和解密使用的是两个不同的密钥,所以这种算法叫作非对称加密算法

常用算法

  • RSA(RSA algorithm):目前使用最广泛的算法
  • DSA(Digital Signature Algorithm):数字签名算法,和 RSA 不同的是 DSA仅能用于数字签名,不能进行数据加密解密,其安全性和RSA相当,但其性能要比RSA快
  • ECC(Elliptic curve cryptography,椭圆曲线加密算法)
  • ECDSA:Elliptic Curve Digital Signature Algorithm,椭圆曲线签名算法,是ECC和 DSA的结合,相比于RSA算法,ECC 可以使用更小的秘钥,更高的效率,提供更高的安全保障

原理

客户端向服务端发起连接请求

服务端收到请求后,将会话id和服务端的公钥交给客户端

客户端接收后,将会话id和客户端的公钥进行异或运算得到回复信息Res(Res=会话id^客户端公钥)

客户端再将Res用服务端的公钥加密,得到密文U(U=Res+服务端的公钥id),并将密文U交给服务端

服务端用服务端私钥将密文U解密,得到回复信息Res

服务端将Res和会话id进行异或运算,得到客户端的公钥

[root@localhost ~]# vim /etc/ssh/sshd_config 
#Port 22                     #监听端口
#AddressFamily any           #地址为任意网卡
#ListenAddress 0.0.0.0
#ListenAddress ::

#LoginGraceTime 2m           #登陆时间为2分钟
#PermitRootLogin yes         #是否允许root用户登录
#StrictModes yes             
#MaxAuthTries 6              #最大重试密码次数为6
#MaxSessions 10

#PubkeyAuthentication yes

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile      .ssh/authorized_keys

#AuthorizedPrincipalsFile none

#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no            #禁止空密码用户登录
PasswordAuthentication yes

#UseDNS no                          #禁止DNS反向解析,提高速率

[root@localhost ~]# ssh-keygen -t ecdsa        #生成密钥文件
Generating public/private ecdsa key pair.
Enter file in which to save the key (/root/.ssh/id_ecdsa):     #密钥存放位置 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):                    #密码为空直接回车
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_ecdsa.
Your public key has been saved in /root/.ssh/id_ecdsa.pub.
The key fingerprint is:
SHA256:cjM0TYPcoopfBaqUoY3qJPozDpbKHlXkDERj5+Da8e0 root@localhost.localdomain
The key's randomart image is:
+---[ECDSA 256]---+
|  oB o . oo      |
|  + X . +o..     |
| + = * oo..      |
|o * = o...       |
|.o = o.oS        |
|o.= . oo o       |
|*+ . . E         |
|=o+ .            |
|o=oo             |
+----[SHA256]-----+
[root@localhost ~]# cd .ssh
[root@localhost .ssh]# ls
id_ecdsa  id_ecdsa.pub             #公钥和私钥
[root@localhost .ssh]# ssh-copy-id -i id_ecdsa.pub 192.168.118.30
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_ecdsa.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
root@192.168.118.30's password:                  #服务端密码
Number of key(s) added: 1

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

[root@localhost .ssh]# ssh 192.168.118.30
Last login: Mon Apr 29 13:50:39 2024 from 192.168.118.20
[root@localhost ~]#