ssh和ssh2
轉(zhuǎn)載自:http://blog.lifeclaw.com/
所有關(guān)于SSH服務(wù)的一切
SSH為Secure Shell的縮寫,由IETF的網(wǎng)絡(luò)工作小組(Network Working Group)所制定;SSH為創(chuàng)建在應(yīng)用層和傳輸層基礎(chǔ)上的安全協(xié)議。在維基百科上,有關(guān)于SSH的詳細(xì)詞條,但通俗點(diǎn)說,SSH能夠讓一個客戶端安全的登錄上一個服務(wù)器上進(jìn)行管理操作。所以,忘掉FTP、POP和Telnet吧,專心來愛SSH。
讓我們從最基礎(chǔ)的部分開始,首先假定我們有臺Macbook,然后想登錄上一臺Ubuntu服務(wù)器進(jìn)行管理操作,那么首先要求Ubuntu服務(wù)器上 安裝了SSH服務(wù)。SSH服務(wù)最早是由芬蘭的一家公司開發(fā),現(xiàn)在已經(jīng)發(fā)展到SSH2版本,但由于版權(quán)和加密算法等因素的影響,很多人開始轉(zhuǎn)用 OpenSSH,聽這名字,就知道它是開源和免費(fèi)的。
以下所有操作都需要具備root權(quán)限的賬號,通常我們不太建議在服務(wù)器上直接登錄為root,所以一般會登錄為普通用戶,然后通過在命令前面加上sudo來獲取root權(quán)限。
1.我們先慣例一下
sudo apt-get update sudo apt-get upgrade
2.然后開始安裝OpenSSH服務(wù)
sudo apt-get install openssh-server
3.Ubuntu會幫我們解決一切依賴關(guān)系問題并且安裝好OpenSSH服務(wù),接下來可以做一些配置來實(shí)現(xiàn)更快更安全的目的具體的修改可以參見這里。
至此安裝已經(jīng)結(jié)束了,下面我們可以從Macbook上登錄試試,假設(shè)Ubuntu上存在一個用戶tester。在Macbook上選擇應(yīng)用程序 – 實(shí)用工具 – 終端,然后在打開的終端里面輸入
#注意這里S_IP是服務(wù)器的真實(shí)IP地址 ssh tester@S_IP
然后就會問你test的密碼,輸入密碼就可以成功登錄進(jìn)行操作了。
每次都輸入密碼會很煩,而且也不安全,同時還有其他一些潛在的風(fēng)險,所以SSH也提供基于密鑰的認(rèn)證機(jī)制,你必須為自己創(chuàng)建一對密鑰,并把公鑰放在 需要訪問的服務(wù)器上。客戶端軟件會向服務(wù)器發(fā)出請求,請求用你的私匙進(jìn)行安全驗證。服務(wù)器收到請求之后,先在你在該服務(wù)器的用戶根目錄下尋找你的公鑰,然 后把它和你發(fā)送過來的公鑰進(jìn)行比較。如果兩個密鑰一致,服務(wù)器就用公有密鑰加密“質(zhì)詢”(challenge)并把它發(fā)送給客戶端軟件。從而避免被“中間 人”攻擊。
由于之前所說的原因,會出現(xiàn)一種蛋疼的情況,有些公司還喜歡使用SSH2版本的SSH服務(wù),SSH2和OpenSSH的加密算法是完全不一樣的,他們所使用的的密鑰對也不兼容,所以會出現(xiàn)下面4種組合
1. OpenSSH客戶端對OpenSSH服務(wù)器
2. SSH2客戶端對SSH2服務(wù)器
3. OpenSSH客戶端對SSH2服務(wù)器
4. SSH2客戶端對OpenSSH服務(wù)器
假設(shè)客戶端C試圖使用用戶tester登錄服務(wù)器S,我們來看看各種組合下如何使用密鑰登錄
1. OpenSSH客戶端對OpenSSH服務(wù)器,這是最簡單和最常見的情況
首先在C上操作
ssh-keygen -t rsa
生成的私鑰保存在~/.ssh/id_rsa,注意私鑰一定要是這個名字,除非你更改C的ssh客戶端配置,然后將公鑰id_rsa.pub上傳到S上去
#這里S_IP是服務(wù)器的真實(shí)IP,并假定用戶tester的主目錄是/home/tester scp ~/.ssh/id_rsa.pub tester@S_IP:/home/tester/.ssh/
然后在服務(wù)器S上做如下操作
cd /home/tester/.ssh cat id_rsa.pub >> authorized_keys
退出服務(wù)器S,然后從C上重新登錄一下
ssh tester@S_IP
不出意外,你再也不用輸入密碼了。
2. SSH2客戶端對SSH2服務(wù)器
這種情況也很簡單,因為SSH2版本的ssh服務(wù)已經(jīng)有了個新的工具ssh-keygen2。
首先在C上操作
ssh-keygen2 -t rsa
注意,這將會在C上當(dāng)前用戶的目錄的這個位置~/.ssh2/生成一對密鑰id_rsa_2048_a和id_rsa_2048_a.pub
你必須在~/.ssh2/目錄下建立一個文件identification,并通過它來指定私鑰
cd ~/.ssh2/ vi identification #輸入如下內(nèi)容 IdKey id_rsa_2048_a #保存修改
然后將公鑰id_rsa_2048_a.pub傳到服務(wù)器S上去
#這里S_IP是服務(wù)器的真實(shí)IP,并假定用戶tester的主目錄是/home/tester scp ~/.ssh2/id_rsa_2048_a.pub tester@S_IP:/home/tester/.ssh2/
然后在服務(wù)器S上做如下操作
cd /home/tester/.ssh2 vi authorization #在里面新增一行 Key id_rsa_2048_a.pub #保存修改
退出服務(wù)器S,然后從C上重新登錄一下
ssh tester@S_IP
不出意外,這能夠工作了。
3. OpenSSH客戶端對SSH2服務(wù)器
這種情況是最復(fù)雜的一種,網(wǎng)絡(luò)上很多的免密碼登錄SSH的文章都沒有涉及到這種,下面具體介紹一下應(yīng)該如何配置
首先在C上操作
ssh-keygen -t rsa
生成的私鑰保存在~/.ssh/id_rsa,注意私鑰一定要是這個名字,除非你更改C的ssh客戶端配置,然后你需要做一件事情,就是將公鑰轉(zhuǎn)換成為SSH2所兼容的模式,使用以下的指令
cd ~/.ssh/ ssh-keygen -e -f id_rsa.pub > id_rsa_2.pub
然后將公鑰id_rsa_2.pub上傳到S上去
#這里S_IP是服務(wù)器的真實(shí)IP,并假定用戶tester的主目錄是/home/tester scp ~/.ssh2/id_rsa_2.pub tester@S_IP:/home/tester/.ssh2/
然后在服務(wù)器S上做如下操作
cd /home/tester/.ssh2 vi authorization #在里面新增一行 Key id_rsa_2.pub #保存修改
退出服務(wù)器S,然后從C上重新登錄一下
ssh tester@S_IP
不出意外,這能夠工作了。
4. SSH2客戶端對OpenSSH服務(wù)器
這種情況是最蛋疼的,應(yīng)該非常少見吧?這意味你將用一臺商業(yè)授權(quán)的服務(wù)器去管理一臺開源的服務(wù)器?希望你的工作不用這么糾結(jié),雖然這種情況的配置是非常簡單的,基本和1一致,因為SSH2原生也支持SSH1,所以就請大家參見1的配置了。
如果了解完了上面所說的一切,包括引用鏈接,你就完全夠?qū)SH應(yīng)用到工作的各個方面的,下面還會稍微透露一下,平時可能需要了解到的一些秘籍
1.SSH2密鑰和OpenSSH密鑰的相互轉(zhuǎn)換。
#OpenSSH轉(zhuǎn)SSH2 ssh-keygen -e -f OpenSSH.pub > SSH2.pub #SSH2轉(zhuǎn)OpenSSH2 ssh-keygen -i -f SSH2.pub > SSH2.pub
2.平時如果我們在Windows環(huán)境下,通常會使用SecureCRT,XShell以及Putty等優(yōu)秀的SSH客戶端軟件,它們可以讓SSH 的工作變得更輕松,但如果在Mac或者Linux環(huán)境下,命令行的SSH操作則更自然,那么你知道在命令行下的SSH如何使用代理嘛,當(dāng)需要的時候?
下面以O(shè)penSSH客戶端為例,假設(shè)有兩個服務(wù)器S1和S2,需要通過一個代理服務(wù)器P1的80端口才能夠連接。
vi ~/.ssh/config #修改如下內(nèi)容 Host S1_IP S2_IP ProxyCommand nc -X connect -x P1:80 %h %p ServerAliveInterval 60
此外,在使用scp都時候還有可能因為ssh和ssh2的問題出現(xiàn)如下錯誤:"scp - FATAL: Executing ssh1 in compatibility mode failed (check that scp1 is in your PATH)."
Quote 1:
This problem is often quite perplexing, since a ssh -V trace may show that you're using SSH-2 - so what
is a message about "ssh1 compatibility mode " doing in there?
What's happening is this:
1. On the OpenSSH client, you run say, scp foo server:bar
2. scp runs ssh in a subprocess to connnect to the server, and run the remote command scp -t bar. This
is intend to start an instance of the scp program on the server, and the two scp's will cooperate by
speaking over the SSH connection, to retrieve the file.
3. ssh connects to the server (using either protocol 1 or 2, it doesn't matter), and runs the remote scp
command. However, the "scp" that gets run on the server is the SSH2 scp program (scp2), not the
OpenSSH one. The crux of the problem is: besides the name, these two scp's have exactly nothing in
common. scp2 cannot speak the file-transfer protocol that OpenSSH scp does. However, scp2 recognizes
from the "-t" flag what's expected, and tries exec scp1 to service the connection (this is the extent
of SSH2's SSH-1 compatibility; where OpenSSH has code for both protocols in a single set of programs,
SSH2 expects to execute programs from a parallel SSH1 installation). It fails (presumably because
you don't have SSH1 installed), and reports the problem.
The solution is to install either the OpenSSH or SSH1 version of scp on the server under the name "scp1",
somewhere in the sshd2's PATH.
Quote 2:
OpenSSH implements "scp" via RCP over an SSH channel.
ssh.com implement "scp" via FTP over an SSH channel.
OpenSSH's server has both implementations, but it's client only uses
the RCP version.
So if the client is OpenSSH, use "s
上述情況發(fā)生的場景一般是openssh作為client,要連接一個ssh2都server,
如果上述兩種解決方案都覺得麻煩的話,可以通過tar來繞過這個問題:scp2() {
tar cf - -C $(dirname $1) $(basename $1) | ssh user_name@server_ip -- "tar xmf - -C $2"
}
scp2r () {
ssh user_name@server_ip -- "tar cf - -C $(dirname $1) $(basename $1)" | tar xmf - -C ${2:-.};
}