SSH(Secure Shell)具有客戶端/服務器體系結構。OpenSSH支持SSH協議1和協議2,本文忽略前者。
ssh |
客戶端 |
~/.ssh/config |
sshd |
服務器 |
/etc/ssh/sshd_config |
認證(Authentication)
5種認證方式?
- GSSAPI-based authentication
- host-based authentication
- public key authentication
- challenge-response authentication
- password authentication
在ssh的配置文件中,通過 PreferredAuthentiactions可以更改優先順序,默認值
gssapi-with-mic, hostbased, publickey, keyboard-interactive, password
GSSAPI-based authentication
GSSAPI: Generic Security Services Application Program Interface
sshd配置選項:
GSSAPIAuthentication |
是否啓用,默認no |
GSSAPIKeyExchange |
|
... |
... |
基於主機的認證(host-based authentication)
在sshd的配置文件中,相關的選項:
HostbasedAuthentication |
是否啓用,默認爲 no |
HostbasedUsesNameFromPacketOnly |
默認爲 no |
IgnoreRhosts |
是否忽略 .rhost和 .shosts ,默認爲yes |
IgnoreUserKnownHosts |
是否忽略 ~/.ssh/known_hosts,默認爲no |
如果客戶端主機名存在於服務器中的
- /etc/hosts.equiv
- /etc/shosts.equiv
- ~/.rhosts
- ~/.shosts
文件內,
而且客戶端的host key存在於服務器中的:
- /etc/ssh/ssh_known_hosts
- ~/.ssh/known_hosts
文件內
認證通過。
基於公鑰的認證(public key authentication)
在sshd的配置文件中,相關的選項:
PubkeyAuthentication |
是否啓用,默認爲yes |
RevokedKeys |
指定一個文件,包含所有被廢棄的key |
AuthorizedKeysFile |
指定用來進行認證的公鑰文件,默認 .ssh/authorized_keys |
服務器文件:
- ~/.ssh/authorized_keys
包含允許登錄的公鑰
客戶端通過 ssh-keygen 生成公鑰密鑰對(有3種算法可用):
DSA |
~/.ssh/id_dsa |
~/.ssh/id_dsa.pub |
RSA |
~/.ssh/id_rsa |
~/.ssh/id_rsa.pub |
ECDSA |
~/.ssh/id_ecdsa |
~/.ssh/id_ecdas.pub |
工具ssh-copy-id可用來將公鑰拷貝到服務器的 ~/.ssh/authorized_keys 文件中:
ssh-copy-id -i ~/.ssh/id_dsa.pub [email protected]
使用這種方式時,ssh-agent通常會被使用。
certificate authentication
這是 public key authentication 的一個變體,相關配置:
TrustedUserCAKeys |
AuthorizedPrincipalsFile |
Challenge-response authentication
sshd配置選項
ChallengeResponseAuthentication |
是否允許,默認yes(debian系默認no) |
UsePAM |
是否使用PAM(Pluggable Authentication Module) |
服務器發送任意的challenge文字,客戶端解碼後送回。
例子:
- BSD Authentication
- PAM (非BSD系統)
基於密碼的認證(password authentication)
如果其他的都失敗了,將採用該方式,sshd配置選項
PasswordAuthentication |
是否啓用,默認yes |
PermitEmptyPasswords |
是否允許空密碼,默認no |
UsePAM |
是否使用PAM |
ssh-agent
使用 ssh-keygen 生成公鑰密鑰對時,一般會選擇一個passphrase對本地的密鑰進行加密。這樣以來,每次使用到密鑰時,都需要輸入這個passphrase,挺麻煩。
ssh-agent可以稍微減輕一點這種痛苦:
ssh-agent 是一個守護進程(daemon),設計任務就是對密鑰進行高速緩存。
運行ssh-agent,它啓動時會有如下的輸出:
debao@ubuntu~$ ssh-agent SSH_AUTH_SOCK=/tmp/ssh-jeQdoYU11925/agent.11925; export SSH_AUTH_SOCK; SSH_AGENT_PID=11926; export SSH_AGENT_PID; echo Agent pid 11926;
可以看出:這是shell命令,它會設置2個環境變量。但我們需要用eval執行它。一般來說,都合併到一塊進行
debao@ubuntu:~$ eval `ssh-agent` Agent pid 12385
而後,使用 ssh-add 密鑰加入ssh-agent的緩存:
$ ssh-add ~/.ssh/id_dsa Enter passphrase for /home/debao/.ssh/id_dsa: Identity added: /home/debao/.ssh/id_dsa (/home/debao/.ssh/id_dsa)