ssh
网络
用法
ssh [-p <port>] <user>@<host> [command]在另一台机器上打开一个加密的 shell;私钥文件只要别人也能读,ssh 就直接拒绝,所以要 `chmod 600`,而端口在这里写 `-p`,到了 scp 和 sftp 里却是 `-P`。
常用选项
| 选项 | 含义 |
|---|---|
| -p 2222 | Port. Note that scp and sftp spell the same thing -P. |
| -i ~/.ssh/id_ed25519 | Use a specific private key. |
| -L 8080:localhost:80 | Local forward: your port 8080 reaches the remote side. |
| -R 9000:localhost:3000 | Remote forward: their port 9000 reaches your machine. |
| -D 1080 | A local SOCKS proxy tunnelled through the host. |
| -J <bastion> | Jump through a bastion host in one command. |
| -N -f | No remote command, go to the background. The pair used for tunnels. |
| -v / -vvv | Show the handshake, which is how you find out why a key was refused. |
| -o <Option>=<value> | Any ssh_config setting for this one connection. |
示例
ssh -p 2222 deploy@example.comLog in on a non-standard port.
ssh -N -L 5432:localhost:5432 deploy@db.example.comReach the remote database as if it were local.
ssh -vvv git@github.comSee exactly which keys were offered and rejected.
连不上的时候,这些能把范围缩小——是名字解析不了、没有路由,还是端口被挡。
怎么看
- 方括号 [ ] 表示这部分可以不写。
- 省略号 … 表示可以写多个。
- 选项区分大小写——有些命令里 -r 和 -R 不是一回事。
常见问题
Q. ssh 是做什么的?
在另一台机器上打开一个加密的 shell;私钥文件只要别人也能读,ssh 就直接拒绝,所以要 `chmod 600`,而端口在这里写 `-p`,到了 scp 和 sftp 里却是 `-P`。
Q. 怎么写?
ssh [-p <port>] <user>@<host> [command] —— 方括号表示可以省略的部分。
Q. 值得记的选项有几个?
这里列了 9 个,完整列表在 man ssh。这条命令属于网络。
相关命令
man 手册: man ssh