--- title: '無密碼 login (一般\gitHub\AWS)' disqus: hackmd --- ## 無密碼登入 流程 1. **ssh-keygen** * 作用:產生公私鑰 * `無其他設定的話,連按6次Enter` 3. **ssh-copy-id** -i xxx.pub username@ip * 作用:將我們的公鑰傳 給 這台服務氣 * authorized_keys:紀錄 接收到的公鑰 * -i : 指定傳輸的公鑰 4. ssh userName@ip * 再次登入可已使用 法2 無密碼登入 概念 --- * ssh userName@ip 1. 傳送一個用 我方私鑰 [<font color="#f00">**加密**</font>] 的數據 userName@ip 2. 對方使用 傳過來的公鑰 [<font color="#f00">**解密**</font>] 數據 3. 對方使用 傳過來的公鑰 [<font color="#f00">**加密**</font>] 數據,再回傳給我方 4. 我方在 用 私鑰[<font color="#f00">**解密**</font>] 5. 連線 無密碼登入 概念圖 --- ```sequence Clinet->Server: 發送一個 用私鑰 加密的數據 Note right of Server: 解密傳輸過的數據 Server-->Clinet: 發送一個 用 client 公鑰 加密的數據 Note left of Clinet: 解密傳輸過的數據 ``` 建立公私鑰 --- 1. cd .ssh/ 2. ssh-keygen -t rsa >> 會產生兩個檔案 ,連按6次Enter * id_rsa: 私鑰 <font color="b90097">#私鑰要保存好 絕對不能被竊取</font> * id_rsa.pub : 公鑰 4. ssh-keygen **常用參數** * **-t** : 創建的密鑰類型 * **-f**: 指定密鑰對的文件名 無密碼登入 法1 --- * 登入server:ssh -i ~/.ssh/xxx username@ip * -i : 指定 用哪一隻私鑰 [解密\加密],通常可以不指定 * 第一次登入需要輸入密碼 * 登入AWS的server:ssh -i ~/.ssh/xxx.pem username@ip * 作法一樣,指定的密鑰的副檔名是**.pem** 無密碼登入 法2 --- 1. touch config <font color="b90097"> #.ssh目錄下建立 </font> 2. vim config <font color="b90097"> #可以放置多組login資訊</font> ```bash= host tos-esb Hostname 172.16.126.206 user sysadmin IdentityFile ~/.ssh/id_rsa IdentitiesOnly yes ``` >- host:自定義的名稱,登入用 >- hostname: 主機IP\域名 >- user: 登入該主機的 帳號名 >- IdentityFile: 指定使用哪一個[<font color="#f00">私鑰</font>] <font color="b90097"># ~/ =>家目錄</font> 3. ssh tos-esb 4. 第一次登入需要輸入密碼 建立AWS 的無密碼登入 --- 1. vim config ```bash= host xxxTest Hostname 主機IP\域名 user ubuntu IdentityFile ~/.ssh/XXX.pem IdentitiesOnly yes ``` >- IdentityFile: 這邊需指定為 AWS的密鑰對 >- 將 XXX.pem密鑰對的權限設定為: <font color='#f00'> chmod 700 XXX.pem</font> 2. ssh xxxTest ## github的無密碼登入 ```bash= host github.com user git Hostname github.com PreferredAuthentications publickey IdentityFile ~/.ssh/pouchenGit IdentitiesOnly yes port 22 ``` >- 需照上述的範本建立,只能修改 IdentityFile >- 須將 ~/.ssh/pouchenGit.pub(公鑰) 貼到 github上面 <font color="b90097">#這邊請 google如何在github設定 </font> ### 公司或網路環境封鎖port22 * 改用**SSH over HTTPS** ```bash= host github.com user git Hostname ssh.github.com PreferredAuthentications publickey IdentityFile ~/.ssh/pouchenGit IdentitiesOnly yes port 443 ``` * **調整** * **Hostname**:github.com -> `ssh.github.com` * **port**:22 -> `443` * **測試連線**:`ssh -T git@github.com` ## GitLab 設定 SSH Key 1. **檢查是否已有 SSH 金鑰** * **指令**:`ls -al ~/.ssh` * 看到類似 id_rsa 和 id_rsa.pub 的檔案,表示你已經有 **SSH 金鑰**,可以跳到「步驟三」 2. **產生新的 SSH 金鑰** * **指令**:`ssh-keygen -t ed25519 -C "your_email@example.com"` * 如果**系統不支援 ed25519**,可以改用上面寫的其他方式,來產生金鑰 * **無腦6次Enter** 4. **複製公鑰** * **複製** ~/.ssh/id_ed25519.pub 中的內容 4. **將 SSH 金鑰加到 GitLab** 1. 登入 GitLab。 2. 點右上角頭像 → Edit profile。 3. 左側選單點選 SSH Keys。 4. 將剛剛複製的公鑰貼上到「Key」欄位。 5. **可選擇設定** * **Title** * **Usage type** * `Authentication & Signing`:**推送程式碼**和**commit 簽名**都用這把鑰匙 * **Expiration date**:過期時間 * **可以不填**(`不會過期`) 7. 點選 Add key。 5. **測試連線** * **指令**:`ssh -T git@自家gitlab的網址` * **如果自架的gitlab的網址**:`git.nicola.tw` * ssh -T git@`git.nicola.tw` * **成功訊息**:Welcome to GitLab, @yourusername!