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