这里我们可以使用wireshark抓包工具验证telnet明文传输
[root@server ~]# yum install telnet-server -y # 安装telnet
[root@server ~]# systemctl start telnet.socket # 启动服务
[root@server ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:9e:1d:25 brd ff:ff:ff:ff:ff:ff
altname enp3s0
inet 192.168.80.129/24 brd 192.168.80.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe9e:1d25/64 scope link noprefixroute
valid_lft forever preferred_lft forever
在wireshark选择记录后单右键->追踪流->TCP流
我们可以看到是明文传输
明文:原始消息
密文:加密后的消息
密钥:加解密的关键参数
加密算法:替换,位置移动等等
明文:hello
密钥:3
参考:
abcdefghijklmnopq...
defghijklmnopqrstuv...
密文:KHOOR
加解密使用同一个密钥,DES
加解密双方各用一对密钥,公钥和私钥
bob 公钥b 私钥b
alice 公钥a 私钥a
1.bob 公钥b加密 <------> alice 私钥b解密 (不成立)
2.bob 公钥a加密 <------> alice 私钥a解密 (能保证数据安全,但不能保证身份验证)
3.bob 私钥b加密 <------> alice 公钥b解密 (能进行身份验证,但不能保证数据安全)
4.bob 私钥a加密 <------> alice 公钥a解密 (不成立)
所以,在生产中,也就只能使用23方法
非对称加密使用rsa算法
数字证书
1.传统的密码认证
2.密钥认证
SSH-<主协议版本号>.<次协议版本号>.<软件版本号>
,协议版本号由主版本号和次版本号组成,软件版本号主要是为调试使用。[root@server ~]# yum install openssh-server -y
[root@server ~]# systemctl status sshd
[root@server ~]# vim /etc/ssh/sshd_config
21.#Port 22 # 默认监听22端口,可修改
22.#AddressFamily any # IPV4和IPV6协议家族用哪个,any表示二者均有
23.#ListenAddress 0.0.0.0 # 指明监控的地址,0.0.0.0表示本机的所有地址(默认可修改)
24.#ListenAddress :: # 指明监听的IPV6的所有地址格式
26.#HostKey /etc/ssh/ssh_host_rsa_key # rsa私钥认证,默认
27.#HostKey /etc/ssh/ssh_host_ecdsa_key # ecdsa私钥认证
28.#HostKey /etc/ssh/ssh_host_ed25519_key # ed25519私钥认证
34 #SyslogFacility AUTH # ssh登录系统的时会记录信息并保存在/var/log/secure
35.#LogLevel INFO # 日志的等级
39.#LoginGraceTime 2m # 登录的宽限时间,默认2分钟没有输入密码,则自动断开连接
40 #PermitRootLogin prohibit-password # 只允许root以密钥形式登录,不能以密码的方式登录 可以设置PermitRootLogin yes,允许管理员登录
41.#StrictModes yes # 是否让sshd去检查用户主目录或相关文件的权限数据
42.#MaxAuthTries 6 # 最大认证尝试次数,最多可以尝试6次输入密码。之后需要等待某段时间后才能再次输入密码
43.#MaxSessions 10 # 允许的最大会话数
49.AuthorizedKeysFile .ssh/authorized_keys # 选择基于密钥验证时,客户端生成一对公私钥之后,会将公钥放到.ssh/authorizd_keys里面
65 #PasswordAuthentication yes # 登录ssh时是否进行密码验证
66 #PermitEmptyPasswords no # 登录ssh时是否允许密码为空
……
123.Subsystem sftp /usr/libexec/openssh/sftp-server #支持 SFTP ,如果注释掉,则不支持sftp连接
AllowUsers user1 user2 #登录白名单(默认没有这个配置,需要自己手动添加),允许远程登录的用户。如果名单中没有的用户,则提示拒绝登录
[root@server ~]# vim /etc/ssh/sshd_config
Port 2222 # 修改第21行参数,去掉#,改为2222
[root@server ~]# systemctl restart sshd
[root@server ~]# netstat -ntlp # 查看端口号是否改变
[root@node1 ~]# ssh [email protected] # 默认登录会被拒绝,因为我们已经把端口号改了
ssh: connect to host 192.168.80.129 port 22: Connection refused
[root@node1 ~]# ssh -p 2222 [email protected] # 使用-p执行端口登录
[root@server ~]#
# 定位server端,修改会原来的22端口
[root@server ~]# vim /etc/ssh/sshd_config
Port 22 # 修改第21行参数,去掉#,改为22
[root@server ~]# systemctl restart sshd
[root@server ~]# netstat -ntlp
# 开启selinux
[root@server ~]# vim /etc/selinux/config
SELINUX=enforcing # 改为enforcing即开启
[root@server ~]# reboot # 重启生效,等待
[root@server ~]# vim /etc/ssh/sshd_config # 重新修改端口号为2222
Port 2222
#重启服务后会报错,selinux会拦截端口号修改
[root@server ~]# systemctl restart sshd
Job for sshd.service failed because the control process exited with error code.
See "systemctl status sshd.service" and "journalctl -xeu sshd.service" for details. # 输入这两条提示命令可以进行排错
[root@server ~]# setenforce 0 # 临时关闭selinux,为1表示开启selinux
[root@server ~]# getenforce # 查看selinux状态
Permissive # 临时关闭,不拦截但会记录行为
[root@server ~]# systemctl restart sshd # 临时关闭selinux后重启服务成功
[root@server ~]# ll /etc/ssh/sshd_config.d
总用量 8
-rw-r--r--. 1 root root 141 11月 10 23:42 01-permitrootlogin.conf
-rw-------. 1 root root 719 10月 30 18:23 50-redhat.conf
[root@server ~]# vim /etc/ssh/sshd_config.d/01-permitrootlogin.conf
PermitRootLogin no # yes修改为no,表示拒绝以root身份登录服务器
[root@server ~]# systemctl restart sshd
[root@node1 ~]# ssh -p 2222 [email protected] # 以root身份登录被拒绝
[email protected]'s password:
Permission denied, please try again.
在做实验3之前,我们西安介绍一些基础知识
[root@server ~]# ssh-keygen -t rsa
分析
第一步:定位node1,制作公私对
[root@node1 ~]# ssh-keygen -t rsa
[root@node1 ~]# ssh-copy-id [email protected] # 将node1公钥上传给129主机
The authenticity of host '192.168.48.130 (192.168.48.130)' can't be established.
ED25519 key fingerprint is SHA256:K7nvJFkfIh+p9YytEGR44wLbTfpB0Y52oVou0UdG6nc.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? 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
[email protected]'s password: # 输入登录密码
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@node1 ~]# ssh [email protected]
Activate the web console with: systemctl enable --now cockpit.socket
Register this system with Red Hat Insights: insights-client --register
Create an account or view all your systems at https://red.ht/insights-dashboard
Last failed login: Sun Nov 19 15:34:55 CST 2023 from 192.168.80.130 on ssh:notty
There were 6 failed login attempts since the last successful login.
Last login: Sun Nov 19 14:38:54 2023 from 192.168.80.1
[root@server ~]#
[root@server ~]# ssh-keygen -t rsa #当这条命令一执行后,就会在当前用户的家目录下会有一个.ssh的目录,.ssh下会有id_rsa 和 id_rsa.pub
[root@server ~]# ssh-copy-id [email protected] # 当这条命令执行后,192.168.80.130这台主机上的当前用户的家目录下的.ssh目录下会出现authorized_keys这个文本
[root@server ~]# ssh [email protected]
Activate the web console with: systemctl enable --now cockpit.socket
Register this system with Red Hat Insights: insights-client --register
Create an account or view all your systems at https://red.ht/insights-dashboard
Last login: Sun Nov 19 15:42:21 2023 from 192.168.80.129
[root@node1 ~]#
最终实验3完成
# 注意:以下步骤检查后若有authorized_keys文件则可忽略
# 先在服务器端检查/root/.ssh/authorized_keys是否存在,它是存储公钥的文件,若不存在需要创建
# 服务器端操作
[root@server ~]# cd /root
[root@server ~]# ls -a # 先检查
[root@server ~]# mkdir .ssh
[root@server ~]# cd .ssh
[root@server .ssh]# vim authorized_keys
#有时需要注意.ssh目录的权限