github官方操作文档:Generating a new SSH key and adding it to the ssh-agent - GitHub Docs
操作流程如下
1.生成一个新的ssh文件(your_email@example.com 替换为自己的邮箱)
2.添加公钥到github
复制公钥内容到剪贴板
打开浏览器,登录github.com ,右上角,点击settings
找到 SSH and GPG keys ,点击进入
点击添加
测试权限是否正常,能否通过ssh访问git
b.访问失败如下:
访问失败,需要检查公钥文件是否添加到github
其他机器通过指定秘钥文件访问【方式一】
1.创建目录,并拷贝秘钥文件该目录下,并修改为0600权限
2.启动ssh-agent代理,并添加私钥,然后进行测试
当ssh-agent进程结束时,将失去访问权限,若想继续访问,还需要重新执行操作
重新添加私有执行
其他机器通过指定秘钥文件访问【方式二】
通过 .gitconfig 配置文件进行配置,该配置针对git命令
core.sshCommand
If this variable is set, and will use the specified command instead of when they need to connect to a remote system. The command is in the same form as the environment variable and is overridden when the environment variable is set.
该操作会在用户家目录自动生成.gitconfig配置文件,内容如下
测试,需要指定克隆私有仓库进行测试,下图表示测试成功
其他机器通过指定秘钥文件访问【方式三】
通过 GIT_SSH_COMMAND 环境变量实现访问
takes precedence over , and is interpreted by the shell, which allows additional arguments to be included. on the other hand must be just the path to a program (which can be a wrapper shell script, if additional arguments are needed).
注意:GIT_SSH_COMMAND 的优先权大于 GIT_SSH
通过 GIT_SSH 环境变量实现访问
官方文档: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 ,则当git通过ssh连接主机是,调用GIT_SSH设置的脚本来替换默认的ssh命令
1.创建一个文件,内容如下 ~/.ssh_git/ssh-git.sh
2.添加可执行权限
3.通过添加私有方式进行访问
4.整理上面操作步骤,可总结一个脚本git.sh,内容如下:
执行操作如下: