github官方操作文檔:Generating a new SSH key and adding it to the ssh-agent - GitHub Docs
操作流程如下
1.生成一個新的ssh文件(your_email@example.com 替換為自己的郵箱)
ssh-keygen -t ed25519 -C "your_email@example.com"
# 如果系統(tǒng)不支持 Ed25519 算法,可以使用下面方法創(chuàng)建
# ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# 如果需要給秘鑰設(shè)置密碼,也可以在這兩步的時候,設(shè)置密碼
> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]
2.添加公鑰到github
復(fù)制公鑰內(nèi)容到剪貼板
打開瀏覽器,登錄github.com ,右上角,點擊settings
?找到 SSH and GPG keys ,點擊進入
?點擊添加
?
?測試權(quán)限是否正常,能否通過ssh訪問git
ssh -T git@github.com
?a.訪問成功如下:
b.訪問失敗如下:
?訪問失敗,需要檢查公鑰文件是否添加到github
其他機器通過指定秘鑰文件訪問【方式一】?
?1.創(chuàng)建目錄,并拷貝秘鑰文件該目錄下,并修改為0600權(quán)限
mkdir -pv ~/.ssh_git
# 將私鑰文件拷貝到該目錄,并修改權(quán)限
chmod 0600 -R ~/.ssh_git
2.啟動ssh-agent代理,并添加私鑰,然后進行測試
eval "$(ssh-agent -s)"
ssh-add ~/.ssh_git/id_ed25519
ssh -T git@github.com
當(dāng)ssh-agent進程結(jié)束時,將失去訪問權(quán)限,若想繼續(xù)訪問,還需要重新執(zhí)行操作
?
?重新添加私有執(zhí)行
?其他機器通過指定秘鑰文件訪問【方式二】?
通過 .gitconfig 配置文件進行配置,該配置針對git命令
core.sshCommand
If this variable is set,?git fetch
?and?git push
?will use the specified command instead of?ssh
?when they need to connect to a remote system. The command is in the same form as the?GIT_SSH_COMMAND
?environment variable and is overridden when the environment variable is set.
git config --global core.sshCommand 'ssh -i ~/.ssh_git/id_ed25519 -p 22'
該操作會在用戶家目錄自動生成.gitconfig配置文件,內(nèi)容如下
測試,需要指定克隆私有倉庫進行測試,下圖表示測試成功
?其他機器通過指定秘鑰文件訪問【方式三】?
通過?GIT_SSH_COMMAND 環(huán)境變量實現(xiàn)訪問
?$GIT_SSH_COMMAND
?takes precedence over?$GIT_SSH
, and is interpreted by the shell, which allows additional arguments to be included.?$GIT_SSH
?on the other hand must be just the path to a program (which can be a wrapper shell script, if additional arguments are needed).
export GIT_SSH_COMMAND="ssh -i ~/.ssh_git/id_ed25519 -p 22"
注意:GIT_SSH_COMMAND? 的優(yōu)先權(quán)大于?GIT_SSH
通過?GIT_SSH 環(huán)境變量實現(xiàn)訪問
官方文檔:Git - git Documentation
? ? ? GIT_SSH, if specified, is a program that is invoked instead of ssh when Git tries to connect to an SSH host. It is invoked like $GIT_SSH [username@]host [-p <port>] <command>. Note that this isn’t the easiest way to customize how ssh is invoked; it won’t support extra command-line parameters, so you’d have to write a wrapper script and set GIT_SSH to point to it. It’s probably easier just to use the ~/.ssh/config file for that.
?大概意思指定了 GIT_SSH ,則當(dāng)git通過ssh連接主機是,調(diào)用GIT_SSH設(shè)置的腳本來替換默認的ssh命令
1.創(chuàng)建一個文件,內(nèi)容如下?~/.ssh_git/ssh-git.sh
vim ~/.ssh_git/ssh-git.sh
#!/bin/bash
if [ -z "$PKEY" ]; then
# if PKEY is not specified, run ssh using default keyfile
ssh "$@"
else
ssh -i "$PKEY" -p 22 "$@"
fi
2.添加可執(zhí)行權(quán)限
chmod a+x ~/.ssh_git/ssh-git.sh
3.通過添加私有方式進行訪問
export GIT_SSH=~/.ssh_git/ssh-git.sh
PKEY=~/.ssh_git/id_ed25519 git clone git@github.com:nineaiyu/scorems.git
?4.整理上面操作步驟,可總結(jié)一個腳本git.sh,內(nèi)容如下:
#!/bin/bash
#
if [ $# -eq 0 ]; then
echo "git.sh -i ssh-key-file git-command"
exit 1
fi
git_ssh_tmp=~/.git_ssh.tmp
trap "rm -f ${git_ssh_tmp}" 0
if [ "$1" = "-i" ]; then
SSH_KEY=$2
shift
shift
echo "ssh -i $SSH_KEY -p 22 \$@" > ${git_ssh_tmp}
chmod +x ${git_ssh_tmp}
export GIT_SSH=${git_ssh_tmp}
fi
[ "$1" = "git" ] && shift
git "$@"
執(zhí)行操作如下:文章來源:http://www.zghlxwxcb.cn/news/detail-819652.html
chmod a+x git.sh
./git.sh -i ~/.ssh_git/id_ed25519 clone git@github.com:nineaiyu/scorems.git
?文章來源地址http://www.zghlxwxcb.cn/news/detail-819652.html
到了這里,關(guān)于git通過SSH指定秘鑰文件克隆代碼的三種方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!