SSH2 vs OpenSSH

常見的 SSH Implementation 有兩種,ssh.com 的 SSH 以及 OpenSSH,詳細發展緣由可參考 OpenSSH History

由於歷史因素,所以提到 SSH 相關用語時,常因不明確而混淆。建議可以參考 O’Reilly book 出版的 SSH, The Secure Shell: The Definitive Guide 一書中對於 SSH 各種詞彙的精確定義:

Terminology: SSH Protocols and Products

SSH
    A generic term referring to SSH protocols or software products.
SSH-1
    The SSH protocol, Version 1. This protocol went through several
    revisions, of which 1.3 and 1.5 are the best known, and we will
    write SSH-1.3 and SSH-1.5 should the distinction be necessary.
SSH-2
    The SSH protocol, Version 2, as defined by several draft standards
    documents of the IETF SECSH working group.
SSH1
    Tatu Ylönen's software implementing the SSH-1 protocol; the original
    SSH. Now distributed and maintained (minimally) by SSH
    Communications Security, Inc.
SSH2
    The "SSH Secure Shell" product from SSH Communications Security, Inc.
    This is a commercial SSH-2 protocol implementation, though it is
    licensed free of charge in some circumstances.
ssh (all lowercase letters)
    A client program included in SSH1, SSH2, OpenSSH, F-Secure SSH, and
    other products, for running secure terminal sessions and remote
    commands. In SSH1 and SSH2, it is also named ssh1/ssh2, respectively.
OpenSSH
    The product OpenSSH from the OpenBSD project,
    which implements both the SSH-1 and SSH-2 protocols.
OpenSSH/1
    OpenSSH, referring specifically to its behavior
    when using the SSH-1 protocol.
OpenSSH/2
    OpenSSH, referring specifically to its behavior
    when using the SSH-2 protocol.

OpenSSH 基於相容 BSD license 的 ssh 1.2.12 來開發,後續又加上了對 SSH-2 的支援

但 OpenSSH 對 SSH-2 的實作與 ssh.com 後來對 SSH-2 的實作已經有了一些差異。

現今大部分的 open source 作業系統都已經改用 OpenSSH 了,不過有些地方還是可以看到 SSH2 的蹤跡,可能是商業版本或是非商業版本,例如:

# ssh -V
ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu

SSH Communications Security, Inc. (www.ssh.com) 的商業版本 SSH (SSH2) 正式名稱叫做 Tectia SSH client/server

熟悉 OpenSSH 的人初次接觸 SSH2 最常遇到的問題就是 Public-Key Authentication 的作法以及 Key format 都不太一樣。

OpenSSH/1 與 SSH1 的 authorization file 都是放在 ~/.ssh/authorized_keys 這個檔案之內,identity 則是放在 ~/.ssh/identity 這個檔案內

OpenSSH/2 的 authorization file 一樣是 ~/.ssh/authorized_keys,identity 則是在 ~/.ssh/id_dsa 或 ~/.ssh/id_rsa 內,依據 key type 而異。但其實因為 OpenSSH/2 有向下相容 OpenSSH/1 的功能,所以事實上也會參考 ~/.ssh/identity 來當作 SSH-1 的 identity

SSH2 就很不一樣了。SSH2 允許有多個 identity,因此會有一個檔案 ~/.ssh/identification 可以設定有哪些 identity 可用,例如:

IdKey identity-1
IdKey identity-2
IdKey identity-3

真正的 private key 放在 identity-1, identity-2, identity-3 這三個檔案內,而使用 ssh -i 指定 identity 時,不同於 OpenSSH 是直接指到 key 本身,SSH2 必須指定這個間接的 identification file

SSH2 的 public key format 也與 OpenSSH 不同,不再是一行而已,也因此 authorization file 沒辦法跟 ~/.ssh/authorized_keys 一樣一行放一個 key 值。

SSH2 也是一樣使用一個檔案來設定允許的 public key 有哪些,檔案放在 ~/.ssh/authorization

Key identity-1.pub
Key identity-2.pub
Key identity-3.pub

這個檔案表示可接受三個 public key 分別為 identity-1.pub, identity-2.pub, identity-3.pub

而如同 OpenSSH 一樣,SSH2 的 authorization 檔案內也可以指定各種 options,例如:

Key identity-1.pub
Options no-port-forwarding,no-pty
Key identity-2.pub
Options command="rsync -az --server . /home/backup-x",no-pty

詳細資訊可參考 SSH2 的 manual

SSH2 的 key format 與 OpenSSH 不同,這在兩者之間要作 public key authentication 時造成了一些麻煩。OpenSSH 提供的 ssh-keygen 可以用 -e 和 -i 兩個選項來轉換兩者的 key format:

# ssh-keygen -e -f openssh-key.pub > ssh2-key.pub
# ssh-keygen -i -f ssh2-key.pub > openssh-key.pub

OpenSSH 與 SSH2 之間的問題還不只這樣,SCP 的實作方式也不同

OpenSSH 與 SSH1 的 SCP 是 RCP over SSH,而 SSH2 附的 SCP2 背地裡則是用 SFTP 實作的,因此兩者互相 copy 東西時有時會 copy 不過去,建議還是一律改用有經過 IETF 標準認證的 SFTP 就好了

如果可以的話,全部用 OpenSSH 是最單純的,不過有時不是自己能控制的,碰到使用 SSH2 的系統,其實只要稍微了解一下差異也很容易適應就是了

參考資料:

在〈SSH2 vs OpenSSH〉中有 3 則留言

  1. 自動引用通知: Public Key Authentication between OpenSSH and SSH2 « It rains.

沈覓仁 發表迴響取消回覆