ssh
文章目录
工具收集
sshpass
- 个人环境的 ssh 密码输入工具
- 注意:不安全,不建议用于服务器
参考:
ssh tunnel
linux:
=autossh 命令autossh -M 4010 -NR 22:remote_host:8822 remote_host
windows:
sshtunnel-ng 软件java -jar sshtunnel-ng-0.5.9.jar
- 缺点:无法记住 map 类型
windows: jentunnel
pythonLocal Forwarding
sshtunnel 包
使用内网穿透工具
X11 forwarding: ssh 在本地打开远程的应用, remote X11 GUI
- 参考:OpenSSH - ArchWiki
命令:
ssh -Y myhost emacs -q
依赖
- sshd_config 开启 X11Forward=YES
- server 和 client 安装 xorg-xauth
wayland forwarding
- 使用 waypipe
命令:
waypipe ssh <myhost>
依赖
- 两端安装 wayland, waypipe
- 应用
部署
启动 ssh 服务
1sudo systemctl enable sshd.service权限设置
/home/user设置成 755/home/user/.ssh设置成 700/home/user/.ssh/*设置成 600eg:
1 2 3chmod 755 ~ chmod 700 ~/.ssh/ chmod 600 ~/.ssh/*
Google VPS
Hosts
bigwave
| |
mosh
MOSH CONNECT 60001 g3uQ5AZyxoDZifgvSdaHow
Office
Hosts
| |
使用跳板机 间接 ssh
https://blog.csdn.net/DiamondXiao/article/details/52474104
命令法
| |
设置 config 配置
| |
eg:
| |
端口映射法
- 先通过跳板机端口映射到本机
- 在访问本机端口
port mapping
-N
- 不登录,只做 mapping
-f
- 后台运行
-L
| |
作用
- 访问 (local_ip: local_port), 相当于 访问 (to_visit_ip: to_visit_port)
用途
问题解说
- 本人是 A
- 不能访问的网站 google.com 是 B
- 外网机器 C 是能访问 B 的
破防操作
- 通过 C 来访问 B(google.com)
1ssh (A)localhost:443:(B)www.google.com:443 name@C_machine记忆口诀
抢端口,抢别人的端口
- 用别人的端口
- 好访问别人
-R
解决问题
在家中访问公司内网,但是公司可以访问外网(即家中)
- 实现
- 在公司做 ssh -R
把能够访问服务的机器的端口好,映射到不能访问给定服务的机器上
- 举例:学校的机器被禁用访问外网,
格式
1ssh -R [remote_host:]remote_port:local_ip:local_port Remote_Machine(提供sshd 的机器)注解
- remote_host
省略
- 表示 localhost, 即 只有 Remote_Machine 的本机 127.0.0.1
其它
- Remote_Machine 的 其它有效地址,如:192.168.1.127
作用
把本机(或内网的端口),送到远处
- 即把内网的端口绑定到远程机器
记忆口诀
抛射端口,扔出去自己的端口
- 给别人用
- 让别人可以访问我们
调试 sshd
| |
sshd 错误
没有 hostkeys
- "sshd: no hostkeys available – exiting."
解决办法
ssh-keygen 命令 生成 keys
1sudo ssh-keygen -A
参考
ssh 建立连接的密钥验证机制
防黑客
工具
cowrie
- GitHub - cowrie/cowrie: Cowrie SSH/Telnet Honeypot https://cowrie.readthedocs.io
- Cowrie 部署 SSH 蜜罐_寰宇 001 的博客-CSDN博客
使用:
| |
autossh 用法
功能:
- 支持 remote map 和 local map
- 自动 keep alive
systemd 用法
下述例子 -R, 把本地的 127.0.0.1:22 端口映射到服务器 theuser@server-ip 登录后的 server 上 127.0.0.1:9422 端口
| |
实战
wsl + win10 rdp + remote map
功能:把宿主机 Win10 的 3389 端口,remote map (-R) 映射到远端(Home)机器上
注意事项:
win10 宿主机
- 需要在设置开启 rdp 远程桌面功能
- 开启防火墙(如果失败,需要开启防火墙)
wsl 设置 ssh -R 或者 auotssh -R
/usr/bin/autossh -M 0 -N -q -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" home -R :13389:<win10-host-ip>:3389- 注意这里在 wsl 上映射时,需要使用 win10 宿主机的 ip (eg: 192.168.1.100), 不能使用 localhost, 避免无效映射
无法映射到远端机器外网 ip 解决办法
- 使用
socat把本地端口转发到外网 ip 端口 sudo socat TCP-LISTEN:23389,fork,reuseaddr,bind=<home-pc-ip> TCP:localhost:13389- 这样在远端内容中,直接 rdp <home-pc-ip>:3389 就可以连接远端 win10 主机登录了
- 使用
实现例子:
autossh win-host-ip:3389 -> home-pc-localhost:3389
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15[Unit] Description=Keeps a tunnel to 'office_win rdp remote map to laptop' open After=network.target [Service] # User=autossh # -p [PORT] # -l [user] # -M 0 --> no monitoring # -N Just open the connection and do nothing (not interactive) # LOCALPORT:IP_ON_EXAMPLE_COM:PORT_ON_EXAMPLE_COM ExecStart=/usr/bin/autossh -M 0 -N -q -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" home -R :13389:192.168.1.100:3389 [Install] WantedBy=default.targetsocat 把本地端口 3389 转发到 wsl
1 2 3 4 5 6 7 8 9[Unit] Description=Socat Script service After=network.target [Service] ExecStart=socat TCP-LISTEN:23389,fork,reuseaddr,bind=<home-pc-ip> TCP:localhost:13389 [Install] WantedBy=default.target
ssh + socks5 代理
启动代理服务
| |
- 服务 192.168.1.100:7890 会把流量转发到 172.168.1.200 机器上,再转到互联网上
使用代理服务
使用本地 dns服务
1https_proxy=socks5://192.168.1.100:7890 curl https://www.baidu.com使用远程 dns服务
1https_proxy=socks5h://192.168.1.100:7890 curl https://www.baidu.com- 注意:
socks5h–> 多了一个h
- 注意:
ssh -R 如何允许绑定到 ip 上
命令:
| |
绑定失败修复:
修改 /etc/ssh/sshd_config
1GatewayPorts yes
文章作者
上次更新 2025-06-20 (811ee6f)