Linux 和 Windows 下配置 SSH(複用已有的公鑰)

假如項目中添加了需要使用 ssh 方式連接服務器的 git 子模塊,則需要配置 ssh 公鑰,這裏我是直接複用之前用過的公鑰,這樣我們可以在多臺設備上使用同一份公鑰。

複用舊公鑰

~/ 目錄下新建 .ssh 目錄,然後複製已有的 id_rsaid_rsa.pub 到此目錄下,然後修改 id_rsa 的權限,不然會報錯 "Permissions 0644 for '/root/.ssh/id_rsa' are too open.",修改方式:

$ cd ~/.ssh
$ sudo chmod 0600 id_rsa

然後執行 ssh 指令來添加應用當前公鑰的服務器地址,例如 [email protected][email protected]

ssh -T [email protected]
The authenticity of host 'gitee.com (116.211.167.14)' can't be established.
ECDSA key fingerprint is SHA256:xxx
ECDSA key fingerprint is MD5:27:xxx
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitee.com,116.211.167.14' (ECDSA) to the list of known hosts.
Welcome to Gitee.com, linshuhe1!

~/.ssh 目錄下會生成一個 known_hosts 文件。

免密碼 git 操作

假如配置完了 ssh 然後執行 git pull 還需要輸入賬號密碼,說明在使用 git clone 指令拉取倉庫源碼時使用的地址是 HTTPS 格式而非 SSH 格式,兩個格式的差別在於:

推薦使用 SSH 格式拉取 git 工程。

解決 warning: push.default is unset

使用 ssh 管理工程之後,在執行 git push 提示:

git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

導致原因:push 的時候沒有指定要 push 修改的分支

解決方案:執行 git config --global push.default matching 即可,它的作用就是 push 的時候在沒有指定 branch 分支時,採用同名匹配的規則,默認 push 到與當前項目所在 branch 同名的分支上。

Window 下複用已有公鑰

將已有的公鑰和私鑰文件 id_rsaid_rsa.pub 放在當前用戶目錄下的 .ssh 文件夾下,然後在 Git bash 下配置倉庫服務器:
github 配置:

$ ssh -T [email protected]
Hi linshuhe! You've successfully authenticated, but GitHub does not provide shell access.

碼雲配置:

$ ssh -T [email protected]
The authenticity of host 'gitee.com (116.211.167.14)' can't be established.
ECDSA key fingerprint is SHA256:(...).
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitee.com,116.211.167.14' (ECDSA) to the list of known hosts.
Hi linshuhe1! You've successfully authenticated, but GITEE.COM does not provideshell access.

Coding 配置:

$ ssh -T [email protected]
The authenticity of host 'git.coding.net (118.25.166.124)' can't be established.
RSA key fingerprint is SHA256:(...).
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'git.coding.net,118.25.166.124' (RSA) to the list ofknown hosts.
Coding 提示: Hello linshuhe, You've connected to Coding.net via SSH. This is a personal key.
linshuhe,你好,你已經通過 SSH 協議認證 Coding.net 服務,這是一個個人公鑰

參考:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章