全部速查表

SSH 與 OpenSSH 命令速查

OpenSSH 快速參考:連線、金鑰管理、使用者設定檔、檔案傳輸(scp/rsync/sshfs)、埠轉發與跳板機——附常用參數與實作範例。

26 條命令

連線

ssh <user>@<host>

在遠端主機上開啟一個互動式 SSH 工作階段。

-p PORT;-i identity_file;-l user(覆寫 user@);-E logfile 除錯;-v / -vv / -vvv 詳細度

ssh -p 2222 [email protected]
ssh <user>@<host> '<cmd>'

在遠端主機上執行單一非互動命令。

ssh ops@db1 'sudo systemctl status postgresql'
ssh -J <jumphost>

透過跳板機繞送 SSH 連線(ProxyJump)。

-J user1@jump1:2222,user2@jump2;-o ProxyCommand=ssh -W ...

ssh -J [email protected] db1.private -i ~/.ssh/db1_key
ssh -N -L <l:r> -f <host>

背景執行本地埠轉發,不開啟 shell;-L 本地:r遠端。

-N 不執行遠端命令;-f 背景;-L 8080:localhost:80;-g 允許遠端連回本地側

ssh -N -L 5432:localhost:5432 -f db1 && psql -h localhost
ssh -N -R <r:localhost:l>

反向通道——在遠端開一個埠,把流量轉回本地埠。

-N;-f;-R 8080:localhost:3000;GatewayPorts yes(伺服器端設定)

ssh -N -R 8080:localhost:3000 [email protected]
ssh -D <lport>

透過 SSH 伺服器在本地建立 SOCKS 代理。

-C 壓縮;-q 安靜;-D 1080

ssh -D 1080 -q -N tunnel.example.com && curl --proxy socks5h://localhost:1080 https://ifconfig.me
ssh -A <user>@<host>

將本地的 ssh-agent 轉發到遠端主機(可在遠端再接力 SSH)。

-A 啟用 agent 轉發;-a 停用

ssh -A ops@bastion && ssh internal-db # 會沿用本機的金鑰
ssh -o <key>=<value>

臨時帶入 SSH 選項(適合一次性調整,不必寫進設定檔)。

StrictHostKeyChecking=no;UserKnownHostsFile=/dev/null(不安全,僅供測試);RequestTTY=force

ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null [email protected]
ssh -F <configfile>

使用指定的 ssh_config 檔(適合按專案/客戶分開設定)。

-F 路徑;-G 解析後輸出設定而不實際連線

ssh -F ~/.ssh/config.work -G devbox | grep -E 'hostname|user'

金鑰

ssh-keygen -t ed25519 -C '<email>'

產生一組現代且精簡的 ED25519 金鑰對(建議優先選用,取代 RSA)。

-t ed25519|rsa|ecdsa;-b 位元數;-C 註解;-f 輸出路徑;-N 'passphrase';-q 安靜

ssh-keygen -t ed25519 -C 'you@work' -f ~/.ssh/work_ed25519 -N ''
ssh-keygen -lf <pubkey>

顯示公鑰的指紋與長度(稽核 SSH banner 時使用)。

-E md5|sha256|shake256;-l 列表;-i 從 stdin 讀取

ssh-keygen -lf ~/.ssh/id_ed25519.pub -E sha256
ssh-keygen -R <host>

從 known_hosts 移除某個主機金鑰(伺服器重灌/換金鑰後用)。

-R '[host]:port' 支援非預設埠

ssh-keygen -R '[bastion.example.com]:2222'
ssh-copy-id <user>@<host>

把公鑰安裝到遠端的 authorized_keys(需要密碼登入)。

-i identity.pub;-p PORT;-o 選項

ssh-copy-id -i ~/.ssh/work_ed25519.pub -p 2222 deploy@bastion
ssh-add [path]

把私鑰加入執行中的 ssh-agent(之後不必再輸入 passphrase)。

-l 列出指紋;-D 刪除全部;-d path 移除指定;-t N 存活時間

eval $(ssh-agent) && ssh-add ~/.ssh/work_ed25519 && ssh-add -l
ssh-keyscan <host>

輸出遠端主機的金鑰(用於預先植入 known_hosts/bootstrap)。

-t rsa|ed25519|ecdsa;-p PORT

ssh-keyscan -t ed25519 -p 2222 bastion.example.com >> ~/.ssh/known_hosts

設定檔

~/.ssh/config

使用者級 SSH 設定檔——宣告主機、別名、身份、跳板、keepalive。

Host 別名;HostName 實體;User;Port;IdentityFile;PreferredAuthentications;AddKeysToAgent yes;ServerAliveInterval 30;IdentitiesOnly yes;ProxyJump / ProxyCommand;LocalForward / RemoteForward;ControlMaster auto / ControlPath / ControlPersist

Host bastion HostName bastion.example.com Port 2222 User ops IdentityFile ~/.ssh/work_ed25519 Host db* ProxyJump bastion User ubuntu

檔案傳輸

scp <src> <dest>

透過 SSH 在主機間複製檔案(舊式但隨處可見)。

-r 遞迴;-P PORT;-i 身份檔;-C 壓縮;-p 保留權限/時間;-l 限制頻寬(Kbit/s)

scp -P 2222 -r dist/ [email protected]:/srv/app/releases/1.4.2/
rsync -avz -e ssh <src> <dest>

差異同步的鏡像/複製——重複傳大樹時比 scp 快很多。

-a 歸檔;-v 詳細;-z 壓縮;-P 保留部分檔+進度;--delete 鏡像;--exclude;--dry-run;-e ssh -p 2222

rsync -avz -e 'ssh -p 2222' ./build/ ops@bastion:/srv/app/current/
sshfs <user>@<host>:<path> <mount>

透過 SSH 掛載遠端檔案系統(FUSE)——用本地工具直接編輯遠端檔案。

-p PORT;-o reconnect;-o transform_symlinks;-C 壓縮

sshfs ops@bastion:/srv/app ./remote -o reconnect,transform_symlinks
sftp <user>@<host>

透過 SSH 子系統做互動式/腳本化檔案傳輸。

sftp -i id -P port;指令:get / put / sync / ls / lls / cd / lcd / chmod

sftp ops@bastion <<'EOF' mkdir /srv/app/releases/1.4.2 put -r dist/* /srv/app/releases/1.4.2/ EOF

故障排除

ssh -vvv <host>

追蹤連線過程:KEX、認證、通道建立。「為什麼連不上?」時首選。

-v / -vv / -vvv;-E file 寫入檔案

ssh -vvv -E /tmp/ssh.dbg ops@bastion 2>&1 | grep -E 'Authentications|method|Permission denied'
ssh-keygen -y -f <private>

從私鑰反推出對應的公鑰(手上只有私鑰時使用)。

-f 檔案;-E 雜湊演算法

ssh-keygen -y -f ~/.ssh/id_ed25519 > /tmp/derived.pub && diff ~/.ssh/id_ed25519.pub /tmp/derived.pub && echo OK
ssh -o ConnectTimeout=<N> -o ConnectionAttempts=<K>

調整連線逾時/重試次數,適合網絡不穩或跳板機需熱身的場景。

ssh -o ConnectTimeout=5 -o ConnectionAttempts=2 ops@bastion
ssh -o ServerAliveInterval=30 -o ServerAliveCountMax=3

讓閒置的 SSH 連線穿過 NAT/防火牆持續保活。

ssh -o ServerAliveInterval=30 -o ServerAliveCountMax=3 -L 5432:db:5432 -N ops@bastion

跳板轉發

ssh -W %h:%p <proxy>

作為 ProxyCommand 串接連線——ProxyJump 的傳統替代方案。

ProxyCommand ssh -W %h:%p myjumphost nc -q0 %h %p

ssh -o ProxyCommand='ssh -W %h:%p bastion.example.com' internal-db

維護

ssh -O stop / ssh -M -S <sock>

ControlMaster——多個終端機共用單一 SSH 工作階段,速度更快。

-M master 模式;-S socket 路徑;-O check|forward|exit

ssh -M -S ~/.ssh/cm-%h bastion; ssh -S ~/.ssh/cm-%h bastion -O check

速查頁版本 1.0.0

適用於 OpenSSH 9.0+