首頁·終端機命令

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