ssh-key生成密鑰及SSH無密碼登錄的配置


文章作者:foochane

原文鏈接:https://foochane.cn/article/2019061601.html

1 ssh-keygen命令

ssh-keygen命令說明:

  • -t :指定加密類型(如:rea,dsa)
  • -C : 指定注釋,用於識別這個密鑰

其他參數具體可以查看幫助

$ ssh-keygen help
Too many arguments.
usage: ssh-keygen [-q] [-b bits] [-t dsa | ecdsa | ed25519 | rsa]
                  [-N new_passphrase] [-C comment] [-f output_keyfile]
       ssh-keygen -p [-P old_passphrase] [-N new_passphrase] [-f keyfile]
       ssh-keygen -i [-m key_format] [-f input_keyfile]
       ssh-keygen -e [-m key_format] [-f input_keyfile]
       ssh-keygen -y [-f input_keyfile]
       ssh-keygen -c [-P passphrase] [-C comment] [-f keyfile]
       ssh-keygen -l [-v] [-E fingerprint_hash] [-f input_keyfile]
       ssh-keygen -B [-f input_keyfile]
       ssh-keygen -F hostname [-f known_hosts_file] [-l]
       ssh-keygen -H [-f known_hosts_file]
       ssh-keygen -R hostname [-f known_hosts_file]
       ssh-keygen -r hostname [-f input_keyfile] [-g]
       ssh-keygen -G output_file [-v] [-b bits] [-M memory] [-S start_point]
       ssh-keygen -T output_file -f input_file [-v] [-a rounds] [-J num_lines]
                  [-j start_line] [-K checkpt] [-W generator]
       ssh-keygen -s ca_key -I certificate_identity [-h] [-U]
                  [-D pkcs11_provider] [-n principals] [-O option]
                  [-V validity_interval] [-z serial_number] file ...
       ssh-keygen -L [-f input_keyfile]
       ssh-keygen -A
       ssh-keygen -k -f krl_file [-u] [-s ca_public] [-z version_number]
                  file ...
       ssh-keygen -Q -f krl_file file ...

實際情況中也用不到那么多參數,指定加密類型和注釋即可。
例如:

$ ssh-keygen -t rsa -C "myname@163.com"
Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\fucheng/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in C:\Users\fucheng/.ssh/id_rsa.
Your public key has been saved in C:\Users\fucheng/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:9OlHGn5uIlELfGIYXdWectiEV5XS2quWpD1qpd2QJC8 myname@163.com
The key's randomart image is:
+---[RSA 2048]----+
|       . ....o..=|
|      . .   ..+o |
|       +.    *+. |
|      ..=.oooo=. |
|       .S=+.=o. .|
|        .o.E * . |
|         .+ @ =  |
|        . .B.B . |
|         ..++ .  |
+----[SHA256]-----+

一般情況下不需要輸入密碼,直接回車即可。

執行完ssh-keygen之后會在,用戶目錄下的.ssh文件下,生成一個id_rsa文件和id_rsa.pub文件。

  • id_rsa文件是私鑰,要保存好,放在本地,私鑰可以生產公鑰,反之不行。
  • id_rsa.pub文件是公鑰,可以用於發送到其他服務器,或者git上。

2 ssh設置無密碼登錄服務器

將之前在本地生成的公鑰id_rsa.pub,發送到需要無密碼登錄的服務器,然后將id_rsa.pub的內容追加到服務器的~/.ssh/authorized_keys 文件中即可。

如果沒有.ssh目錄,創建一個就好,或者執行ssh localhost登錄本地,ssh會自動創建。

可以使用如下命令進行操作:

$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys 

另外如果想要無密碼登錄本地localhost,那么在本地執行上面的命令即可,執行之后再 ssh localhost 就不需要輸入密碼了。

3 設置ssh無密碼訪問git倉庫

注意這里訪問的主要是私有倉庫。

github為例,找到個人主頁,點擊[settings],找到[SSH and GPG keys] ,新建SSH keys,將本地id_rsa.pub的內容復制到key里面,tittle可以隨便填寫,這樣就配置好了。

找到要訪問的倉庫主頁,點擊Clone or Downloaduse Http換成use SSH,然后就會顯示對應的倉庫地址如:git@github.com:uername/xxxxx.git

使用該地址就可以在本地進行無密碼訪問倉庫了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM