在 SSH 中使用 RSA 和 DSA 認證(詳解)
[原貼]https://weblog.kreny.com/archives/2005/10/rsadsa_authenti.html [作者]:kreny
一直想把自己的服務器的 SSH 認證的模式從用戶名密碼模式轉換成 RSA 和 DSA 認證協議,借著OpenSSH 4.2的發布,今天寫了一下配置過程並收集了一些關於 RSA 和 DSA 的參考文章。
思路整理:
一直沒有理解公匙模式下的認證過程,這裏總結一下公匙和秘匙的製作和安置方法。以下假設一台服務器Server和一台PC作為例子。
首先,並不是在服務器上生成公匙和秘匙。因為很多關於RSA 和 DSA 認證協議的文章都使用Linux服務器作為道具,自然也就使用諸如 ssh-keygen -t rsa 之類的命令,而使得我總是誤認為要先在Linux服務器上先生成公匙和秘匙。而真正的安置方法是:
當從PC連接Server的時候,需要在PC上保存一對公匙和秘匙(這對公匙和秘匙可以用諸如PenguiNet之類的工具生成),而隻要把生成的公匙傳到Server上即可。而往往在Server上,公匙是被放在 ~/.ssh/authorized_keys 這個文件中。這個文件的設置可以在 /etc/ssh/sshd_config 中找到。
AuthorizedKeysFile .ssh/authorized_keys那麼當從一個Linux Client Server連接到另外一個Linux Server的時候,我們應該在 Linux Client Server 上生成一對秘匙(RSA時默認為 id_rsa 和 id_rsa.pub),保存在 ~/.ssh/ 中, 這個設置可以在 /etc/sshd/ssh_config 中設置:
# IdentityFile ~/.ssh/identity這樣我們也可以更好地理解 ssh_config 和 sshd_config 的區別了。
# IdentityFile ~/.ssh/id_rsa
# IdentityFile ~/.ssh/id_dsa

RAS/DSA認證安裝過程(以 tenten 用戶為例):
()
[root@domain ~]$su - tenten查看生成的文件:
[tenten@domain ~]ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/tenten/.ssh/id_rsa):[Enter]
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/tenten/.ssh/id_rsa.
Your public key has been saved in /home/tenten/.ssh/id_rsa.pub.
The key fingerprint is:
c7:93:83:c4:24:30:56:90:37:a0:eb:a7:5d:4c:8d:ea:1a: tenten@example.com:::或者:::
[tenten@kdx ~]$ ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/home/tenten/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/tenten/.ssh/id_dsa.
Your public key has been saved in /home/tenten/.ssh/id_dsa.pub.
The key fingerprint is:
a2:be:16:2e:66:e4:69:68:a0:eb:a7:5d:4c:8d:ea:1a:da:54:35:55:32:8e:e2 tenten@example.com
[tenten@kdx ~]$ ls /home/tenten/.ssh/ -la設置 sshd_config 文件,去除密碼認證
total 28
drwx------ 2 tenten tenten 4096 Oct 11 16:09 .
drwx------ 3 tenten tenten 4096 Oct 9 16:50 ..
-rw------- 1 tenten tenten 736 Oct 11 16:09 id_dsa
-rw-r--r-- 1 tenten tenten 612 Oct 11 16:09 id_dsa.pub
-rw------- 1 tenten tenten 951 Oct 11 16:03 id_rsa
-rw-r--r-- 1 tenten tenten 232 Oct 11 16:03 id_rsa.pub
-rw-r--r-- 1 tenten tenten 667 Oct 9 16:48 known_hosts
# To disable tunneled clear text passwords, change to no here!由於在 sshd_config 文件裏麵,我們設置了以下內容:
PasswordAuthentication no
#AuthorizedKeysFile .ssh/authorized_keys所以我們要把共匙重命名為 autherized_keys
[root@domain .ssh]# mv id_dsa.pub autherized_keys所以最終服務器端 ~/.ssh/ 目錄下的內容為(注意authorized_keys的權限為 644)
[tenten@domain .ssh]$ ls -la
total 12
drwx------ 2 tenten tenten 4096 Oct 11 19:57 .
drwx------ 3 tenten tenten 4096 Oct 11 18:50 ..
-rw-r--r-- 1 tenten tenten 232 Oct 11 19:46 authorized_keys
引申:
在安裝 openssh 的最後,輸入 make install 命令後,會發現最後會生成 public/private key:
Generating public/private key pair.
Your identification has been saved in /usr/local/etc/ssh_host_key.
Your public key has been saved in /usr/local/etc/ssh_host_key.pub.
The key fingerprint is:
22:67:00:5f:82:87:ab:22:e7:8e:cd:bb:d2:07:98:57 root@example.comGenerating public/private key pair.
Your identification has been saved in /usr/local/etc/ssh_host_dsa_key.
Your public key has been saved in /usr/local/etc/ssh_host_dsa_key.pub.
The key fingerprint is:
17:6c:d8:6f:31:db:bd:3c:66:81:86:12:13:a4:33:a3 root@example.comGenerating public/private key pair.
Your identification has been saved in /usr/local/etc/ssh_host_rsa_key.
Your public key has been saved in /usr/local/etc/ssh_host_rsa_key.pub.
The key fingerprint is:
27:0e:16:41:f8:96:ed:93:b6:a8:61:74:fe:87:e2:91 root@example.com
/usr/local/sbin/sshd -t -f /usr/local/etc/sshd_config
參考文檔:
張微波:linux下SSH配合SecureCRT的密匙完美使用方法
Daniel Robbins: 通用線程: OpenSSH 密鑰管理,第 1 部分
OpenSSH 的 RSA 和 DSA 認證協議的基礎是一對專門生成的密鑰,分別叫做專用密鑰和公用密鑰。使用這些基於密鑰的認證係統的優勢在於:在許多情況下,有可能不必手工輸入密碼就能建立起安全的連接。
英文原文:Common threads: OpenSSH key management, Part 1
李洋:使用SSH實現Linux下的安全數據傳輸
ssh-keygen Description
ssh-keygen generates, manages and converts authentication keys for ssh(1). ssh-keygen can create RSA keys for use by SSH protocol version 1 and RSA or DSA keys for use by SSH protocol version 2. The type of key to be generated is specified with the -t option.
通用線程: OpenSSH 密鑰管理,第 1 部分
RSA/DSA 密鑰的工作原理
下麵從整體上粗略的介紹了 RSA/DSA 密鑰的工作原理。讓我們從一種假想的情形開始,假定我們想用 RSA 認證允許一台本地的 Linux 工作站(稱作 localbox)打開 remotebox上的一個遠程 shell, remotebox 是我們的 ISP 的一台機器。此刻,當我們試圖用 ssh 客戶程序連接到 remotebox時,我們會得到如下提示:% ssh drobbins@remotebox此處我們看到的是 ssh 處理認證的 缺省方式的一個示例。換句話說,它要求我們輸入 remotebox上的 drobbins 這個帳戶的密碼。如果我們輸入我們在 remotebox 上的密碼, ssh 就會用安全密碼認證協議,把我們的密碼傳送給 remotebox 進行驗證。但是,和 telnet 的情況不同,這裏我們的密碼是加密的,因此它不會被偷看到我們的數據連接的人截取。一旦 remotebox 把我們提供的密碼同它的密碼數據庫相對照進行認證,成功的話,我們就會被允許登錄,還會有一個 remotebox 的 shell 提示歡迎我們。雖然 ssh 缺省的認證方法相當安全,RSA 和 DSA 認證卻為我們開創了一些新的潛在的機會。
drobbins@remotebox's password:但是,與 ssh 安全密碼認證不同的是,RSA 認證需要一些初始配置。我們隻需要執行這些初始配置步驟一次。之後, localbox 和 remotebox 之間的 RSA 認證就毫不費力了。要設置 RSA 認證,我們首先得生成一對密鑰,一把專用密鑰和一把公用密鑰。這兩把密鑰有一些非常有趣的性質。公用密鑰用於對消息進行加密,隻有擁有專用密鑰的人才能對該消息進行解密。公用密鑰隻能用於 加密,而專用密鑰隻能用於對由匹配的公用密鑰編碼的消息進行 解密。RSA(和 DSA)認證協議利用密鑰對的這些特殊性質進行安全認證,並且不需要在網上傳輸任何保密的信息。
要應用 RSA 或者 DSA 認證,我們要執行一步一次性的配置步驟。我們把 公用密鑰拷貝到 remotebox。公用密鑰之所以被稱作是“公用的”有一個原因。因為它隻能用於對那些給我們的消息進行加密,所以我們不需要太擔心它會落入其它人手中。一旦我們的公用密鑰已經被拷貝到 remotebox並且為了 remotebox 的 sshd 能夠定位它而把它放在一個專門的文件(~/.ssh/authorized_keys)裏,我們就為使用 RSA 認證登錄到 remotebox上做好了準備。
要用 RSA 登錄的時候,我們隻要在 localbox 的控製台鍵入 ssh drobbins@remotebox ,就象我們常做的一樣。可這一次, ssh 告訴 remotebox 的 sshd 它想使用 RSA 認證協議。接下來發生的事情非常有趣。 Remotebox 的 sshd 會生成一個隨機數,並用我們先前拷貝過去的公用密鑰對這個隨機數進行加密。然後, sshd 把加密了的隨機數發回給正在 localbox 上運行的 ssh 。接下來,輪到我們的 ssh 用 專用密鑰對這個隨機數進行解密後,再把它發回給 remotebox,實際上等於在說:“瞧,我 確實有匹配的專用密鑰;我能成功的對您的消息進行解密!”最後, sshd 得出結論,既然我們持有匹配的專用密鑰,就應當允許我們登錄。因此,我們有匹配的專用密鑰這一事實授權我們訪問 remotebox。
ssh 協議的版本 1 使用的是 RSA 密鑰,而 DSA 密鑰卻用於協議級 2,這是 ssh 協議的最新版本。目前所有的 OpenSSH 版本都應該既能使用 RSA 密鑰又能使用 DSA 密鑰。DSA 密鑰以如下類似於 RSA 密鑰的方式使用 OpenSSH 的 ssh-keygen 生成:
ssh_config 和 sshd_config 的設置說明
配置“/etc/ssh/ssh_config”文件 “/etc/ssh/ssh_config”文件是OpenSSH 係統範圍的配置文件,允許你通過設置不同的選項來改變客戶端程序的運行方式。這個文件的每一行包含“關鍵詞-值”的匹配,其中“關鍵詞”是忽略大小寫的。下麵列出來的是最重要的關鍵詞,用man命令查看幫助頁(ssh (1))可以得到詳細的列表。
編輯“ssh_config”文件(vi /etc/ssh/ssh_config),添加或改變下麵的參數:
# Site-wide defaults for various options
Host *
ForwardAgent no
ForwardX11 no
RhostsAuthentication no
RhostsRSAAuthentication no
RSAAuthentication yes
PasswordAuthentication yes
FallBackToRsh no
UseRsh no
BatchMode no
CheckHostIP yes
StrictHostKeyChecking no
IdentityFile ~/.ssh/identity
Port 22
Cipher blowfish
EscapeChar ~
下麵逐行說明上麵的選項設置:
Host *
選項“Host”隻對能夠匹配後麵字串的計算機有效。“*”表示所有的計算機。
ForwardAgent no
“ForwardAgent”設置連接是否經過驗證代理(如果存在)轉發給遠程計算機。
ForwardX11 no
“ForwardX11”設置X11連接是否被自動重定向到安全的通道和顯示集(DISPLAY set)。
RhostsAuthentication no
“RhostsAuthentication”設置是否使用基於rhosts的安全驗證。
RhostsRSAAuthentication no
“RhostsRSAAuthentication”設置是否使用用RSA算法的基於rhosts的安全驗證。
RSAAuthentication yes
“RSAAuthentication”設置是否使用RSA算法進行安全驗證。
PasswordAuthentication yes
“PasswordAuthentication”設置是否使用口令驗證。
FallBackToRsh no
“FallBackToRsh”設置如果用ssh連接出現錯誤是否自動使用rsh。
UseRsh no
“UseRsh”設置是否在這台計算機上使用“rlogin/rsh”。
BatchMode no
“BatchMode”如果設為“yes”,passphrase/password(交互式輸入口令)的提示將被禁止。當不能交互式輸入口令的時候,這個選項對腳本文件和批處理任務十分有用。
CheckHostIP yes
“CheckHostIP”設置ssh是否查看連接到服務器的主機的IP地址以防止DNS欺騙。建議設置為“yes”。
StrictHostKeyChecking no
“StrictHostKeyChecking”如果設置成“yes”,ssh就不會自動把計算機的密匙加入“$HOME/.ssh/known_hosts”文件,並且一旦計算機的密匙發生了變化,就拒絕連接。
IdentityFile ~/.ssh/identity
“IdentityFile”設置從哪個文件讀取用戶的RSA安全驗證標識。
Port 22
“Port”設置連接到遠程主機的端口。
Cipher blowfish
“Cipher”設置加密用的密碼。
EscapeChar ~
“EscapeChar”設置escape字符。
配置“/etc/ssh/sshd_config”文件 “/etc/ssh/sshd_config” 是OpenSSH的配置文件,允許設置選項改變這個daemon的運行。這個文件的每一行包含“關鍵詞-值”的匹配,其中“關鍵詞”是忽略大小寫的。下麵列出來的是最重要的關鍵詞,用man命令查看幫助頁(sshd (8))可以得到詳細的列表。
編輯“sshd_config”文件(vi /etc/ssh/sshd_config),加入或改變下麵的參數:
# This is ssh server systemwide configuration file. Port 22 ListenAddress 192.168.1.1 HostKey /etc/ssh/ssh_host_key ServerKeyBits 1024 LoginGraceTime 600 KeyRegenerationInterval 3600 PermitRootLogin no IgnoreRhosts yes IgnoreUserKnownHosts yes StrictModes yes X11Forwarding no PrintMotd yes SyslogFacility AUTH LogLevel INFO RhostsAuthentication no RhostsRSAAuthentication no RSAAuthentication yes PasswordAuthentication yes PermitEmptyPasswords no AllowUsers admin
下麵逐行說明上麵的選項設置:
Port 22
“Port”設置sshd監聽的端口號。
ListenAddress 192.168.1.1
“ListenAddress”設置sshd服務器綁定的IP地址。
HostKey /etc/ssh/ssh_host_key
“HostKey”設置包含計算機私人密匙的文件。
ServerKeyBits 1024
“ServerKeyBits”定義服務器密匙的位數。
LoginGraceTime 600
“LoginGraceTime”設置如果用戶不能成功登錄,在切斷連接之前服務器需要等待的時間(以秒為單位)。
KeyRegenerationInterval 3600
“KeyRegenerationInterval”設置在多少秒之後自動重新生成服務器的密匙(如果使用密匙)。重新生成密匙是為了防止用盜用的密匙解密被截獲的信息。
PermitRootLogin no
“PermitRootLogin”設置root能不能用ssh登錄。這個選項一定不要設成“yes”。
IgnoreRhosts yes
“IgnoreRhosts”設置驗證的時候是否使用“rhosts”和“shosts”文件。
IgnoreUserKnownHosts yes
“IgnoreUserKnownHosts”設置ssh daemon是否在進行RhostsRSAAuthentication安全驗證的時候忽略用戶的“$HOME/.ssh/known_hosts”
StrictModes yes
“StrictModes”設置ssh在接收登錄請求之前是否檢查用戶家目錄和rhosts文件的權限和所有權。這通常是必要的,因為新手經常會把自己的目錄和文件設成任何人都有寫權限。
X11Forwarding no
“X11Forwarding”設置是否允許X11轉發。
PrintMotd yes
“PrintMotd”設置sshd是否在用戶登錄的時候顯示“/etc/motd”中的信息。
SyslogFacility AUTH
“SyslogFacility”設置在記錄來自sshd的消息的時候,是否給出“facility code”。
LogLevel INFO
“LogLevel”設置記錄sshd日誌消息的層次。INFO是一個好的選擇。查看sshd的man幫助頁,已獲取更多的信息。
RhostsAuthentication no
“RhostsAuthentication”設置隻用rhosts或“/etc/hosts.equiv”進行安全驗證是否已經足夠了。
RhostsRSAAuthentication no
“RhostsRSA”設置是否允許用rhosts或“/etc/hosts.equiv”加上RSA進行安全驗證。
RSAAuthentication yes
“RSAAuthentication”設置是否允許隻有RSA安全驗證。
PasswordAuthentication yes
“PasswordAuthentication”設置是否允許口令驗證。
PermitEmptyPasswords no
“PermitEmptyPasswords”設置是否允許用口令為空的帳號登錄。
AllowUsers admin
“AllowUsers”的後麵可以跟著任意的數量的用戶名的匹配串(patterns)或user@host這樣的匹配串,這些字符串用空格隔開。主機名可以是DNS名或IP地址。
配置OpenSSH使其使用TCP-Wrappers inetd超級服務器
TCP-WRAPPERS用來啟動和停止sshd1服務。當inetd運行的時候,它會從配置文件(默認為“/etc/inetd.conf”)中讀入配置信息。在配置文件中每一行的不同項是用TAB或空格分開。
最後更新:2017-04-02 00:00:28