Linux命令-ssh(SSH 远程登录工具)
-
- 快速参考
- 基本连接
- SSH 配置文件
- 端口转发(SSH 隧道)
- 文件传输相关
- X11 转发与图形
- SSH 选项速查
- 常用组合
- 安全最佳实践
ssh(Secure Shell)是 Linux/Unix 世界中最核心的远程管理工具,用于安全地登录远程主机并执行命令。它通过加密隧道传输所有数据,替代了不安全的 telnet 和 rlogin,是现代系统管理和运维的基础设施级工具。
快速参考
ssh [选项] [用户@]主机 [命令]
ssh 默认使用 TCP 22 端口建立加密连接。首次连接会提示验证主机指纹,确认后存入 ~/.ssh/known_hosts。
基本连接
# 使用当前用户名连接
ssh remote_host
# 指定用户名连接
ssh admin@remote_host
# 指定端口
ssh -p 2222 user@remote_host
# 使用指定密钥连接
ssh -i ~/.ssh/custom_key user@remote_host
# 执行单条命令后退出(非交互模式)
ssh user@remote_host "df -h"
# 执行多条命令
ssh user@remote_host "cd /var/log && ls -la && tail -n 5 syslog"
# 执行本地脚本
ssh user@remote_host 'bash -s' < local_script.sh
# 压缩传输(低速网络下加速)
ssh -C user@remote_host
SSH 配置文件
~/.ssh/config 可以简化连接参数:
cat > ~/.ssh/config << 'EOF'
# 通用默认配置
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
StrictHostKeyChecking ask
ForwardAgent no
# 生产服务器别名
Host prod-web
HostName 203.0.113.10
User deploy
Port 2222
IdentityFile ~/.ssh/prod_ed25519
# 跳板机后的内网主机
Host internal-db
HostName 10.0.1.50
User admin
ProxyJump prod-web
# GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github
IdentitiesOnly yes
EOF
chmod 600 ~/.ssh/config
# 使用别名连接
ssh prod-web # 相当于 ssh -p 2222 -i ~/.ssh/prod_ed25519 deploy@203.0.113.10
ssh internal-db # 自动通过跳板机连接
端口转发(SSH 隧道)
# === 本地端口转发 (-L) ===
# 格式: ssh -L [本地地址:]本地端口:目标主机:目标端口 user@ssh_server
# 将本地 8080 转发到远程的 localhost:80
ssh -L 8080:localhost:80 user@remote_host
# 本地访问 http://localhost:8080 相当于远程 http://localhost:80
# 通过跳板机访问内网数据库
ssh -L 3306:internal_db:3306 user@jumphost
# 本地 mysql -h 127.0.0.1 -P 3306 即可连接内网数据库
# 绑定到特定本地地址
ssh -L 127.0.0.1:8080:localhost:80 user@host
# === 远程端口转发 (-R) ===
# 格式: ssh -R [远程地址:]远程端口:目标主机:目标端口 user@ssh_server
# 将远程 9090 转发到本地 localhost:3000
ssh -R 9090:localhost:3000 user@remote_host
# 远程主机上访问 localhost:9090 会转发到本地的 3000 端口
# 暴露本地开发服务器到公网
ssh -R 8080:localhost:80 serveo.net
# === SOCKS 代理 (-D) ===
# 在本地启动 SOCKS5 代理
ssh -D 1080 user@remote_host
# 配置浏览器 SOCKS 代理为 localhost:1080
# 配合程序使用
curl –socks5 localhost:1080 https://example.com
# === 动态端口转发 ===
# 多端口一次性转发
ssh -L 8080:localhost:80 -L 8443:localhost:443 user@host
文件传输相关
# 从本地传文件到远程
scp localfile.txt user@host:/path/to/destination/
# 从远程下载文件
scp user@host:/path/to/file.txt ./localfile.txt
# 传输目录(递归)
scp -r ./project/ user@host:/opt/
# rsync 同步目录(高效增量)
rsync -avz -e ssh ./local_dir/ user@host:/remote_dir/
# 通过管道传输数据
tar czf – /var/log | ssh user@host "tar xzf – -C /backup/"
# SFTP 模式
sftp user@host
X11 转发与图形
# 启用 X11 转发(远程 GUI 应用显示在本地)
ssh -X user@remote_host
# 登录后运行图形程序:
firefox &
xclock &
gedit &
# 可信 X11 转发(性能更好,但安全性略低)
ssh -Y user@remote_host
SSH 选项速查
| -p PORT | 指定 SSH 端口 |
| -i KEYFILE | 指定私钥文件 |
| -v / -vv / -vvv | 调试输出(逐级详细) |
| -L | 本地端口转发 |
| -R | 远程端口转发 |
| -D | SOCKS 代理 |
| -N | 不执行远程命令(仅转发) |
| -f | 后台运行 |
| -T | 禁用伪终端分配 |
| -C | 压缩数据传输 |
| -A | 启用 SSH Agent 转发 |
| -X / -Y | X11 转发 |
| -4 / -6 | 仅使用 IPv4/IPv6 |
| -o OPTION | 传递配置选项 |
| -q | 静默模式 |
常用组合
# 后台建立持久隧道
ssh -fNT -L 3306:db.internal:3306 user@jumphost
# 调试连接问题
ssh -vvv user@problematic_host
# 跳过多重跳板
ssh -J user@hop1,user@hop2 user@final_target
# 仅测试认证不执行命令
ssh -o ConnectTimeout=5 user@host echo "OK"
# 连接并自动退出(超时控制)
timeout 10 ssh user@host || echo "连接超时"
安全最佳实践
安全准则:
# 安全的 SSH Config 模板
cat >> ~/.ssh/config << 'EOF'
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
HashKnownHosts yes
IdentitiesOnly yes
PreferredAuthentications publickey
PasswordAuthentication no
Host production
HostName prod.example.com
User deploy
IdentityFile ~/.ssh/prod_ed25519
Port 2222
ForwardAgent no
StrictHostKeyChecking yes
EOF
ssh 已远远超越了简单的"远程 Shell"定位。其端口转发功能使其成为构建安全网络通道的瑞士军刀,而密钥认证和配置文件则让多服务器管理变得安全和高效。每个 Linux 从业者对 ssh 的理解深度,往往决定了其运维效率的上限。
网硕互联帮助中心



评论前必须登录!
注册