ssh配置免密碼登陸

平常工做中不少狀況下都須要登陸服務器進行管理,通常都是用ssh進行鏈接,爲了防止密碼外泄,能夠配置下ssh的免密碼登陸。html

首先服務器兩臺:服務器

A:43.224.34.*dom

B:104.238.161.*ssh

配置的結果是B機器能夠免密碼登陸到A服務器。ide

首先在B服務器上生成本機的公私密鑰:ui

ssh-keygen -t rsa -P ''

過程爲:spa

[root@XX .ssh]# ssh-keygen -t rsa -P ''
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
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:
d3:81:0f:f9:6f:3e:d5:aa:ba:28:5f:a8:27:ac:70:88 root@liujianjun
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|         o       |
|        + .      |
|         = .     |
|        S +    . |
| . .     o .  . .|
|E o ..  . . o. . |
|   o  +..o o. .  |
|    ...=o oooo   |
+-----------------+
[root@XX .ssh]# l
total 20
drwx------ 2 root root 4096 Oct 29 14:05 .
dr-xr-x--- 7 root root 4096 Oct 29 12:30 ..
-rw------- 1 root root    0 Oct 21 09:57 authorized_keys
-rw------- 1 root root 1675 Oct 29 14:05 id_rsa
-rw-r--r-- 1 root root  397 Oct 29 14:05 id_rsa.pub
-rw-r--r-- 1 root root  350 Oct 29 12:09 known_hosts

操做生成了兩個文件,一個是id_rsa,一個是id_rsa.pub,爲了傳輸文件的時候出現名稱衝突,把id_rsa.pub名稱修改成104.238.161.*-id_rsa.pub。code

scp將104.238.161.*-id_rsa.pub文件拷貝到A服務器的.ssh目錄下:regexp

scp ./104.238.161.\*-id_rsa.pub root@43.224.34.*:/root/shh

在A服務器的/root/.ssh目錄下多了一個文件,先看下/root/.ssh目錄中的authorized_keys文件的內容,開始的時候是空的,而後將拷貝過來的文件內容導入到authorized_keys中:htm

[root@vultr .ssh]# cat authorized_keys 
[root@vultr .ssh]# cat ./104.238.161.\*-id_rsa.pub >> authorized_keys 
[root@vultr .ssh]# cat authorized_keys 
ssh-rsa *********QDLNuwwf7wUTg829nM2/KFqwW24WXA3OBBkX21cXlo*************pduE62NTHj5fbsgtljVlVk7y3iaIz79KDPgRQPpt779cTuB274mdWQx7g3b5c/tya/OqSTsHWU7PgMkck6RnYZGaFtxJEhZrXyq************z9JVUCToP9DOKgRQsEZFgbT5INMTh3j9o5L/LbGxILBp2vlEJfJQjR96sTumaww95dqKeEgOngpEA2tj74wlFNLQgT9RTzY0U0CWgW8DyISX2IroVqkA9IaTwvYCp9SJF4xDQbl6pzdlYkuCzpwvfc20elB6S0Z root@****
[root@vultr .ssh]# 

從執行結果能夠看出,已經將B的密鑰導入了A的authorized_keys文件中。導入完畢以後要修改一下authorized_key文件的訪問權限:

chmod 700 ~/.ssh/
chmod
600 authorized_keys

這樣配置完畢以後,在B機器就能不用密碼登陸到A機器了,在B機器上直接執行:ssh 43.224.34.*就能登陸到A機器,若是想從A免密碼登陸到B,進行反向的相同的操做便可。

相關文章
相關標籤/搜索