ssh — 安全远程登录

ssh 是通过加密连接远程登录和执行命令的标准工具。

命令说明

ssh 使用 SSH 协议建立安全的加密连接,可以进行远程登录、执行命令、端口转发、文件传输等操作。它是服务器管理和开发运维的基础工具。

应用场景

  • 远程登录服务器
  • 执行远程命令
  • 建立 SSH 隧道/端口转发
  • SCP/SFTP 文件传输
  • Git 操作(通过 SSH 密钥)
  • 代理跳转

常用示例

示例 1:远程登录

bash
复制代码
ssh user@remote.example.com

示例 2:使用非默认端口

bash
复制代码
ssh -p 2222 user@remote.example.com

示例 3:执行远程命令

bash
复制代码
ssh user@remote "ls -la /var/log"

示例 4:使用 SSH 密钥

bash
复制代码
ssh -i ~/.ssh/id_ed25519 user@remote.example.com

示例 5:本地端口转发

bash
复制代码
ssh -L 8080:localhost:80 user@remote.example.com

示例 6:远程端口转发

bash
复制代码
ssh -R 8080:localhost:3000 user@remote.example.com

示例 7:动态端口转发(SOCKS 代理)

bash
复制代码
ssh -D 1080 user@remote.example.com

示例 8:通过跳板机连接

bash
复制代码
ssh -J jump_user@jump.example.com target_user@target.example.com

示例 9:配置文件快速连接

bash
复制代码
Host myserver
    HostName 192.168.1.100
    User alice
    Port 22
    IdentityFile ~/.ssh/id_ed25519

ssh myserver

help 参数详解

参数 长参数 说明
-p PORT 指定端口(默认 22)
-i FILE --identity-file=FILE 指定私钥文件
-L [BIND:]PORT:HOST:HOSTPORT 本地端口转发
-R [BIND:]PORT:HOST:HOSTPORT 远程端口转发
-D PORT 动态端口转发(SOCKS)
-J HOST 指定跳板机
-v 详细模式(调试用)
-q 静默模式
-f 后台模式
-N 不执行远程命令
-o OPTION 设置配置选项
-X 启用 X11 转发
-A 启用代理转发
-4 使用 IPv4
-6 使用 IPv6
-C 启用压缩