請選擇 進入手機版 | 繼續訪問電腦版
搜索
熱搜: 活動 交友 discuz
查看: 1639|回復: 0

[Linux] 如何使用公鑰/私鑰登入 Linux 系統?

[複製鏈接]
發表於 2007-12-5 17:45:01 | 顯示全部樓層 |閱讀模式
How to use Authorized Key by ssh for login?
如何使用公鑰/私鑰登入 Linux 系統?
====[Root篇---開始]=========================================================================
以下的 config 檔,為 FC4 的 /etc/ssh/sshd_config 的內容
--------------------------------------------------------------------------------------
代碼:
#       $OpenBSD: sshd_config,v 1.70 2004/12/23 23:11:00 djm Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options change a
# default value.

Port 22
#Protocol 2,1
#Because Portocol 1 is not safe ,so I don't use, and avoid attack.
Protocol 2
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key

# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 1h
#ServerKeyBits 768

# Logging
#obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
LogLevel INFO

# Authentication:

#LoginGraceTime 2m
PermitRootLogin yes
#StrictModes yes
MaxAuthTries 3

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile      .ssh/authorized_keys

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#RhostsRSAAuthentication no
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication no
#PermitEmptyPasswords no

# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no

# GSSAPI options
#GSSAPIAuthentication no
GSSAPIAuthentication yes
#GSSAPICleanupCredentials yes
GSSAPICleanupCredentials yes

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication mechanism.
# Depending on your PAM configuration, this may bypass the setting of
# PasswordAuthentication, PermitEmptyPasswords, and
# "PermitRootLogin without-password". If you just want the PAM account and
# session checks to run without PAM authentication, then enable this but set
# ChallengeResponseAuthentication=no
#UsePAM no
UsePAM yes

#AllowTcpForwarding yes
#GatewayPorts no
#X11Forwarding no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PrintMotd yes
#PrintLastLog yes
TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation yes
#PermitUserEnvironment no
#Compression yes
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS yes
#PidFile /var/run/sshd.pid
#MaxStartups 10
#ShowPatchLevel no

# no default banner path
#Banner /some/path

# override default of no subsystems
Subsystem       sftp    /usr/libexec/openssh/sftp-server
---------------------------------------------------------------------------------------
以上內容為 FC4 的 /etc/ssh/sshd_config 的內容,但僅供參考,我不敢保證完全對,
所以照抄不一定 OK,所以請依您系統的實際狀況及週遭的情況去調整。
=======================================================
0.事前準備:
Puttygen.exe → http://the.earth.li/~sgtatham/putty/latest/x86/puttygen.exe
Putty.exe → http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe
Pietty.exe(建議使用,有支援中文輸入,SCP 檔案上傳.)
http://ntu.csie.org/~piaip/pietty/stable/pietty0327.exe
WinSCP3 → http://winscp.net/eng/
==============================================
1.修改 /etc/ssh/sshd_config
#在 sshd_config 裡,找到與下列相符的選項,就將選項前的 # 號拿掉:
Protocol 2
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PermitRootLogin yes
#找到 ChallengeResponseAuthentication yes 的選項,改成 no ,如下:
#(選項解釋:密碼登入選項,一定要改成 no,這樣子沒有擁有私鑰的人就會無法登入了)
# Change to no to disable s/key passwords
#修改成 no 來停用 s/key 密碼
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no
還有把 PasswordAuthentication yes
改成 PasswordAuthentication no
#以下為非必要選項
SyslogFacility AUTHPRIV(或 SyslogFacility AUTH)
LogLevel INFO

接下來就存檔離開
重新啟動 sshd 服務.
#service sshd restart

2.在 Server 端,使用 ssh-keygen 來建立 DSA Private Key 和 Public Key.
先檢查 /root 下有沒有 .ssh 的資料夾,如果有就略過建立資料夾的步驟,
在 /root 下建立資料夾 .ssh,
#mkdir .ssh
#cd .ssh
#ssh-keygen -b 2048 -t rsa
(#ssh-keygen -b 1024 -t dsa)
然後,輸入 Private Key(私鑰)檔名:id_dsa,和 Public Key(公鑰)檔名:id_dsa.pub
這個時候會問你 key passphrase,
(可以自由選擇輸入與否,即使現在不輸入也沒關係,
後面再用 puttygen.exe 轉換格式時再輸入,也是可以的,
但是不可為了省卻輸入密碼,而只用 KEY 就登入 LINUX 系統,
此舉很危險,萬一 Key 檔案被有心人士偷取,就糟糕了.>"<)
再來把公鑰的內容導入到 authorized_keys
#cat id_dsa.pub > authorized_keys

3.因為 Openssh 的私鑰格式和 putty 使用的格式截然不同,
所以需要由 puttygen.exe 轉換格式後才能使用,不然可能會有兩種錯誤的情況:
--------------------------------------------------------------
可能出現的幾種問題:
(a)、Server refused our key
公鑰和私鑰不匹配,或者沒有 authorized_keys 文件
(b)、Unable to use key file "id_dsa.ppk" (SSH2 private key)
私鑰檔案的格式不正確
--------------------------------------------------------------
3.1把 server 上的私鑰:id_dsa 拷貝到 PC 上
將 id_dsa 的內容顯示在螢幕上,再複製下來.
#cat id_dsa
把複製的內容貼到記事本裡存成 id_dsa_by_ssh-keygen.ppk
再來就是開啟用 puttygen.exe → Conversions → Import Key
匯入後,在 Key passphrase 和 Confirm passphrase 輸入保護私鑰的密碼後,
(不想打密碼的人,就保留成空白也可以,不過萬一私鑰掉了被撿到,那就慘了>"<),
然後,再從 File → Save Private Key 把私鑰另存新檔即可使用了。

4.Pietty/Putty 使用方法:
啟動 Pietty/Putty ,從 SSH → Auth 去指定私鑰的檔案路徑即可。

5.強烈建議:私鑰(Private Key)一定要用 Key Passphrase 來保護,
密碼(至少 8 位數以上,每三個月更換一次公/私鑰)千萬不要跟 root 的密碼一樣.
另外,當你在 Server 上用 ssh-keygen 公/私鑰時,會詢問公/私鑰的檔名和
保護 KEY 用的 Passphrase 的密碼,在 Server 有輸入的話,在用 puttygen.exe
做轉換時也會叫你輸入,所以要小心輸入喔!
====[Root篇---結束]=========================================================================
有些人也想說,我也想替我的使用者也提供類似的服務,不要再用密碼登入系統,想要用 Key 來登入,但是,
因為目前密碼的服務,可能已經關閉了,怎麼辦?
====[User篇---開始]=========================================================================
0.修改 sshd 的組態檔
/etc/ssh/sshd_config 要修改一行的內容
找到 AuthorizedKeysFile .ssh/authorized_keys
改成 AuthorizedKeysFile %h/.ssh/authorized_keys
存檔離開,並重新啟動 sshd 服務
#service sshd restart


1.建立 splin 這個使用者帳號,並修改密碼.
#useradd splin
#passwd splin
#cd /home/splin/
#mkdir .ssh
#cd .ssh
#ssh-keygen -b 2048 -t dsa
以下為 ssh-keygen 執行所會發問的問題:
Generating public/private dsa key pair.

當你用 root 登入時,執行 ssh-keygen 會出現下列顯示的路徑
Enter file in which to save the key (/root/.ssh/id_dsa):
當你用 splin 登入時,執行 ssh-keygen 會出現下列顯示的路徑
Enter file in which to save the key (/home/splin/.ssh/id_dsa):
所以以上不管出現何種,如果出現的是預設的路徑(/home/splin/.ssh/id_dsa)就按 Enter 跳過,
則不用輸入→ /home/splin/.ssh/id_dsa (要含路徑和檔名),以上跟你用那個 ID 登入系統有關係。

Enter passphrase (empty for no passphrase):
輸入要保護私鑰的密碼,這裡跟系統的密碼不用一樣,而且密碼千萬是不要一樣。

再來接下來和 Root 篇的公鑰內容導入至 authorized_keys
#cat id_dsa.pub > authorized_keys
但是要確認在 /home/[users]/.ssh/ 路徑和檔名有沒有錯誤。
[users]→表示為 user 的帳號名稱。


2把 server 上的私鑰:id_dsa 拷貝到 PC 上
將 id_dsa 的內容顯示在螢幕上,再複製下來.
#cat id_dsa
把複製的內容貼到記事本裡存成 id_dsa_by_ssh-keygen.ppk
(檔名命名方法沒有一定,但是為不讓自己搞不清楚,這樣子做最好)
再來就是開啟用 puttygen.exe → Conversions → Import Key
(匯入時,因為我們在用 ssh-keygen 產生公/私鑰時,就有用密碼了,所以在你使用 puttygen.exe,
做匯入的動作時,自然會要求你輸入密碼了)
匯入後,在 Key passphrase 和 Confirm passphrase 輸入保護私鑰的密碼後,
(不想打密碼的人,就保留成空白也可以,不過萬一私鑰掉了被撿到,那就慘了>"<),
然後,再從 File → Save Private Key 把私鑰另存新檔即可使用了。


3.強烈建議:私鑰(Private Key)一定要用 Key Passphrase 來保護,
密碼(至少 8 位數以上,每三個月更換一次公/私鑰)千萬不要跟 root 的密碼一樣.


4.因為我是用 root 來替 splin 這個 user 建立公/私鑰,
所以我要把檔案權限和屬性稍微修改一下。
#chown -R splin.splin /home/splin/.ssh/
#chmod -R 755 /home/splin/.ssh/

5.Pietty/Putty 使用方法:
啟動 Pietty/Putty ,從 SSH → Auth 去指定私鑰的檔案路徑即可。

====[User篇---結束]=========================================================================

PS.
1.以上有任何看不懂的,歡迎在此發問,或來信詢問皆可。
2.在您用 putty 測試私鑰是否可以使用之前,請先不要把原先的連線直接關閉,
請先測試用私鑰可以登入後,再把停用密碼登入的選項設定由 yes 改成 no.
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

本論壇為非營利之網路平台,所有文章內容均為網友自行發表,不代表論壇立場!若涉及侵權、違法等情事,請告知版主處理。


Page Rank Check

廣告刊登  |   交換連結  |   贊助我們  |   服務條款  |   免責聲明  |   客服中心  |   中央分站

手機版|中央論壇

GMT+8, 2024-3-29 18:26 , Processed in 0.022194 second(s), 17 queries .

Powered by Discuz!

© 2005-2015 Copyrights. Set by YIDAS

快速回復 返回頂部 返回列表