📖 SSH/SCP 远程操作速查

按使用场景分组,点击"复制"按钮一键复制命令

登录与免密
密码登录(指定端口与用户)
ssh -p <port> <user>@<host>
生成密钥对(推荐 ed25519)
ssh-keygen -t ed25519 -C "<email>"
一键配置免密登录(推送公钥)
ssh-copy-id -i ~/.ssh/id_ed25519.pub <user>@<host>
手动追加公钥到服务器
cat ~/.ssh/id_ed25519.pub >> ~/.ssh/authorized_keys
修正免密不生效的权限问题
chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys
指定私钥登录
ssh -i ~/.ssh/<key> <user>@<host>
文件传输(scp/rsync)
上传文件到远端
scp <file> <user>@<host>:/path/
从远端下载文件
scp <user>@<host>:/path/<file> ./
递归传输目录
scp -r <dir> <user>@<host>:/path/
指定端口传输
scp -P <port> <file> <user>@<host>:/path/
rsync 增量同步(显示进度)
rsync -avzP <dir>/ <user>@<host>:/path/
rsync 断点续传大文件
rsync -avP --partial <file> <user>@<host>:/path/
端口转发
本地转发:本地 8080 → 远端内网服务
ssh -L 8080:<内网host>:80 <user>@<host>
远程转发:把本地服务暴露到远端
ssh -R 9090:localhost:3000 <user>@<host>
动态转发(SOCKS5 代理)
ssh -D 1080 <user>@<host>
后台转发不开 shell
ssh -fNL 8080:<内网host>:80 <user>@<host>
跳板机与多跳
经跳板机连目标机(-J)
ssh -J <jump-user>@<jump-host> <user>@<target>
多级跳板
ssh -J <jump1>,<jump2> <user>@<target>
经跳板机传文件
scp -o ProxyJump=<jump-user>@<jump-host> <file> <user>@<target>:/path/
配置别名简化(~/.ssh/config)
Host web / HostName <ip> / ProxyJump <jump>
排错与 known_hosts
详细输出定位登录失败
ssh -v <user>@<host>(更深用 -vvv)
删除单个主机的 known_hosts 记录
ssh-keygen -R <host>
跳过主机密钥确认 ⚠️(仅限临时环境)
ssh -o StrictHostKeyChecking=no <user>@<host>
保持连接不断开
ssh -o ServerAliveInterval=60 <user>@<host>
查看公钥指纹(核对中间人告警)
ssh-keygen -lf ~/.ssh/id_ed25519.pub