目录
一、SSH协议概述
二、SSH协议工作原理
三、ssh服务与配置文件
3.1、openssh
3.2、ssh命令
3.3、服务端配置
四、基于密钥验证的免交互登录
4.1、客户端生成密钥
4.2、将公钥拷贝至服务器
4.3、验证免密登录
一、SSH协议概述
SSH(Secure Shell)是一种加密的网络协议,广泛用于远程登录、远程命令执行和数据传输,具有高度的安全性。
SSH 协议通常运行在TCP 22端口上,允许用户通过命令行界面(CLI)连接到远程计算机。它通过加密技术提供认证、加密通信、数据完整性验证和保护,从而确保通信的安全性。
二、SSH协议工作原理
- 密钥交换:ssh协议使用对称加密和公钥加密结合的方式进行通信,客户端与服务器通过密钥交换协议安全地交换密钥。
- 连接建立:客户端发起连接请求,服务器响应并协商加密算法。
- 身份验证:
- 密码验证:客户端提供密码,服务器验证密码地正确性。
- 密钥验证:通过上图中公钥与私钥配对进行身份验证。服务器保存客户端的公钥,客户端通过加密算法生成签名,服务器用保存的公钥验证签名,确认客户端身份。
- 加密通信:身份验证成功后,客户端与服务器间的所有通信和数据传输,都会通过加密通道进行传输,保障数据的安全。
三、ssh服务与配置文件
3.1、openssh
在CentOS7中默认安装了openssh相关的软件包,sshd服务作为守护进程,监听客户端的ssh连接请求。
- 服务名称:sshd
- 服务端主程序:/usr/sbin/sshd
- 服务端配置文件:/etc/ssh/sshd_config
- 客户端配置文件:/etc/ssh/ssh_config
3.2、ssh命令
语法格式:ssh 参数 域名或IP地址
-p | 设置远端服务器上的端口号 | -l | 设置登录用户名 |
-t | 进行跳板连接 | -o | 设置配置参数 |
参考示例:
[root@localhost ~]# ssh 192.168.153.223
The authenticity of host '192.168.153.223 (192.168.153.223)' can't be established.
ECDSA key fingerprint is SHA256:vaEdVp3wv8zsRpAgVT2OrDSw6PcGMfetQ65uzt9Vwgo.
ECDSA key fingerprint is MD5:7b:f9:4a:12:87:fb:ef:12:b7:b8:c8:3e:81:71:2b:f7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.153.223' (ECDSA) to the list of known hosts.
root@192.168.153.223's password:
Last login: Tue Feb 25 21:01:24 2025 from 192.168.153.1
[root@localhost ~]# ssh 192.168.91.101 ifconfig //还可衔接命令使用
3.3、服务端配置
[root@ky15-1 ~]# vim /etc/ssh/sshd_config
17 Port 22
//ssh协议默认22端口,生产环境建议修改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
//用来设置最大失败尝试登陆次数为6MaxSessions 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
//以下可以限制可登录用户的办法:白名单 黑名单
AllowUsers user1 user2 user3@ip(限制主机)
DenyUsers user1 user2 user3
AllowGroups g1 g2
DenyGroups g1 g2
四、基于密钥验证的免交互登录
4.1、客户端生成密钥
[root@localhost ~]# ssh-keygen //通过ssh-keygen命令生成密钥
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:
SHA256:JOcopg5QMvdl2H0M4mHwZ7tQtm4HzZ/RA9w+BSbz2w0 root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| ..+ . o o |
| * + o . * . |
|o o . B O o o E .|
| = . o @ * + =.|
|. + o S o . * o|
|. o . o o . o o |
|. . + . o |
| o . . |
| . |
+----[SHA256]-----+
[root@localhost ~]# ls /root/.ssh //密钥生成
id_rsa id_rsa.pub
4.2、将公钥拷贝至服务器
[root@localhost .ssh]# ssh-copy-id -i id_rsa.pub 192.168.153.23
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_rsa.pub"
The authenticity of host '192.168.153.23 (192.168.153.23)' can't be established.
ECDSA key fingerprint is SHA256:ImW98bPgqIlxXQjUtQethpgpBSotG8KqEKOAOqVcWg0.
ECDSA key fingerprint is MD5:71:8f:21:16:23:ba:9a:c4:ea:1d:d2:f7:f7:57:92:cd.
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.153.23's password: //输入目标服务器密码Number of key(s) added: 1 //传输完成Now try logging into the machine, with: "ssh '192.168.153.23'"
and check to make sure that only the key(s) you wanted were added.[root@localhost .ssh]# ls
id_rsa id_rsa.pub known_hosts
4.3、验证免密登录
若想实现两台服务器互相免密登录,需两台服务器互相拷贝公钥。
[root@localhost .ssh]# ssh 192.168.153.23 //免密登录完成
Last login: Tue Feb 25 21:01:29 2025 from 192.168.153.1
[root@localhost ~]#