歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> 關於AS5 SSH支持key認證的完全解決方案

關於AS5 SSH支持key認證的完全解決方案

日期:2017/3/3 15:59:32   编辑:關於Linux

SSH 為 Secure Shell 的縮寫, 專為遠程登錄會話和其他網絡服務提供安全性的協議。利用 SSH 協議可以有效防止遠程管理過程中的信息洩露問題。透過 SSH 可以對所有傳輸的數據進行加密,也能夠防止 DNS欺騙和IP欺騙.還有一個優點就是為其傳輸的數據是經過壓縮的,所以可以加快傳輸的速度.

在客戶端來看,SSH提供兩種級別的安全驗證。

第一種級別(基於密碼的安全驗證),知道帳號密碼,就可登錄到遠程主機,並且所有傳輸的數據都會被加密。但是,可能會有別的服務器在冒充真正的服務器,無法避免被“中間人”攻擊。

第二種級別(基於密匙的安全驗證),需要依靠密匙,也就是你必須為自己創建一對密匙,並把公有密匙放在需要訪問的服務器上。客戶端軟件會向服務器發出請求,請求用你的密匙進行安全驗證。服務器收到請求之後,先在你在該服務器的用戶根目錄下尋找你的公有密匙,然後把它和你發送過來的公有密匙進行比較。如果兩個密匙一致,服務器就用公有密匙加密“質詢”(challenge)並把它發送給客戶端軟件。從而避免被“中間人”攻擊。

在服務器端,SSH也提供兩種安全驗證。

第一種方案,主機將自己的公用密鑰分發給相關的客戶端,客戶端在訪問主機時則使用該主機的公開密鑰來加密數據,主機則使用自己的私有密鑰來解密數據,從而實現主機密鑰認證,確定客戶端的可靠身份。

第二種方案,存在一個密鑰認證中心,所有提供服務的主機都將自己的公開密鑰提交給認證中心,而任何作為客戶端的主機則只要保存一份認證中心的公開密鑰就可以了。在這種模式下,客戶端必須訪問認證中心然後才能訪問服務器主機。

實驗環境:

解決方案:

可以通過客戶端或者服務器端提供ssh安全認證

1.客戶端提供ssh安全認證,也就是上面提到第二種級別

(1).客戶端

利用客戶端linux系統提供ssh安全認證,如客戶端是XP系統,可以選擇用SecureCRT自動生成ssh2的公鑰與私鑰.

由於本地系統是windows 7 ,這裡使用SecureCRT生成ssh認證。

SecureCRT---Options---Global Options----SSH2

URL:http://www.bianceng.cn/OS/Linux/201410/45756.htm

完成後會在C:\Users\Administrator\AppData\Roaming\VanDyke\目錄自動生成以Identity命名公鑰與私鑰.

URL:http://www.bianceng.cn/OS/Linux/201410/45756.htm

(2)服務器端

為了server安全性考慮,禁止空密碼與密碼登錄系統。

需修改/etc/ssh/sshd.conf

#PermitEmptyPasswords no 修改為 PermitEmptyPasswords no

PasswordAuthentication yes ========= PasswordAuthentication no

[root@viong .ssh]# service sshd restart

需要重啟ssh才能使配置生效

Stopping sshd: [ OK ]

Starting sshd: [ OK ]

[root@viong ~]# ssh localhost

The authenticity of host 'localhost (127.0.0.1)' can't be established.

RSA key fingerprint is 05:de:f8:d8:7b:db:36:0a:1e:01:cc:06:64:0e:7e:0a.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added 'localhost' (RSA) to the list of known hosts.

root@localhost's password: 輸入系統密碼

Last login: Tue Feb 23 00:32:06 2010 from 192.168.15.253

[root@viong ~]# cd ~/.ssh

利用RZ小工具上傳剛剛客戶端生成的Identity.pub到~/.ssh目錄下

關於RZ 與SZ的安裝請看http://viong.blog.51cto.com/844766/260081

[root@viong .ssh]#rz

上傳Identity.pub

[root@viong .ssh]# cat Identity.pub >>authorized_keys

把公鑰的內容輸出到authorized_keys

[root@viong .ssh]# rm -rf Identity.pub

刪除公鑰

[root@viong .ssh]# chmod 400 authorized_keys

設置authorized_keys的權限為400

[root@viong .ssh]# ll

total 12

-r-------- 1 root root 608 Feb 23 02:38 authorized_keys

-rw-r--r-- 1 root root 391 Feb 23 02:34 known_hosts

重新連接服務器,提示密鑰輸入密碼,說明設置成功了.

======================================================================

客戶端與服務器端都是linux系統基於RSA認證無私鑰保護密碼情況下

Client:192.168.15.50 server:192.168.15.64

Clinet:

[root@client .ssh]# pwd

/root/.ssh 如沒這個目錄,可以手動添加

[root@client .ssh]# ssh-keygen –t rsa

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa): 直接回車

Enter passphrase (empty for no passphrase): 直接回車

Enter same passphrase again: 直接回車

Your identification has been saved in viong.

Your public key has been saved inid_rsa .pub.

The key fingerprint is:

f9:14:16:7f:a8:68:f2:90:46:2e:28:b8:45:cc:aa:e9 root@client

[root@client .ssh]# ll

total 8

-rw------- 1 root root 1675 Feb 23 22:21 id_rsa

-rw-r--r-- 1 root root 393 Feb 23 22:21 id_rsa .pub

[root@client .ssh]# ssh-copy-id -i /root/.ssh/id_rsa .pub [email protected]

URL:http://www.bianceng.cn/OS/Linux/201410/45756.htm

發布公匙到服務器端,會在服務器端自動生成.ssh/authorized_keys這個文件

20

reverse mapping checking getaddrinfo for macbook_air failed - POSSIBLE BREAK-IN ATTEMPT!

[email protected]'s password: 輸入192.168.15.64的root密碼

Now try logging into the machine, with "ssh '192.168.15.64'", and check in:

.ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

提示:ssh-copy-id 這個工具是針對22號端口,如果對方ssh端口是2520,那麼就要修改為:

ssh-copy-id -i /root/.ssh/id_rsa .pub “-p 2520 192.168.15.64”

Server:

[root@server .ssh]# ll

查看是否有authorized_keys生成

total 16

-rw------- 1 root root 393 Feb 23 22:28 authorized_keys

-rw-r--r-- 1 root root 395 Feb 23 21:49 known_hosts

[root@server .ssh]# cat authorized_keys確認是否發布成功

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAtUOq6yotBEkldfBFZalh4FrFtX6wERChbLn7igQI4aofBNThWgIlBPfaThncfhs2Vu9DSBxExyGje/evcu/TyQRVBTuejEQnF4RPB+5fNMS+Qq48pdS33Rd/yt4m+Np/eGYDYW/ch5r/MRm5nek/WCW2MPAQbqrSZ7prj++SnMlavZDlAVsgHbY3GLYDD/7UbFJvewbY099zjSrW7yqIPdF9nACIg0dN+aMRtezJwr+NvOFZwrUuuz/ek1iCl2Q6soxRAkOTWbkE5bnF5LJJWtOox+ZxDu/mAry0dSv3eH0NMs6NPfBkwE5pyE9HG47r7k6kp7qKsuKCysk+MASNaQ== root@client

在客戶端輸入

[root@client .ssh]# ssh [email protected]

Last login: Wed Feb 24 00:13:09 2010 from 192.168.15.50

注意:Enter file in which to save the key (/root/.ssh/id_rsa): 直接回車---這個地方不能輸入名稱,不然無密碼登陸會報以下錯誤:

Permission denied (publickey,gssapi-with-mic

可以等生成後再做修改

===================================================================

[root@server ~]# 連接成功

提示:由於生成密鑰的口令的密碼為空,可以利用scp遠程加密備份,scp是有Security的文件copy,基於ssh登錄。操作起來比較方便,比如要把當前一個文件copy到遠程另外一台主機上,可以如下命令。

[root@clinet .ssh]# scp id_dsa.pub [email protected]:/home/

reverse mapping checking getaddrinfo for macbook_air failed - POSSIBLE BREAK-IN ATTEMPT!

id_dsa.pub 100% 601 0.6KB/s 00:00

[root@server .ssh]# ll /home/

total 4

-rw-r--r-- 1 root root 601 Feb 24 00:25 id_dsa.pub 說明拷貝成功

如果想反過來操作,把文件從遠程主機copy到當前系統,也很簡單。

[root@server home]# scp id_dsa.pub [email protected]:/home/

The authenticity of host '192.168.15.50 (192.168.15.50)' can't be established.

RSA key fingerprint is 05:de:f8:d8:7b:db:36:0a:1e:01:cc:06:64:0e:7e:0a.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '192.168.15.50' (RSA) to the list of known hosts.

Address 192.168.15.50 maps to ysgame2-pc, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!

[email protected]'s password: 輸入client密碼

id_dsa.pub 100% 601 0.6KB/s 00:00

[root@clinet .ssh]# ll /home/

total 4

-rw-r--r-- 1 root root 601 Feb 24 00:29 id_dsa.pub 說明拷貝成功

2.服務器端提供ssh安全認證,也就上面提到第一種方案

(1)服務器端(AS5)

同樣修改/etc/ssh/sshd.conf

#PermitEmptyPasswords no 修改為 PermitEmptyPasswords no

PasswordAuthentication yes ========= PasswordAuthentication no

[root@viong .ssh]# service sshd restart

需要重啟ssh才能使配置生效

Stopping sshd: [ OK ]

Starting sshd: [ OK ]

[root@viong ~]# ssh localhost

URL:http://www.bianceng.cn/OS/Linux/201410/45756.htm

第一次通過ssh命令登入服務器時,要輸入“yes”,再輸入本地服務器的密碼,登入信息記入到用戶主目錄下.ssh的known_host文件中。

The authenticity of host 'localhost (127.0.0.1)' can't be established.

RSA key fingerprint is 05:de:f8:d8:7b:db:36:0a:1e:01:cc:06:64:0e:7e:0a.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added 'localhost' (RSA) to the list of known hosts.

root@localhost's password: 輸入系統密碼

Last login: Tue Feb 23 00:32:06 2010 from 192.168.1.253

[root@viong ~]#cat known_hosts

localhost ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAy1OKMCYaKdT544qv0hRBBC6rFZviEJ6ECwnBV/mNkwahgkPp5TTHyHDUeBPtYmmKVdcxgjItUtTBHFPQjN2aKwNmPfikqf0WoaN7yf2F6IVAyAIaIi5jXwkJzadW61izmCEkdi/33r4oj5uoNfTjMBfghZZzrMwGZHEaEg2nWbbhMJrzuMU6v31WhjPm6MlZ+xBoR7T2iqiDyUczzSJZ2KG2rPMTG6Zdxm+WLrioOoeIn25wY+4gsrpPUGHxQsxVNIpUP6UtGZA/Y9uksgN5OSIkSxRwGPDTCp65mYQ7pDo9Pvbxg1yQFs54+zA2XcsKz55mbn0MMucPrOxbfGtIEQ==

以上連接是基於口令認證,相對telnet還是很安全.但還是會受到“中間人”攻擊

[root@viong ~]# cd ~/.ssh

[root@viong ~]# ssh-keygen -t dsa 生成公鑰與私鑰

Generating public/private dsa key pair.

Enter file in which to save the key (/root/.ssh/id_dsa): viong密鑰文件名

Enter passphrase (empty for no passphrase): 輸入口令

Enter same passphrase again: 再次輸入口令

Your identification has been saved in viong.

Your public key has been saved in viong.pub.

The key fingerprint is:

cf:fd:14:c1:7c:eb:de:e4:5e:bb:91:6a:70:57:1c:8e root@viong

[root@viong .ssh]# ll

total 12

-rw-r--r-- 1 root root 391 Feb 23 02:34 known_hosts

-rw------- 1 root root 736 Feb 23 03:08 viong

-rw-r--r-- 1 root root 600 Feb 23 03:08 viong.pub

[root@viong .ssh]# catviong.pub >>authorized_keys

把公鑰的內容輸出到authorized_keys

[root@viong .ssh]# chmod 400 authorized_keys

設置authorized_keys的權限為400

(2)客戶端(WIN 7-SecureCRT)

把viong 私鑰和viong.pub公鑰用sz小工具下載到本地同一個目錄

[root@viong .ssh]#sz viong

[root@viong .ssh]#sz viong.pub

[root@viong .ssh]# rm -rf viong.pub

[root@viong .ssh]# rm -rf viong

刪除公鑰與密鑰

用SecureCRT測試連接,如下圖,說明連接成功。

關於大批量服務器更改key的腳本,後面會更新上去.

備注:關於 /etc/ssh/sshd.conf詳解,轉載了鳥哥的文章給大家參考。

http://203.208.39.132/search?q=cache:MGSOnAay0xoJ:blog.chinaunix.net/u3/99067/showart_2022385.html+&cd=3&hl=zh-CN&ct=clnk&gl=cn&st_usg=ALhdy2_BDztvJ6iX9NPcUkXym9Ohx0SclQ

# 1. 關於 SSH Server 的整體設定,包含使用的 port 啦,以及使用的密碼演算方式

Port 22          # SSH 預設使用 22 這個 port,您也可以使用多的 port !

# 亦即重復使用 port 這個設定項目即可!

Protocol 2,1       # 選擇的 SSH 協議版本,可以是 1 也可以是 2 ,

# 如果要同時支持兩者,就必須要使用 2,1 這個分隔了!

#ListenAddress 0.0.0.0  # 監聽的主機適配卡!舉個例子來說,如果您有兩個 IP,

# 分別是 192.168.0.100 及 192.168.2.20 ,那麼只想要

# 開放 192.168.0.100 時,就可以寫如同下面的樣式:

ListenAddress 192.168.0.100 # 只監聽來自 192.168.0.100 這個 IP 的SSH聯機。

# 如果不使用設定的話,則預設所有接口均接受 SSH

PidFile /var/run/sshd.pid      # 可以放置 SSHD 這個 PID 的檔案!左列為默認值

LoginGraceTime 600     # 當使用者連上 SSH server 之後,會出現輸入密碼的畫面,

# 在該畫面中,在多久時間內沒有成功連上 SSH server ,

# 就斷線!時間為秒!

Compression yes      # 是否可以使用壓縮指令?當然可以啰!

# 2. 說明主機的 Private Key 放置的檔案,預設使用下面的檔案即可!

HostKey /etc/ssh/ssh_host_key    # SSH version 1 使用的私鑰

HostKey /etc/ssh/ssh_host_rsa_key  # SSH version 2 使用的 RSA 私鑰

HostKey /etc/ssh/ssh_host_dsa_key  # SSH version 2 使用的 DSA 私鑰

URL:http://www.bianceng.cn/OS/Linux/201410/45756.htm

# 2.1 關於 version 1 的一些設定!

KeyRegenerationInterval 3600    # 由前面聯機的說明可以知道, version 1 會使用

# server 的 Public Key ,那麼如果這個 Public

# Key 被偷的話,豈不完蛋?所以需要每隔一段時間

# 來重新建立一次!這裡的時間為秒!

ServerKeyBits 768           # 沒錯!這個就是 Server key 的長度!

# 3. 關於登錄文件的訊息數據放置與 daemon 的名稱!

SyslogFacility AUTH         # 當有人使用 SSH 登入系統的時候,SSH會記錄資

# 訊,這個信息要記錄在什麼 daemon name 底下?

# 預設是以 AUTH 來設定的,即是 /var/log/secure

# 裡面!什麼?忘記了!回到Linux 基礎去翻一下

# 其它可用的 daemon name 為:DAEMON,USER,AUTH,

# LOCAL0,LOCAL1,LOCAL2,LOCAL3,LOCAL4,LOCAL5,

LogLevel INFO            # 登錄記錄的等級!嘿嘿!任何訊息!

# 同樣的,忘記了就回去參考!

# 4. 安全設定項目!極重要!

# 4.1 登入設定部分

PermitRootLogin no    # 是否允許 root 登入!預設是允許的,但是建議設定成 no!

UserLogin no        # 在 SSH 底下本來就不接受 login 這個程序的登入!

StrictModes yes      # 當使用者的 host key 改變之後,Server 就不接受聯機,

# 可以抵擋部分的木馬程序!

#RSAAuthentication yes   # 是否使用純的 RSA 認證!?僅針對 version 1 !

PubkeyAuthentication yes # 是否允許 Public Key ?當然允許啦!只有 version 2

AuthorizedKeysFile .ssh/authorized_keys

# 上面這個在設定若要使用不需要密碼登入的賬號時,那麼那個

# 賬號的存放檔案所在檔名!

# 4.2 認證部分

RhostsAuthentication no  # 本機系統不止使用 .rhosts ,因為僅使用 .rhosts 太

# 不安全了,所以這裡一定要設定為 no !

IgnoreRhosts yes      # 是否取消使用 ~/.ssh/.rhosts 來做為認證!當然是!

RhostsRSAAuthentication no # 這個選項是專門給 version 1 用的,使用 rhosts 檔案在

# /etc/hosts.equiv配合 RSA 演算方式來進行認證!不要使用

HostbasedAuthentication no # 這個項目與上面的項目類似,不過是給 version 2 使用的!

IgnoreUserKnownHosts no  # 是否忽略家目錄內的 ~/.ssh/known_hosts 這個檔案所記錄

# 的主機內容?當然不要忽略,所以這裡就是 no 啦!

PasswordAuthentication yes # 密碼驗證當然是需要的!所以這裡寫 yes 啰!

PermitEmptyPasswords no  # 若上面那一項如果設定為 yes 的話,這一項就最好設定

# 為 no ,這個項目在是否允許以空的密碼登入!當然不許!

ChallengeResponseAuthentication yes # 挑戰任何的密碼認證!所以,任何 login.conf

# 規定的認證方式,均可適用!

#PAMAuthenticationViaKbdInt yes # 是否啟用其它的 PAM 模塊!啟用這個模塊將會

# 導致 PasswordAuthentication 設定失效!

# 4.3 與 Kerberos 有關的參數設定!因為我們沒有 Kerberos 主機,所以底下不用設定!

#KerberosAuthentication no

#KerberosOrLocalPasswd yes

#KerberosTicketCleanup yes

#KerberosTgtPassing no

# 4.4 底下是有關在 X-Window 底下使用的相關設定!

X11Forwarding yes

#X11DisplayOffset 10

#X11UseLocalhost yes

# 4.5 登入後的項目:

PrintMotd no # 登入後是否顯示出一些信息呢?例如上次登入的時間、地點等

# 等,預設是 yes ,但是,如果為了安全,可以考慮改為 no !

PrintLastLog yes     # 顯示上次登入的信息!可以啊!預設也是 yes !

KeepAlive yes       # 一般而言,如果設定這項目的話,那麼 SSH Server 會傳送

# KeepAlive 的訊息給 Client 端,以確保兩者的聯機正常!

# 在這個情況下,任何一端死掉後, SSH 可以立刻知道!而不會

# 有僵屍程序的發生!

UsePrivilegeSeparation yes # 使用者的權限設定項目!就設定為 yes 吧!

MaxStartups 10      # 同時允許幾個尚未登入的聯機畫面?當我們連上 SSH ,

# 但是尚未輸入密碼時,這個時候就是我們所謂的聯機畫面啦!

# 在這個聯機畫面中,為了保護主機,所以需要設定最大值,

# 預設最多十個聯機畫面,而已經建立聯機的不計算在這十個當中

# 4.6 關於使用者抵擋的設定項目:

DenyUsers *        # 設定受抵擋的使用者名稱,如果是全部的使用者,那就是全部

# 擋吧!若是部分使用者,可以將該賬號填入!例如下列!

DenyUsers test

DenyGroups test      # 與 DenyUsers 相同!僅抵擋幾個群組而已!

# 5. 關於 SFTP 服務的設定項目!

Subsystem sftp /usr/lib/ssh/sftp-server

本文出自 “技術在於折騰” 博客,請務必保留此出處http://viong.blog.51cto.com/844766/278062

Copyright © Linux教程網 All Rights Reserved