git clone权限配置

1,115 阅读1分钟

问题

git clone报错:

git@github.com: Permission denied (publickey).

建议分环境创建github(个人账号)和gitlab(公司账号)

github账号

输入命令:

ssh-keygen -t rsa

提示输入文件名(绝对路径):

github_rsa

然后一路回车,就生成了相关文件:

cat ~/.ssh/github_rsa.pub

前往GitHub登陆,依次点击"Setting -> SSH Keys"->"New SSH key",粘贴上述文件内容。

gitlab账号

输入命令:

ssh-keygen -t rsa -C "公司邮箱"

提示输入文件名(绝对路径):

gitlab_rsa

然后一路回车,就生成了相关文件:

cat ~/.ssh/gitlab_rsa.pub

前往公司gitlab登陆,进入SSH Keys添加上述文件内容。

配置config

vi ~/.ssh/config

# gitlab
Host gitlab
    User git
    HostName git.公司域名.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/gitlab_rsa
    ServerAliveInterval 300
    ServerAliveCountMax 10

# github
Host github
    User git
    HostName github.com
    Port 22
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/github_rsa
    ServerAliveInterval 300
    ServerAliveCountMax 10

# github.com 若某些网络连不上可换443端口,不需要则注释掉下面
Host github.com
    User git
    HostName ssh.github.com
    Port 443
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/github_rsa
    ServerAliveInterval 300
    ServerAliveCountMax 10

检查是否通过:

ssh -T git@github.com

若某些网络连不上可换443端口: ssh -T -p 443 git@ssh.github.com

Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.

ssh -T git@git.公司域名.com

Welcome to GitLab, xxx!

不通过则添加:

ssh-add -k ~/.ssh/github_rsa

ssh-add -k ~/.ssh/gitlab_rsa

若机器重启可能ssh-add缓存失效,可将以上两句加入.bash_profile中。