SSH 与 OpenSSH 命令速查
OpenSSH 快速参考:连接、密钥管理、用户配置文件、文件传输(scp/rsync/sshfs)、端口转发与跳板机,并附带常用参数和实操示例。
26 条命令
连接
ssh <user>@<host>在远程主机上开启一个交互式 SSH 会话。
-p PORT;-i identity_file;-l user(覆盖 user@ 部分);-E logfile 调试日志;-v / -vv / -vvv 详细级别
ssh -p 2222 [email protected]ssh <user>@<host> '<cmd>'在远程主机上执行一条非交互式命令。
ssh ops@db1 'sudo systemctl status postgresql'ssh -J <jumphost>通过跳板机中转 SSH 连接(ProxyJump)。
-J user1@jump1:2222,user2@jump2;-o ProxyCommand=ssh -W ...
ssh -J [email protected] db1.private -i ~/.ssh/db1_keyssh -N -L <l:r> -f <host>后台本地端口转发,不开启 Shell;-L 本地端口:远端端口。
-N 不执行远端命令;-f 后台;-L 8080:localhost:80;-g 允许远端连到本地侧
ssh -N -L 5432:localhost:5432 -f db1 && psql -h localhostssh -N -R <r:localhost:l>反向隧道——在远端开放端口,转发回本地的端口。
-N;-f;-R 8080:localhost:3000;GatewayPorts yes(服务端配置)
ssh -N -R 8080:localhost:3000 [email protected]ssh -D <lport>在本地侧通过 SSH 服务器建立 SOCKS 代理。
-C 压缩;-q 静默;-D 1080
ssh -D 1080 -q -N tunnel.example.com && curl --proxy socks5h://localhost:1080 https://ifconfig.messh -A <user>@<host>将本地 ssh-agent 转发到远端主机(链式 SSH 时复用本地密钥)。
-A 开启 agent 转发;-a 关闭
ssh -A ops@bastion && ssh internal-db # 使用本地密钥ssh -o <key>=<value>在命令行传入 SSH 选项(适合临时调整,不必写入配置文件)。
StrictHostKeyChecking=no;UserKnownHostsFile=/dev/null(不安全,仅供演示);RequestTTY=force
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null [email protected]ssh -F <configfile>使用指定的 ssh_config 文件(适合按项目 / 客户隔离配置)。
-F 路径;-G 仅解析配置并打印,不实际连接
ssh -F ~/.ssh/config.work -G devbox | grep -E 'hostname|user'密钥
ssh-keygen -t ed25519 -C '<email>'生成现代且紧凑的 ED25519 密钥对(推荐优于 RSA)。
-t ed25519|rsa|ecdsa;-b 位数;-C 注释;-f 输出路径;-N '口令';-q 静默
ssh-keygen -t ed25519 -C 'you@work' -f ~/.ssh/work_ed25519 -N ''ssh-keygen -lf <pubkey>显示公钥的指纹与位数(用于审计 SSH banner)。
-E md5|sha256|shake256;-l 列出;-i 从 stdin 读取
ssh-keygen -lf ~/.ssh/id_ed25519.pub -E sha256ssh-keygen -R <host>从 known_hosts 中移除某个主机密钥(服务器重建 / 换密钥后)。
-R '[host]:port' 支持非默认端口
ssh-keygen -R '[bastion.example.com]:2222'ssh-copy-id <user>@<host>将本地公钥安装到远端 authorized_keys(需要先有密码登录)。
-i identity.pub;-p PORT;-o 选项
ssh-copy-id -i ~/.ssh/work_ed25519.pub -p 2222 deploy@bastionssh-add [path]将私钥加入当前 ssh-agent(避免每次重输口令)。
-l 列出指纹;-D 全部删除;-d path 删除某把;-t N 生命周期
eval $(ssh-agent) && ssh-add ~/.ssh/work_ed25519 && ssh-add -lssh-keyscan <host>打印远端主机密钥(用于预先植入 known_hosts / 引导)。
-t rsa|ed25519|ecdsa;-p PORT
ssh-keyscan -t ed25519 -p 2222 bastion.example.com >> ~/.ssh/known_hosts配置文件
~/.ssh/config用户级 SSH 配置——声明主机别名、身份、跳板机、心跳等。
Host 别名;HostName 真实地址;User;Port;IdentityFile;PreferredAuthentications;AddKeysToAgent yes;ServerAliveInterval 30;IdentitiesOnly yes;ProxyJump / ProxyCommand;LocalForward / RemoteForward;ControlMaster auto / ControlPath / ControlPersist
Host bastion
HostName bastion.example.com
Port 2222
User ops
IdentityFile ~/.ssh/work_ed25519
Host db*
ProxyJump bastion
User ubuntu文件传输
scp <src> <dest>基于 SSH 在主机之间复制文件(旧但仍广泛使用)。
-r 递归;-P PORT;-i 私钥;-C 压缩;-p 保留权限/时间;-l 限速(Kbit/s)
scp -P 2222 -r dist/ [email protected]:/srv/app/releases/1.4.2/rsync -avz -e ssh <src> <dest>增量同步 / 镜像——重复传输大目录时比 scp 快得多。
-a 归档;-v 详细;-z 压缩;-P 保留部分 + 显示进度;--delete 镜像;--exclude;--dry-run;-e ssh -p 2222
rsync -avz -e 'ssh -p 2222' ./build/ ops@bastion:/srv/app/current/sshfs <user>@<host>:<path> <mount>通过 SSH 挂载远端文件系统(FUSE)——用本地工具编辑远端文件。
-p PORT;-o reconnect;-o transform_symlinks;-C 压缩
sshfs ops@bastion:/srv/app ./remote -o reconnect,transform_symlinkssftp <user>@<host>通过 SSH 子系统进行交互式 / 脚本式文件传输。
sftp -i id -P port;命令:get / put / sync / ls / lls / cd / lcd / chmod
sftp ops@bastion <<'EOF'
mkdir /srv/app/releases/1.4.2
put -r dist/* /srv/app/releases/1.4.2/
EOF排障
ssh -vvv <host>追踪连接过程:KEX、认证、通道建立。判断“为何失败”的最佳手段。
-v / -vv / -vvv;-E file 写入文件
ssh -vvv -E /tmp/ssh.dbg ops@bastion 2>&1 | grep -E 'Authentications|method|Permission denied'ssh-keygen -y -f <private>从私钥派生对应的公钥(手头只有私钥时使用)。
-f 文件;-E 哈希算法
ssh-keygen -y -f ~/.ssh/id_ed25519 > /tmp/derived.pub && diff ~/.ssh/id_ed25519.pub /tmp/derived.pub && echo OKssh -o ConnectTimeout=<N> -o ConnectionAttempts=<K>调整连接超时 / 重试次数,应对网络抖动或冷启动跳板机。
ssh -o ConnectTimeout=5 -o ConnectionAttempts=2 ops@bastionssh -o ServerAliveInterval=30 -o ServerAliveCountMax=3通过 NAT / 防火墙时维持空闲连接不断开。
ssh -o ServerAliveInterval=30 -o ServerAliveCountMax=3 -L 5432:db:5432 -N ops@bastion代理转发
ssh -W %h:%p <proxy>作为 ProxyCommand 实现链式连接——ProxyJump 的旧式替代方案。
ProxyCommand ssh -W %h:%p myjumphost nc -q0 %h %p
ssh -o ProxyCommand='ssh -W %h:%p bastion.example.com' internal-db运维技巧
ssh -O stop / ssh -M -S <sock>ControlMaster——在多个终端间共享一条 SSH 会话以加速。
-M 主控模式;-S socket 路径;-O check|forward|exit
ssh -M -S ~/.ssh/cm-%h bastion; ssh -S ~/.ssh/cm-%h bastion -O check速查页版本 1.0.0
适用于 OpenSSH 9.0+