ssh 是通过加密连接远程登录和执行命令的标准工具。
ssh 使用 SSH 协议建立安全的加密连接,可以进行远程登录、执行命令、端口转发、文件传输等操作。它是服务器管理和开发运维的基础工具。
示例 1:远程登录
ssh user@remote.example.com
示例 2:使用非默认端口
ssh -p 2222 user@remote.example.com
示例 3:执行远程命令
ssh user@remote "ls -la /var/log"
示例 4:使用 SSH 密钥
ssh -i ~/.ssh/id_ed25519 user@remote.example.com
示例 5:本地端口转发
ssh -L 8080:localhost:80 user@remote.example.com
示例 6:远程端口转发
ssh -R 8080:localhost:3000 user@remote.example.com
示例 7:动态端口转发(SOCKS 代理)
ssh -D 1080 user@remote.example.com
示例 8:通过跳板机连接
ssh -J jump_user@jump.example.com target_user@target.example.com
示例 9:配置文件快速连接
Host myserver
HostName 192.168.1.100
User alice
Port 22
IdentityFile ~/.ssh/id_ed25519
ssh myserver
| 参数 | 长参数 | 说明 |
|---|---|---|
-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 |
启用压缩 |