linux下ssh/scp使用公開金鑰認證方式登陸__linux

來源:互聯網
上載者:User

轉載地址: http://hi.baidu.com/edeed/item/13842c2523a2ae0977272c6d

 

比如想要在192.168.41.76伺服器上, 不輸入使用者密碼即以root使用者登陸192.168.41.75伺服器.

 

1) 在192.168.41.76上建立公開金鑰和私密金鑰

[192.168.41.76]# ssh-keygen -t rsa

Generating public/private rsa key pair.

Enter file in which to save the key (//.ssh/id_rsa): //直接斷行符號, 預設位置.ssh/id_rsa

Enter passphrase (empty for no passphrase): //直接斷行符號, 沒有密碼

Enter same passphrase again: //直接斷行符號, 沒有密碼

Your identification has been saved in //.ssh/id_rsa.

Your public key has been saved in //.ssh/id_rsa.pub.

The key fingerprint is:

f3:66:3d:23:9f:cf:78:0b:f8:58:8f:3d:ee:ba:55:99 root@dw_test1

[192.168.41.76]# ll ~/.ssh

-rw-------   1 root root 883 Jun 30 14:25 id_rsa

-rw-r--r--   1 root root 221 Jun 30 14:25 id_rsa.pub

說明:

-t rsa表示SSH產生RSA類型的密鑰, 這是預設的行為. 也可以執行產生DSA類型的密鑰. 這時查看.ssh檔案夾發現多了兩個檔案, id_rsa為私密金鑰, id_rsa.pub為公開金鑰.


2) 確認192.168.41.75上是否有~/.ssh目錄, 沒有則先建立

[192.168.41.75]# ll ~/.ssh

[192.168.41.75]# mkdir ~/.ssh


3) 在192.168.41.76上使用scp將的公開金鑰id_rsa.pub複製到192.168.41.75上

[192.168.41.76]# scp ~/.ssh/id_rsa.pub root@192.168.41.75:~/

root@192.168.41.75's password: //輸入遠程伺服器root登入密碼


4) 在192.168.41.75上追加id_rsa.pub內容到相應使用者下的~/.ssh/authorized_keys檔案裡, 並刪除id_rsa.pub檔案

[192.168.41.75]# cat ~/id_rsa.pub >> ~/.ssh/authorized_keys

[192.168.41.75]# rm -f id_rsa.pub


5) 這樣在192.168.41.76上就可以不輸入密碼直接以root使用者登陸192.168.41.75了, 也多了一個known_hosts檔案

[192.168.41.76]# ssh 192.168.41.75

[192.168.41.76]# ll ~/.ssh

-rw-r--r--   1 root root 221 2009-06-30 14:25:32.000000000 +0800 id_rsa.pub

-rw-------   1 root root 883 2009-06-30 14:25:32.000000000 +0800 id_rsa

-rw-r--r--   1 root root 223 2009-06-30 14:30:26.000000000 +0800 known_hosts


6) 注意

.ssh目錄的許可權必須是0700, .ssh/authorized_keys檔案許可權必須是0600, 否則公開金鑰認證不會生效. 如果想直接進入192.168.41.75的其他使用者, 則需要將公開金鑰追加到其他使用者的authorized_keys檔案裡.

--END--

#Linux

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.