有一臺Linux伺服器,當我在本地的電腦上通過git上傳代碼到這台Linux伺服器時,希望實現免密登錄,就需要用到SSH秘鑰, 首先,你需要確認自己是否已經擁有密鑰。 預設情況下,用戶的 SSH 密鑰存儲在其 ~/.ssh 目錄下,打開git,輸入命令: $ cd ~/.ssh $ ls id_ds ...
有一臺Linux伺服器,當我在本地的電腦上通過git上傳代碼到這台Linux伺服器時,希望實現免密登錄,就需要用到SSH秘鑰,
首先,你需要確認自己是否已經擁有密鑰。 預設情況下,用戶的 SSH 密鑰存儲在其 ~/.ssh
目錄下,打開git,輸入命令:
$ cd ~/.ssh $ ls id_dsa
id_dsa.pub
known_hosts
其中id_dsa
是私鑰,id_dsa
.pub
是你的公鑰,如果找不到這樣的文件(或者根本沒有 .ssh
目錄),你可以通過運行 ssh-keygen
程式來創建它們:
$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/schacon/.ssh/id_rsa): Created directory '/home/schacon/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/schacon/.ssh/id_rsa. Your public key has been saved in /home/schacon/.ssh/id_rsa.pub. The key fingerprint is: d0:82:24:8e:d7:f1:bb:9b:33:53:96:93:49:da:9b:e3 [email protected]
首先 ssh-keygen
會確認密鑰的存儲位置(預設是 .ssh/id_rsa
),然後它會要求你輸入兩次密鑰口令。如果你不想在使用密鑰時輸入口令,將其留空即可。
查看公鑰具體內容:
$ cat ~/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSU GPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSwg0cda3 Pbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XA t3FaoJoAsncM1Q9x5+3V0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/En mZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbx NrRFi9wrf+M7Q== [email protected]
然後就是遠程免密登錄的配置,介紹兩種常用方法:
方法一:
通過 ssh-copy-id 命令設置,最後一個參數是我們要免密鑰登錄的伺服器 ip 地址。
ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.1.1
方法二:
通過手工複製,將本地 id_rsa.pub 文件的內容複製到遠程伺服器的 /.ssh/authorized_keys 文件中也完全可以的
$ cd .ssh $ ls authorized_keys id_rsa id_rsa.pub known_hosts $ vi authorized_keys
到此,SSH免密鑰登錄就大功告成。