linux ssh key配置方法

來源:互聯網
上載者:User

一、使用公開金鑰認證

1、原理:首先由使用者產生一對密鑰,然後將公開金鑰儲存在SSH伺服器使用者的目錄下.ssh子目錄中的authorized_key檔案裡(/root/.ssh/authorized_key).私密金鑰儲存在本機電腦.當使用者登陸時,伺服器檢查authorized_key檔案的公開金鑰是否與使用者的私密金鑰對應,如果相符則允許登入,否則拒絕.由於私密金鑰只有儲存在使用者的本機電腦中,因此入侵者就算得到使用者口令,也不能登陸到伺服器.

2、啟用公開金鑰認證
修改設定檔/etc/ssh/sshd_config 
將"PasswordAuthentication yes"修改為"PasswordAuthentication no"
3、產生密鑰
[root@rain ~]# 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 /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
4d:dd:48:af:76:c2:ba:a8:bc:20:f3:28:1d:6a:28:53 root@rain.fish.com
4、將公開金鑰/root/.ssh/id_rsa.pub改名為/root/.ssh/authorized_keys
mv /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys
5、將私密金鑰id_rsa拷貝到客戶機/root/.ssh/下就可以直接使用了。

二、SSH服務的配置
1、配置SSH服務的運行參數,是通過修改設定檔/etc/ssh/sshd_config實現的。
2、因為SSH服務使用預設的配置已經能夠很好的工作,如果僅僅提供SSH服務不需要修改。這裡只介紹一些常用的選項。
#Port 22
定義了SSH服務監聽的斷口號,SSH服務預設使用的連接埠號碼是22
#Proctocol 2,1
定義了SSH伺服器使用SSH協議的順序。預設識先使用SSH2協議,如果不成功則使用SSH1協議,為了安全起見,可以設定只使用SSH2協議。
#ListenAddress 0.0.0.0
定義SSH伺服器幫定的IP地址,預設綁定伺服器所有可用的IP地址.
#PermitRootLogin yes
定義是否允許管理員登陸
#PermitEmptyPasswords no
定義是否允許空密碼登陸.

#PasswordAuthentication no
定義是否使用口令認證方式,如果準備使用公開金鑰認證可以設定為no

怎麼在登入不同伺服器使用不同的密鑰呢?man ssh就看到有個 -i
參數可以指定不同的密鑰檔案
方法一:

ssh -i /root/.ssh/id_rsa root@192.168.11.117
ls /root
方法二:新增ssh的設定檔並修改許可權

touch /root/.ssh/config

chmod 600 /root/.ssh/config

修改config檔案
Host 192.168.11.109
        IdentityFile /root/.ssh/id_rsb
        User root(or xxx)
Host 192.168.11.117
        IdentityFile /root/.ssh/id_rsa
        User root(or yyy)

現在可執行:ssh 192.168.11.109 ls /root

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.