Linux下實現免密碼登陸

1.Linux下生成密鑰bash

  ssh-keygen的命令手冊,經過」man ssh-keygen「命令:服務器

  經過命令」ssh-keygen -t rsa「ssh

  生成以後會在用戶的根目錄生成一個 「.ssh」的文件夾spa

  進入「.ssh」會生成如下幾個文件.net

  authorized_keys:存放遠程免密登陸的公鑰,主要經過這個文件記錄多臺機器的公鑰
code

  id_rsa : 生成的私鑰文件
ip

  id_rsa.pub : 生成的公鑰文件
ci

  know_hosts : 已知的主機公鑰清單rem

    若是但願ssh公鑰生效需知足至少下面兩個條件:get

      1) .ssh目錄的權限必須是700

     2) .ssh/authorized_keys文件權限必須是600

2.遠程免密登陸

  原理圖:

  經常使用如下幾種方法:

    2.1 經過ssh-copy-id的方式

    命令: ssh-copy-id -i ~/.ssh/id_rsa.pub <romte_ip>

    舉例:      

1
2
3
4
5
6
7
8
9
10
11
[root@ test . ssh ] # ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.91.135
root@192.168.91.135's password:
Now try logging into the machine, with "ssh '192.168.91.135'" , and check in :
 
. ssh /authorized_keys
 
to make sure we haven 't added extra keys that you weren' t expecting.
 
[root@ test . ssh ] # ssh root@192.168.91.135
Last login: Mon Oct 10 01:25:49 2016 from 192.168.91.133
[root@localhost ~] #

    常見錯誤:

      [root@test ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.91.135

      -bash: ssh-copy-id: command not found //提示命令不存在

      解決辦法:yum -y install openssh-clients

   2.2 經過scp將內容寫到對方的文件中

      命令:scp -p ~/.ssh/id_rsa.pub root@<remote_ip>:/root/.ssh/authorized_keys

      舉例:

1
2
3
4
5
6
7
8
9
[root@ test . ssh ] # scp -p ~/.ssh/id_rsa.pub root@192.168.91.135:/root/.ssh/authorized_keys
root@192.168.91.135's password:
id_rsa.pub 100% 408 0.4KB /s 00:00
[root@ test . ssh ] #
[root@ test . ssh ] #
[root@ test . ssh ] #
[root@ test . ssh ] # ssh root@192.168.91.135
Last login: Mon Oct 10 01:27:02 2016 from 192.168.91.133
[root@localhost ~] #

      也能夠分爲兩步操做:

      $ scp ~/.ssh/id_rsa.pub root@<remote_ip>:pub_key //將文件拷貝至遠程服務器       $ cat ~/pub_key >>~/.ssh/authorized_keys //將內容追加到authorized_keys文件中, 不過要登陸遠程服務器來執行這條命令

相關文章
相關標籤/搜索