【原创】Git -i 指定私钥文件【即如何告诉git要使用哪个私钥?】
问题背景:
使用git ssh管理项目文件时,系统默认会使用的私钥文件:/path/to/.ssh/id_rsa, 如果你的私钥不是这个文件,怎么办?
问题原因:
虽然git支持ssh 协议,但是官方并不支持类似 ssh -i 指令来指定要使用的key文件(私钥)。
解决方案:
借助一个shell脚本来实现,就可以方便的指定私钥文件,命令:gitsh -i /path/to/xxx_rsa git-command
补充说明:
可以将 /path/to/gitsh 脚本放到 /usr/bin/gitsh 并chmod 755,方便全局调用.
脚本源码:
# !/bin/bash # Copyright (c) 2013 Alvin Abad if [ $# -eq 0 ]; then echo "Git wrapper script that can specify an ssh-key file Usage: gitsh -i ssh-key-file git-command " exit 1 fi # remove temporary file on exit trap 'rm -f /tmp/.git_ssh.$$' 0 if [ "$1" = "-i" ]; then SSH_KEY=$2; shift; shift echo "ssh -i $SSH_KEY \$@" > /tmp/.git_ssh.$$ chmod +x /tmp/.git_ssh.$$ export GIT_SSH=/tmp/.git_ssh.$$ fi # in case the git command is repeated [ "$1" = "git" ] && shift # Run the git command git "$@"
版权声明:除非注明,本文由( blogdaren )原创,转载请保留文章出处。
发表评论: