首頁  >  文章  >  php教程  >  Linux下實作免密登入

Linux下實作免密登入

高洛峰
高洛峰原創
2016-10-12 16:14:001255瀏覽

1.Linux下產生金鑰 

  ssh-keygen的指令手冊,透過」man ssh-keygen「指令:

Linux下實作免密登入

  〜透過指令」ssh使用者的根目錄產生一個「.ssh」的資料夾

  Linux下實作免密登入

  進入「.ssh」會產生以下幾個檔案

Linux下實作免密登入『檔案記錄多台機器的公鑰

  id_rsa : 產生的私鑰檔案

  id_rsa.pub : 產生的公鑰檔案

  know_hosts : 已知的主機帳號符合至少兩個條件:

Linux下實作免密登入      1) .ssh目錄的權限必須是700 

      2) .ssh/authorized_keys文件檔案必須為600 2)


    

   常用以下幾種方法:

    2.1 透過ssh-copy-id的方式

    指令: ssh-copy-id -i ~/.ssh/id_rsa.put 舉例  

[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

Linux下實作免密登入to make sure we haven't added extra keys that you weren't expecting.

[root@test .ssh]# ssh root@192.168.91.13555:4135log 2016 from 192.168.91.133

[root@localhost ~]#

    常見錯誤:

      。。＀[root-7.19. .135

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

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

㜀2個

『21㟜〜㟎㟜〜㟜㟎21㟜〜㟎㟜〜2㜀㟜〜㟣㟜〜2㟜〜㟜㟜〜)
      指令:scp -p ~/.ssh/id_rsa.pub root@
:/root/.ssh/authorized_keys

      舉例:

[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:000
[root@test .s [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.133337_y3333G; #

 

      也可分為兩步驟操作:

$ scp ~/.ssh/id_rsa.pub root@

:pub_key     將檔案」 > 遠端/$ pub_sh/41/1/1/p pub_sh. /authorized_keys                     //將內容追加到authorized_keys檔案中, 不過登入遠端伺服器來執行此指令

  密  2.3透過批次實現批次

2.3.1 將需要做免密操作的機器hosts加到/etc/ansible/hosts下:
  [Avoid 4

2.3.2 執行指令進行免密操作

  ansible
-m authorized_key -a "user=root key='{{ lookup('file','/root/.ssh/id_rsa.pub') }}'" -k

範例:🀜 @test sshpass-1.05]# ansible test -m authorized_key -a "user=root key='{{ lookup('file','/root/.ssh/id_rsa.pub') }}'" -k  SSH password : ----->輸入密碼
  192.168.91.135 | success >> {
  "changed": true, 
  "key": "ssh-rsa     AAAAB3NzaC1yc2EAAAABIwAAAQEArZI4kxlYuw7j1nt5ueIpTPWfGBJoZ8Mb02OJHR8yGW7A3izwT3/uhkK7RkaGavBbAlprp5bxp3i0TyNxa/apBQG5NiqhYO8YCuiGYGsQAGwZCBlNLF3gq1/18B6FV5moE/8yTbFA4dBQahdtVP  PejLlSAbb5ZoGK8AtLlcRq49IENoXB99tnFVn3gMM0aX24ido1ZF9RfRWzfYF7bVsLsrIiMPmVNe5KaGL9kZ0svzoZ708yjWQQCEYWp0m+sODbtGPC34HMGAHjFlsC/SJffLuT/ug /hhCJUYeExHIkJF8OyvfC6DeF7ArI6zdKER7D8M0SM  WQmpKUltj2nltuv3w== root@localhost.localdomain", 
。 , 
  "manage_dir": true, 
  "path": null, 
  " state": "present", 
  "unique": false, 
  "user": "root"
  }
  [root@test sshpass-1.05]#34345% _rsa.pub文件的內容拷貝至遠端伺服器的~/.ssh/authorized_keys檔案中



陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn