2 ssh
1 概念
1)密钥对类型:
对称加密密钥对: 只有一个文件,加密解密用同一个文件
非对称加密密钥对: 有两个独立文件,公钥进行加密,私钥进行解密,公钥文件是根据私钥文件创建来的
Linux:
公钥 锁
密钥(私钥) 钥匙
Windows:
公钥 锁
密钥(私钥) 钥匙
1 Windows创建密钥对:第三方软件(xshell)
2 公钥文件上传,位置,名称
2)抓包命令:tcpdunmp
telnet(非加密远程连接)
1)yum安装telnet-server
2)systemct start telnet.socket
3)连接(不能root用户登录)
telnet IP 端口
3)密钥对两种类型:
对称密钥对
非对称密钥对:
## 创建:
[root@localhost ~]# ssh-keygen -t rsa -b 2048
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:i0L/wQke11cHUchg48bQ+joLl3rbhTHJPOKnjrMFp2M root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| ..+oo+.|
| =..o. |
| .+ . .|
| ..+ o . |
| . o S +.O |
| . o * B +.= |
| . + E =.o . |
| . o.O++ . |
| ===+. |
+----[SHA256]-----+
4)密钥对两种用法:加密传输、身份验证
身份验证:(注意:先创建密钥,再根据密钥创建对应的公钥)
1) 口令身份验证方式:账号密码
2) 密钥对身份验证:免密登录
利用xshell创建公钥、密钥文件实现免密登录:
xshell生成公钥、密钥文件,上传到~/.ssh/authorized_keys
5)ssh密钥对验证:
客户机用公钥向服务器请求
服务器接受后验证公钥一致,用公钥加密信息发给客户端
客户端接受加密信息用私钥解密,再用公钥加密发给服务器
服务器接收后,利用私钥解密,若和发送的加密信息一样则通过验证

2 配置ssh服务
1 准备环境:
两台主机:修改不同主机名、关防火墙、关selinux
2 用户密码验证
ssh root@IP
3 密钥对验证
Linux创建密钥对的方式:
1)ssh-keygen -t rsa -b 2048 #生成密钥
2) ssh-copy-id root@IP #密钥传输
ssh-copy-id -i #指定上传公钥文件的位置和名字(不指定时默认为:id_rsa.pub)
注意:
1) 普通用户创建密钥对后,需要修改文件的权限:
目录为700,文件为600
.ssh 目录为700
.id_rsa.pub 文件为600
# 普通用户实现免密登录:
# 在家目录执行ssh-keygen -t rsa -b 2048之后,再将Windows客户端的公钥和生成的公钥,一块放到~/.ssh/authorized_keys^C
2) 密钥对优先级高于密码
#客户端1生成公钥并上传到客户端2
[root@localhost ~]# ssh-keygen -t rsa -b 2048 #生成
[root@localhost ~]# ssh-copy-id root@192.168.18.202 #上传
[root@localhost ~]# ls ./.ssh/
authorized_keys id_rsa id_rsa.pub known_hosts
[root@localhost ~]# cat ./.ssh/id_rsa.pub #查看
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6y87oznwWqgXSOhn+fAhKt2CE1D/lesUBVkD2oxbVB/ozmfp3ZWZztVg2BB9pCB58g/XbiSvYiWaCbJYz3lLELvsYpdzepqem7SaYjGJVI9LTB3dn8RmWt/k50pyk++fSINNqk7kQaWsB9IcSxQZSPdD7lcFtYPmh/g9epvvqr//MbjDPFyKTDD4KO4KNyp8DFrcUb7CYGxlgLw1mXfyoQVlTmZDJVXal4V7sh7RsmKwZR18/qVb9ZrBpsQ41hY48xq/dK74F1rtibd//xyISt5oh3kj29VhkbUBj7oU42XUZ4C0EcheP68PfH/xG/MK5tEAG4rtLbQANQIA/gX root@localhost.localdomain
[root@localhost ~]#
#客户端2验证公钥一样
[root@localhost ~]# cat ./.ssh/authorized_keys #检查
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6y87oznwWqgXSOhn+fAhKt2CE1D/lesUBVkD2oxbVB/ozmfp3ZWZztVg2BB9pCB58g/XbiSvYiWaCbJYz3lLELvsYpdzepqem7SaYjGJVIm9LTB3dn8RmWt/k50pyk++fSINNqk7kQaWsB9IcSxQZSPdD7lcFtYPmh/g9epvvqr//MbjDPFyKTDD4KO4KNyp8DFrcUb7CYGxlgLw1mXfyoQVlTmZDJVXal4V7sh7RsmKwZR18/qVb9ZrBpsQB41hY48xq/dK74F1rtibd//xyISt5oh3kj29VhkbUBj7oU42XUZ4C0EcheP68PfH/xG/MK5tEAG4rtLbQANQIA/gX root@localhost.localdomain
#实验 客户端1 可直接通过公钥登录 客户端2
[root@localhost ~]# ssh 192.168.18.202
Last login: Sat Oct 15 19:34:52 2022 from 192.168.18.1
[root@localhost ~]#
命令别名:
(可实现批量管理服务器,避免客户端来回登录退出)
$ alias ls='ls --color=auto'
#写个脚本
#201客户端
[root@localhost ~]# tree
-bash: tree: 未找到命令
[root@localhost ~]# cat ./alias.sh
#!/bin/bash
for i in 192.168.18.202
do
ssh $i 'yum -y install tree'
done
[root@localhost ~]# ./alias.sh
[root@localhost ~]# tree #执行的脚本对201本机没有影响
-bash: tree: 未找到命令
#202客户端
[root@localhost ~]# tree #201客户端未执行脚本之前没有tree
-bash: tree: 未找到命令
[root@localhost ~]# tree #201客户端执行脚本后,有tree
.
├── anaconda-ks.cfg
└── ifcfg-ens33 -> /etc/sysconfig/network-scripts/ifcfg-ens33
0 directories, 2 files
[root@localhost ~]#
写一个别名:
[root@localhost ~]# for i in 192.168.18.202; do ssh $i 'yum -y install tree'; done #将for循环写入命令行实现同样功能
[root@localhost ~]# alias yq='for i in 192.168.18.202;do'
[root@localhost ~]# yq ssh $i 'df -h | grep "\/$"';done #通过ssh写入脚本,在201客户端查看202的根使用情况
/dev/mapper/centos-root 17G 1.5G 16G 9% /
4 禁止使用密码登录
## /etc/ssh/sshd_config 配置文件
PasswordAuthentication no
[root@localhost ~]# systemctl status sshd
[root@localhost ~]# !sys #执行最近执行的sys开头的命令
[root@localhost ~]# !52 #执行历史命令的第52条命令
ls #第52条命令为ls
alias.sh anaconda-ks.cfg ifcfg-ens33
5 禁用root远程登陆
/etc/ssh/sshd_config 配置文件
PermitRootLogin no
6 修改默认监听端口、监听IP
(可实现:机房连接局域网、远程通过连接局域网实现连接机房)
[root@localhost ~]# vim /etc/ssh/sshd_config #修改ssh配置文件,更换指定监听IP
[root@localhost ~]# grep -a3 "^Port" /etc/ssh/sshd_config
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
Port 20086
#AddressFamily any
ListenAddress 192.168.18.111 #远程只能通过本机ip:192.168.18.111连接
#ListenAddress ::
[root@localhost ~]# systemctl restart sshd #启用配置
[root@localhost ~]# cp -a /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-ens33:0 #配置临时IP
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33:0
[root@localhost ~]# egrep "ens33:0|IPADDR|UUID" /etc/sysconfig/network-scripts/ifcfg-ens33:0
NAME=ens33:0
DEVICE=ens33:0
IPADDR=192.168.18.111
[root@localhost ~]# ifdown ens33:0 && ifup ens33:0 #启用
[root@localhost ~]# ifconfig |egrep -B1 "(inet )" #检查临时IP配置成功
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.18.201 netmask 255.255.255.0 broadcast 192.168.1
--
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.18.111 netmask 255.255.255.0 broadcast 192.168.1
--
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
#Windows连接Linux
[c:\~]$ ssh 192.168.18.201 #之前连接的IP无法连接
Connecting to 192.168.18.201:22...
Could not connect to '192.168.18.201' (port 22): Connection failed.
Type `help' to learn how to use Xshell prompt. #以默认22端口,连接配置文件对应的临时IP,失败
[c:\~]$ ssh 192.168.18.111
Connecting to 192.168.18.111:22...
Could not connect to '192.168.18.111' (port 22): Connection failed.
Type `help' to learn how to use Xshell prompt.
[c:\~]$ ssh 192.168.18.111 20086 #以配置的20086端口,连接配置文件对应的临时IP,成功
Connecting to 192.168.18.111:20086...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Last login: Sat Oct 15 19:14:14 2022 from 192.168.18.1
[root@localhost ~]#
3 相关命令
## ssh -p(小) #指定端口连接
[root@localhost ~]# ssh -p 20086 192.168.18.111
## scp 文件 用户@IP:路径
scp -P(大) 端口 文件名 用户@IP:路径 #指定端口传输文件
[root@localhost ~]# echo "hello yq" >> /root/yq.txt
[root@localhost ~]# ls
anaconda-ks.cfg ifcfg-ens33 yq.txt
[root@localhost ~]#
[root@localhost ~]# scp -P 20086 /root/yq.txt root@192.168.18.111:/root/
root@192.168.18.111's password:
yq.txt
/etc/pam.d/sshd 中添加 pam_tally2.so 模块 (实现用户指定次数登录失败后锁定,解锁时间)
auth required pam_tally2.so deny=3 unlock_time=300
参数介绍:
deny=3 #三次登录失败后即锁定用户
even_deny_root #root用户登录失败,仍锁定
unlock_time #用户登录失败后锁定时间,即解封时间
root_unlock_time #root用户登录失败后锁定时间,即解封时间
pam_tally2命令 #查询指定用户登录来源IP地址
pam_tally2 --user username
ip=$(pam_tally2 --user username |awk '{printf $5"\n"}')
echo "sshd:$ip" >> /etc/hosts.deny #记录ip添加到黑名单文件中,并记录行号,以便后期删除
line=$(grep -n ".*" /etc/hosts.deny | tail -1 | cut -d ":" -f 1)
at now+30min << EOF #写个定时任务取消锁定
sed -i "${line}d" /etc/hosts.deny
[root@localhost ~]# cat ./deny_ip.sh
#!/bin/bash
for j in $(awk -F ":" '{printf $1"\n"}' /etc/passwd)
do
n=$(/usr/sbin/pam_tally2 --user $j | awk 'NR>1{printf $5"\n"}')
if [ -n "$n" ];then
/usr/sbin/pam_tally2 --user $j | awk 'NR>1{printf $5"\n"}' >> /tmp/lock_user.txt
fi
done
for i in $(cat /tmp/lock_user.txt)
do
ip=$(echo $i |cut -d "=" -f 3)
echo "sshd:$ip" >> /etc/hosts.deny
line=$(grep -n ",*" /etc/hosts.deny | tail -1 | cut -d ":" -f 1)
at now + 30min << EOF
sed -i "${line}d" /etc/hosts.deny
EOF
done
rm -rf /tmo/lock_user.txt