ssh服务
1. 关闭密钥检查
- 编辑配置文件禁用首次连接的询问过程
sed -i.bak '/StrictHostKeyChecking/s/.*/StrictHostKeyChecking no/' /etc/ssh/ssh_config
2. openssh服务
2.1 查看软件包
[root@ssh-server ~]# rpm -qa openssh*
openssh-server-7.4p1-21.el7.x86_64
openssh-7.4p1-21.el7.x86_64
openssh-clients-7.4p1-21.el7.x86_64
2.2 服务端包
[root@ssh-server ~]# rpm -ql openssh-server
/etc/pam.d/sshd
/etc/ssh/sshd_config
/etc/sysconfig/sshd
/usr/lib/systemd/system/sshd-keygen.service
/usr/lib/systemd/system/sshd.service
/usr/lib/systemd/system/sshd.socket
/usr/lib/systemd/system/sshd@.service
/usr/lib64/fipscheck/sshd.hmac
/usr/libexec/openssh/sftp-server
/usr/sbin/sshd
/usr/sbin/sshd-keygen
/usr/share/man/man5/moduli.5.gz
/usr/share/man/man5/sshd_config.5.gz
/usr/share/man/man8/sftp-server.8.gz
/usr/share/man/man8/sshd.8.gz
/var/empty/sshd
2.3客户端包
[root@ssh-server ~]# rpm -ql openssh-clients
/etc/ssh/ssh_config
/usr/bin/scp
/usr/bin/sftp
/usr/bin/slogin
/usr/bin/ssh
/usr/bin/ssh-add
/usr/bin/ssh-agent
/usr/bin/ssh-copy-id
/usr/bin/ssh-keyscan
/usr/lib64/fipscheck/ssh.hmac
/usr/libexec/openssh/ssh-pkcs11-helper
/usr/share/man/man1/scp.1.gz
/usr/share/man/man1/sftp.1.gz
/usr/share/man/man1/slogin.1.gz
/usr/share/man/man1/ssh-add.1.gz
/usr/share/man/man1/ssh-agent.1.gz
/usr/share/man/man1/ssh-copy-id.1.gz
/usr/share/man/man1/ssh-keyscan.1.gz
/usr/share/man/man1/ssh.1.gz
/usr/share/man/man5/ssh_config.5.gz
/usr/share/man/man8/ssh-pkcs11-helper.8.gz
2.4 通用包
[root@ssh-server ~]# rpm -ql openssh
/etc/ssh
/etc/ssh/moduli
/usr/bin/ssh-keygen
/usr/libexec/openssh
/usr/libexec/openssh/ctr-cavstest
/usr/libexec/openssh/ssh-keysign
/usr/share/doc/openssh-7.4p1
/usr/share/doc/openssh-7.4p1/CREDITS
/usr/share/doc/openssh-7.4p1/ChangeLog
/usr/share/doc/openssh-7.4p1/INSTALL
/usr/share/doc/openssh-7.4p1/OVERVIEW
/usr/share/doc/openssh-7.4p1/PROTOCOL
/usr/share/doc/openssh-7.4p1/PROTOCOL.agent
/usr/share/doc/openssh-7.4p1/PROTOCOL.certkeys
/usr/share/doc/openssh-7.4p1/PROTOCOL.chacha20poly1305
/usr/share/doc/openssh-7.4p1/PROTOCOL.key
/usr/share/doc/openssh-7.4p1/PROTOCOL.krl
/usr/share/doc/openssh-7.4p1/PROTOCOL.mux
/usr/share/doc/openssh-7.4p1/README
/usr/share/doc/openssh-7.4p1/README.dns
/usr/share/doc/openssh-7.4p1/README.platform
/usr/share/doc/openssh-7.4p1/README.privsep
/usr/share/doc/openssh-7.4p1/README.tun
/usr/share/doc/openssh-7.4p1/TODO
/usr/share/licenses/openssh-7.4p1
/usr/share/licenses/openssh-7.4p1/LICENCE
/usr/share/man/man1/ssh-keygen.1.gz
/usr/share/man/man8/ssh-keysign.8.gz
3. 客户端ssh命令
- 远程执行命令
ssh 10.0.0.12 "sed -i.bak '/StrictHostKeyChecking/s/.*/StrictHostKeyChecking no/' /etc/ssh/ssh_config"
3.1 常见选项
-p 远程服务器监听端口
-b 指定连接的源IP
-v 调试模式
-C 支持x11转发
-t 强制tty分配,如: ssh -t remoteserver1 ssh -t remoteserver2 ssh remoteserver3
-o option 如:-o StrictHostKeyChecking=no
3.2 查看登录失败测试
lastb
lastb -f /var/log/btmp
3.3 安装测试包
- centos8客户端
yum install -y httpd firefox
echo welcome to xier > /var/www/html/index.html
3.4 安装x11工具连接浏览器
- centos8
# windows的地址
export DISPLAY=10.0.0.1:0.0
# 启动
firefox
# Linux中
ssh -X 10.0.0.11
# 启动
firefox
4. 使用MOBA远程连接图形化
- 安装服务
yum install -y xorg-x11-xauth xorg-x11-fonts-* xorg-x11-font-utils
5. 安全案例
5.1 安全跨越连接
- 本机是:10.0.0.10,禁止10.0.0.13,10.0.0.12连接
iptables -A INPUT -s 10.0.0.13,10.0.0.12 -j REJECT
- 连接
ssh -t 10.0.0.12 ssh -t 10.0.0.11 ssh 10.0.0.10
5.2 免询问连接
ssh -o StrictHostKeyChecking=no 10.0.0.10
5.3 远程执行本机脚本
ssh 10.0.0.10 bash < /root/test.sh
5.4 ssh密钥创建使用
[root@centos7 ~]# 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:eESGoHZ9om70kkP6JytxFNdqEadZ7s5yTmlKj5HSExk root@centos7
The key's randomart image is:
+---[RSA 2048]----+
| ...+= |
| ...+O. |
| o .oEo+ |
| . ...oO |
| .+.+ S |
| .=.+ * . |
| .oB O B |
| .o.=.% |
| .o+o o |
+----[SHA256]-----+
ssh-copy-id root@10.0.0.10
5.5 ssh加口令
[root@centos7 ~]# ssh-keygen -p
Enter file in which the key is (/root/.ssh/id_rsa):
Enter new passphrase (empty for no passphrase):
Enter same passphrase again:
Saving key "/root/.ssh/id_rsa" failed: passphrase is too short (minimum five characters).
-
有缺点,传输文件时需要输入口令,需要启动代理
5.6 ssh启动代理
ssh-agent bash
ssh-add
6. 基于key验证连接服务器
6.1 将公钥上传到服务器端
- 认证文件生成
cat id_rsa_2048(xshell).pub > .ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
-
输入用户名登录
7. 自动化免密脚本
[root@centos8 ~]# cat push_key.sh
#!/bin/bash
PASS=000000
rpm -q expect &> /dev/null || yum install -y expect &> /dev/null
ssh-keygen -t rsa -P "" -f /root/.ssh/id_rsa &> /dev/null && echo "ssh key is created"
while read IP; do
expect << EOF
set timeout 20
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@$IP
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$PASS\n" }
}
expect eof
EOF
echo $IP is ready
done < hosts.txt
[root@centos8 ~]# cat hosts.txt
10.0.0.12
10.0.0.13
bash push_key.sh
8. rsync命令
- 增量复制工具,注意两端都需要安装rsync软件
rsync -av /opt server1:/tmp # 复制目录和目录下的文件
rsync -av /etc/ server1:/tmp # 只复制目录下文件
- 常用选项
-n 模拟复制过程
-v 显示详细过程
-r 递归复制目录树
-p 保留权限
-t 保留修改时间戳
-g 保留组信息
-o 保留所有者信息
-l 将软连接文本本身进行复制(默认)
-L 将软连接文件指向的文件复制
-u 如果接受者的文件比发送者的文件较新,将忽略同步
-z 压缩
-a 存档,相当于-rlptgoD,但不保留ACL(-A) 和SELinux属性(-X)
--delete 源数据删除,目标数据也自动同步删除
9. ssh高级应用
9.1 SSH本地端口转发
-
加密SSH Client 端至 SSH Server端之间的通信数据
-
突破防火墙的限制完成一些之前无法建立的TCP连接
-
SSH本地端口转发
ssh -L localport:remotehost:remotehostport sshserver
- 选项
-f 后台启用
-N 不打开远程shell,处于等待状态
-g 启用网关功能
- 范例
ssh -L 9527:telnetsrv:23 -Nfg sshsrv
telnet 127.0.0.1 9527
- 安装服务端软件配置架构
yum install -y telnet-server
systemctl enable --now telnet.socket
useradd xier
- 安装客户端连接
telnet 10.0.0.12
xier
设置的密码
- 服务端配置防火墙规则拒绝
iptables -A INPUT -s 10.0.0.11 -j REJECT
- 被禁用的主机配置端口转发
# 注意上面是被防火墙规则禁用了,所以这里需要一个桥接机
ssh -fNL 9527:10.0.0.12:23 10.0.0.10
最终需要连接的主机IP 桥梁IP
# 查看连接隧道
ss -nt
- 客户端连接测试
telnet 127.0.0.1 9527
9.1.1 实现http端口转发
- web机器
yum install -y httpd
systemctl start httpd
echo website on 10.0.0.12 > /var/www/html/index.html
- 客户端配置端口转发
ssh -fNL 6666:10.0.0.12:80 10.0.0.10
# 访问测试
curl 127.0.0.1:6666
9.2 SSH远程端口转发
- 格式
ssh -R sshserverport:remotehostport sshserver
- 清除规则
iptables -F
- 范例
- 本机是:10.0.0.10
- 配置文件并启动,远程转发启动时会在桥接机(本机)开机一个端口进行使用
vim /etc/ssh/sshd_config
105 GatewayPorts yes
systemctl restart sshd
- 远程转发命令
- 本机是:10.0.0.11
ssh -fNgR 9527:10.0.0.12:80 10.0.0.10
10. 实现kexueshangwang
主机名 | IP |
---|---|
client | 10.0.0.11 |
vps(国外服务器) | 10.0.0.12 |
10.0.0.13 |
- 当火狐访问internet时,本机的1080端口作为代理服务器,火狐的访问请求被转发到sshservver上,由sshserver替之访问internet
ssh -D 1080 root@sshserver -fNg
- 在本机火狐设置代理127.0.0.1:1080
curl --socks5 127.0.0.1:1080 http://www.google.com
10.1 SSH动态端口转发
- 在自己电脑上执行
ssh -D 1080 root@sshserver -fNg
curl --socks5 127.0.0.1:1080 http://www.google.com
10.2 Linux范例
- 测试国内拒绝访问情况(10.0.0.13)
iptables -A INPUT -s 10.0.0.11 -j REJECT
- 开启动态端口转发(10.0.0.11)
ssh -fND 9527 10.0.0.12
curl --socks5 127.0.0.1:9527 http://10.0.0.13
export DISPLAY=10.0.0.1:0.0
firefox
# 配置火狐浏览器的反向代理为:127.0.0.1 9527
10.3 windows范例
- 配置拒绝Windows 80端口作为测试(10.0.0.13)
iptables -A INPUT -s 10.0.0.1 -p tcp --dport 80 -j REJECT
- 配置监听所有转发(10.0.0.11)
- 可以直接用国外服务器作为转发,这里多了一层
ssh -gfND 9527 10.0.0.12
10.4 其它kexueshangwang工具
- ss: https://blog.51cto.com/907832555/4711855
- https://github.com/lbtooth/s k c o s w o d a h s/反
11. ssh其它工具
11.1 挂载远程ssh目录sshfs
-
可以利用协议挂载远程目录
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
yum install -y fuse-sshfs
sshfs 10.0.0.11:/data /mnt
远程主机
df /mnt
11.2 自动登录ssh工具sshpass
- 常见选项
-p password # 指定对方服务器密码
-f filename # 后跟保存密码文件
-e # 环境变量SSHPASS作为密码
- 范例
yum install -y sshpass
sshpass -p 000000 ssh -o StrictHostKeyChecking=no 10.0.0.12
sshpass -p 000000 ssh -o StrictHostKeyChecking=no 10.0.0.13 hostname -I
vim pass.txt
000000
chmod 600 pass.txt
sshpass -f pass.txt ssh -o StrictHostKeyChecking=no 10.0.0.13 hostname -I
export SSHPASS=00000
12. 轻量级自动化运维工具pssh
-
pssh: 基于python编写
-
pdsh:多线程远控shell客户端
-
mussh:一个shell脚本,用于在多个主机上通过ssh执行命令
-
pssh命令选项如下
-H: 主机字符串,内容格式 "[user@]host[:port]"
-h: file: 主机列表文件
-A: 手动输入密码格式
-i: 每个服务器内部处理信息输出
-l: 登录使用的用户名
-p: 并发的线程数[可选]
-o: 输出文件目录
-e: 错误文件输出
-t: TIMEOUT超时时间设置,0无限制
-O: SSH的选项
-P: 打印出服务器返回信息
-V: 详细模式
--version: 查看版本
- 安装使用
yum install -y pssh
vim /etc/ssh/ssh_config
35 StrictHostKeyChecking no
- 当不支持ssh的key认证时,通过 -A 选项,使用密码认证批量执行指令
pass -H "10.0.0.10 10.0.0.11" -A hostname -I
# 如果需要有输出结果
pssh -H "10.0.0.11 10.0.0.12" -A -i hostname -I
vim hosts.txt
10.0.0.11
10.0.0.12
pssh -h hosts.txt -i hostname
pssh -h hosts.txt -i useradd tomcat
pssh -h hosts.txt -i getent passwd tomcat
# -o 输出正确结果,-e 输出错误结果
pssh -h hosts.txt -o /stout -e /serr ls -l /data
# 输出对方主机变量,注意使用单引号
pssh -h hosts.txt -i 'echo $HOSTNAME'
pssh -h hosts.txt -i 'ls -l /data/*'
12.1 pscp命令
- pscp.pssh功能是将本地文件批量复制到远程主机
cat > hostname.sh << EOF
#!/bin/bash
hostname
EOF
chmod +x hostname.sh
pscp.pssh -h hosts.txt /root/hostname.sh /data/
12.2 pslurp命令
- pslurp功能是将远程主机的文件批量复制到本地
-L: 指定从远程主机下载到本机的存储目录,local是下载到本地后的名称
-r: 递归复制目录
- 范例
# 先删除原来的文件,如果之前没有用过,可以不用删除
rm -rf /data/*
pslurp -h hosts.txt -L /data/ /data/xier.txt 42.txt
13 dropbear
-
dropbear是另一个ssh开源项目软件
13.1 编译安装
wget https://matt.ucc.asn.au/dropbear/releases/dropbear-2022.83.tar.bz2
dnf install -y bzip2 gcc zlib-devel make
bzip2 -d dropbear-2022.83.tar.bz2
tar xvf dropbear-2022.83.tar
cd dropbear-2022.83/
mkdir /apps/
./configure --prefix=/apps/dropbear --sysconfdir=/etc/dropbear
make; make install
vim /etc/profile.d/dropbear.sh
PATH=/apps/dropbear/sbin:/apps/dropbear/bin:$PATH
source /etc/profile.d/dropbear.sh
mkdir /etc/dropbear
dropbearkey -t rsa -f /etc/dropbear/dropbear_rsa_host_key -s 2048
# 前台方式运行
dropbear -p :2222 -FE
# 远程连接
ssh 10.0.0.11 -p 2222
# 后台执行
dropbear -p :9527
dbclient 10.0.0.11