首页·终端命令

ssh

网络

用法

ssh [-p <port>] <user>@<host> [command]

在另一台机器上打开一个加密的 shell;私钥文件只要别人也能读,ssh 就直接拒绝,所以要 `chmod 600`,而端口在这里写 `-p`,到了 scp 和 sftp 里却是 `-P`。

常用选项

选项含义
-p 2222Port. Note that scp and sftp spell the same thing -P.
-i ~/.ssh/id_ed25519Use a specific private key.
-L 8080:localhost:80Local forward: your port 8080 reaches the remote side.
-R 9000:localhost:3000Remote forward: their port 9000 reaches your machine.
-D 1080A local SOCKS proxy tunnelled through the host.
-J <bastion>Jump through a bastion host in one command.
-N -fNo remote command, go to the background. The pair used for tunnels.
-v / -vvvShow 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.com

Log in on a non-standard port.

ssh -N -L 5432:localhost:5432 deploy@db.example.com

Reach the remote database as if it were local.

ssh -vvv git@github.com

See 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