ssh远程登录协议

ssh远程登录协议

什么是ssh

SSH即安全外壳协议(Secure Shell),是一种网络协议,用于在计算机网络上提供安全的远程登录和命令执行功能。SSH通过加密通信通道来保护数据传输,防止敏感信息在传输过程中被窃听或篡改。SSH支持多种身份验证方法,包括密码、公钥和证书等,以确保只有授权用户能够访问系统。除了远程登录,SSH还支持文件传输和端口转发等功能,使得SSH成为一种广泛使用的安全远程管理工具。

ssh原理

1.客户端连接服务器的SSH服务端口(默认是22),发送随机数、支持的加密算法列表、SSH版本号等信息。

2.服务器端SSH服务端程序会选择一个加密算法和HASH算法,并生成自己的公钥,发送给客户端。

3.客户端收到服务器的公钥后,会进行验证,如果公钥合法,客户端会生成会话密钥,用服务器的公钥加密,发送给服务器。

4.服务器收到客户端发来的会话密钥后,用自己的私钥解密,确认会话密钥的合法性。

5.服务器和客户端用会话密钥、HASH算法、加密算法等信息生成一个新的密钥,用于后续数据传输的加密。

6.完成上述过程后,客户端和服务器端就可以开始进行加密的数据传输了。

ssh登录

ssh [远程主机用户名]@[远程服务器主机名或IP地址] -p

-l :-l 选项,指定登录名称。
-p: -p 选项,指定登录端口(当服务端的端口非默认时,需要使用-p 指定端口进行登录)

[root@localhost named]# ssh 192.168.10.20  		#若没有修改配置,默认的端口为22
The authenticity of host '192.168.10.20 (192.168.10.20)' can't be established.
ECDSA key fingerprint is SHA256:KtaBKXckd5FGPVjjbVKvTH7FlTeo5/gNAXFWe9UlBlY.
ECDSA key fingerprint is MD5:cf:5f:ec:a4:cc:73:0a:d9:92:7e:35:bd:42:8e:a0:bb.
Are you sure you want to continue connecting (yes/no)? 
 yes
Warning: Permanently added '192.168.10.20' (ECDSA) to the list of known hosts.
root@192.168.10.20's password: 
Last failed login: Sun May  5 15:12:57 CST 2024 from 192.168.10.10 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Sat May  4 17:08:06 2024 from 192.168.10.1
[root@localhost ~]#    						##这是192.168.10.20里

服务器ssh配置

ssh配置文件:/etc/ssh/sshd-config

通过修改配置文件来改变ssh的配置

ListenAddress ip
#监听地址设置SSHD服务器绑定的IP 地址,0.0.0.0 表示侦听所有地址安全建议:如果主机不需要从公网ssh访问,可以把监听地址改为内网地址 这个值可以写成本地IP地址,也可以写成所有地址,即0.0.0.0 表示所有IP。
LoginGraceTime 2m
#用来设定如果用户登录失败,在切断连接前服务器需要等待的时间,单位为秒
PermitRootLogin yes 
#默认 ubuntu不允许root远程ssh登录
StrictModes yes   
#检查.ssh/文件的所有者,权限等
MaxAuthTries 
#用来设置最大失败尝试登陆次数为6
MaxSessions  10         
#同一个连接最大会话
PubkeyAuthentication yes     
#基于key验证
PermitEmptyPasswords no      
#密码验证当然是需要的!所以这里写 yes,也可以设置为 no,在真实的生产服务器上,根据不同安全级别要求,有的是设置不需要密码登陆的,通过认证的秘钥来登陆。
PasswordAuthentication yes   
#基于用户名和密码连接
GatewayPorts no
ClientAliveInterval 10 
#单位:秒
ClientAliveCountMax 3 
#默认3
UseDNS yes 
#提高速度可改为no   内网改为no  禁用反向解析
GSSAPIAuthentication yes #提高速度可改为no
MaxStartups    #未认证连接最大值,默认值10
Banner /path/file
#以下可以限制可登录用户的办法:白名单  黑名单
#修改默认端口
[root@localhost ssh]#vim  /etc/ssh/sshd_config
#17 行修改自己默认的端口
17 Port 9527

ssh免密登录

通过ssh来生成并发送到指定的IP地址

[root@localhost ~]# ssh-keygen -t rsa			#生成ssh的公密钥
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:
SHA256:4yEc1dbyJg7OaJoB7HV8em3EpJgrmFV9w0tQcEqcR0Q root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|       o=@E.     |
|      .o+o@ .    |
| .   o.o.O =     |
|  o o.=.+ = o    |
| . * .oBS= o     |
|  + o =o+o+      |
|     * ...       |
|    o            |
|                 |
+----[SHA256]-----+
[root@localhost ~]# ssh-copy-id -i root@192.168.10.10 			#发送到
/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
root@192.168.10.10's password: 

Number of key(s) added: 1

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

[root@localhost ~]# ssh root@192.168.10.10
Last login: Sun May  5 16:58:06 2024 from 192.168.10.20
[root@localhost ~]# ssh-copy-id -i root@服务端IP		#将密钥文件拷贝给服务端,并输入一次服务器端密码

posted @ 2024-05-05 17:26  红荼  阅读(14)  评论(0编辑  收藏  举报