npx machina-cli add skill chaterm/terminal-skills/ssh --openclawSSH 管理与安全
概述
SSH 密钥管理、跳板机配置、端口转发、安全加固等技能。
基础连接
连接命令
# 基础连接
ssh user@hostname
ssh -p 2222 user@hostname # 指定端口
# 执行远程命令
ssh user@hostname "command"
ssh user@hostname 'ls -la && df -h'
# 详细输出(调试)
ssh -v user@hostname
ssh -vvv user@hostname # 更详细
配置文件
# ~/.ssh/config
Host myserver
HostName 192.168.1.100
User admin
Port 22
IdentityFile ~/.ssh/id_rsa_myserver
Host dev-*
User developer
IdentityFile ~/.ssh/id_rsa_dev
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
AddKeysToAgent yes
# 使用配置
ssh myserver
密钥管理
生成密钥
# 生成 RSA 密钥(推荐 4096 位)
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# 生成 Ed25519 密钥(推荐)
ssh-keygen -t ed25519 -C "your_email@example.com"
# 指定文件名
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_work
# 修改密码
ssh-keygen -p -f ~/.ssh/id_rsa
部署公钥
# 方式1:ssh-copy-id
ssh-copy-id user@hostname
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@hostname
# 方式2:手动复制
cat ~/.ssh/id_ed25519.pub | ssh user@hostname "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
# 方式3:直接编辑
ssh user@hostname
echo "public_key_content" >> ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
SSH Agent
# 启动 agent
eval "$(ssh-agent -s)"
# 添加密钥
ssh-add ~/.ssh/id_rsa
ssh-add -l # 列出已添加的密钥
# 转发 agent(跳板机场景)
ssh -A user@jumphost
端口转发
本地转发
# 将本地端口转发到远程
ssh -L local_port:target_host:target_port user@ssh_server
# 示例:访问远程 MySQL
ssh -L 3306:localhost:3306 user@dbserver
mysql -h 127.0.0.1 -P 3306
# 示例:访问内网服务
ssh -L 8080:internal.server:80 user@jumphost
curl http://localhost:8080
远程转发
# 将远程端口转发到本地
ssh -R remote_port:local_host:local_port user@ssh_server
# 示例:暴露本地服务
ssh -R 8080:localhost:3000 user@public_server
# 现在可以通过 public_server:8080 访问本地 3000 端口
动态转发(SOCKS 代理)
# 创建 SOCKS5 代理
ssh -D 1080 user@ssh_server
# 使用代理
curl --socks5 localhost:1080 http://example.com
后台运行
# 后台运行隧道
ssh -fNL 3306:localhost:3306 user@server
# -f 后台运行
# -N 不执行远程命令
# -L 本地转发
跳板机配置
ProxyJump(推荐)
# 命令行
ssh -J jumphost user@target
# 配置文件
Host target
HostName 192.168.1.100
User admin
ProxyJump jumphost
Host jumphost
HostName jump.example.com
User jumper
ProxyCommand
# 配置文件
Host target
HostName 192.168.1.100
User admin
ProxyCommand ssh -W %h:%p jumphost
安全加固
sshd_config 配置
# /etc/ssh/sshd_config
# 禁用密码登录
PasswordAuthentication no
ChallengeResponseAuthentication no
# 禁用 root 登录
PermitRootLogin no
# 限制用户
AllowUsers admin developer
AllowGroups sshusers
# 修改端口
Port 2222
# 限制登录尝试
MaxAuthTries 3
MaxSessions 5
# 空闲超时
ClientAliveInterval 300
ClientAliveCountMax 2
# 禁用不安全选项
X11Forwarding no
PermitEmptyPasswords no
应用配置
# 测试配置
sshd -t
# 重载配置
systemctl reload sshd
常见场景
场景 1:批量执行命令
# 使用 for 循环
for host in server1 server2 server3; do
ssh $host "uptime"
done
# 使用 parallel-ssh
pssh -h hosts.txt -i "uptime"
场景 2:文件传输
# scp
scp file.txt user@host:/path/
scp -r dir/ user@host:/path/
scp user@host:/path/file.txt ./
# rsync over SSH
rsync -avz -e ssh source/ user@host:/dest/
场景 3:保持连接
# ~/.ssh/config
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
TCPKeepAlive yes
# 使用 autossh
autossh -M 0 -fN -L 3306:localhost:3306 user@server
故障排查
| 问题 | 排查方法 |
|---|---|
| 连接超时 | 检查网络、防火墙、端口 |
| 权限被拒绝 | 检查密钥权限 (600)、authorized_keys |
| Host key 变更 | ssh-keygen -R hostname |
| Agent 转发失败 | 检查 AllowAgentForwarding |
| 连接断开 | 配置 ServerAliveInterval |
# 调试连接
ssh -vvv user@hostname
# 检查密钥权限
ls -la ~/.ssh/
# id_rsa: 600
# id_rsa.pub: 644
# authorized_keys: 600
# ~/.ssh: 700
Source
git clone https://github.com/chaterm/terminal-skills/blob/main/server/ssh/SKILL.mdView on GitHub Overview
你可以通过 SSH 实现密钥生命周期管理、跳板机配置、执行远程任务、建立本地/远程/动态端口转发,并对 SSH 服务进行必要的安全加固。该技能覆盖从基础连接到高级场景,帮助你在云环境和内网中维持高效、可审计的远程运维流程。
How This Skill Works
该技能基于 OpenSSH 客户端与服务端实现。你通过公钥认证、ssh-agent 管理、以及 ~/.ssh/config 统一管理主机配置。你将使用 -J/ProxyJump、-W、-L、-R、-D 等选项实现跳板机、端口转发和 SOCKS 代理,同时通过修改 /etc/ssh/sshd_config 做安全加固并使用 systemctl reload sshd 应用更改。
When to Use It
- 需要对多台服务器执行相同命令或部署脚本时(批量运维)
- 需要从本地网络访问私有/内网资源,需经过跳板机(bastion)实现安全入口
- 需要将远端服务暴露给本地或远程团队,使用本地/远程端口转发
- 需要通过 SOCKS5 代理来安全浏览或转发流量
- 需要对 SSH 服务进行安全加固(禁用密码登录、限制用户、修改端口等)并确保连接稳定性
Quick Start
- 生成密钥对并使用 Ed25519(推荐)
- 将公钥部署到目标服务器(ssh-copy-id 或手动追加到 authorized_keys)
- 在 ~./ssh/config 中添加主机条目,简化日常连接
- 测试连接并逐步引入端口转发与跳板机场景
Best Practices
- 优先使用 Ed25519 或 4096 位 RSA 的密钥,私钥权限设为 600,密钥所在目录权限设为 700
- 在服务器端禁用密码认证与 root 直接登录(PasswordAuthentication no、PermitRootLogin no)并限制 AllowUsers/AllowGroups
- 将跳板机通过 ProxyJump(或 ProxyCommand)集中管理,避免逐台配置
- 开启服务器端和客户端的心跳机制保持连接(ServerAliveInterval、ServerAliveCountMax、Autostart 等)并考虑使用 autossh 维持隧道
- 对密钥和服务器变更进行审计,定期清理不再使用的公钥,使用 known_hosts 的管理工具维护信任域;对敏感端口转发仅在受控网络中使用
Example Use Cases
- 场景示例1:批量执行命令。for host in server1 server2 server3; do ssh $host "uptime"; done;或者用 pssh 实现并行执行
- 场景示例2:文件传输。scp -r dir/ user@host:/path/,或 rsync -avz -e ssh source/ user@host:/dest/
- 场景示例3:保持连接。在 ~/.ssh/config 设置 ServerAliveInterval/ServerAliveCountMax,或使用 autossh 维持隧道状态
- 场景示例4:跳板机访问。ssh -J jumphost user@target,或在配置文件中通过 ProxyJump 设置
- 场景示例5:端口转发。本地转发(ssh -L 3306:localhost:3306 user@dbserver)、远程转发(ssh -R 8080:localhost:3000 user@public_server)与动态转发(ssh -D 1080 user@ssh_server)
Frequently Asked Questions
Related Skills
ssl-tls
chaterm/terminal-skills
SSL/TLS 证书
cron
chaterm/terminal-skills
定时任务管理
hardening
chaterm/terminal-skills
--- name: hardening description: 系统加固 version: 1.0.0 author: terminal-skills tags: [security, hardening, cis, baseline, sysctl] --- # 系统加固 ## 概述 系统加固、基线配置、CIS 标准技能。 ## SSH 加固 ### 配置优化 ```bash # /etc/ssh/sshd_config # 禁用 root 登录 PermitRootLogin no # 禁用密码认证 PasswordAuthentication no PubkeyAuthent
erpnext-permissions
OpenAEC-Foundation/ERPNext_Anthropic_Claude_Development_Skill_Package
Complete guide for Frappe/ERPNext permission system - roles, user permissions, perm levels, data masking, and permission hooks
CI/CD Pipeline Security Expert
martinholovsky/claude-skills-generator
Expert in CI/CD pipeline design with focus on secret management, code signing, artifact security, and supply chain protection for desktop application builds
AFL++ Fuzzing Testing
PramodDutta/qaskills
American Fuzzy Lop Plus Plus mutation-based fuzz testing for finding crashes, hangs, and security vulnerabilities in binary programs.