访问gitlab从http方式改为ssh方式,gitlab使用的不是标准22端口

访问gitlab从http方式改为ssh方式,gitlab使用的不是标准22端口

2018.10.11 11:12:59字数 465阅读 6,675

方案一[推荐]

原地址

设置步骤:

  1. 本地进入.ssh查看是否存在密钥对:xxx和xxx.pub
    命令:cd ~/.ssh

  2. 如果不存在,使用ssh-keygen来创建
    命令:ssh-keygen -t rsa -C "youremail@youremail.com"
    例如:ssh-keygen -t rsa -C "youemail@163.com"
    注解:
    Enter file in which to save the key 输入保存秘钥的文件 直接enter即可
    Enter passphrase (empty for no passphrase) 输入密码 直接enter即可
    此时查看.ssh目录下可看到新增的一对秘钥id_rsa和id_rsa.pub

  3. 查看公钥
    命令: cat ~/.ssh/id_rsa.pub
    复制全部,包括后面的邮箱

  4. 添加到gitlab中
    左侧栏Profile Settings → 左侧栏SSH Keys → 粘贴并Add key

  5. 创建config,端口为22可忽略这一步
    命令:cat>~/.ssh/config
    输入:

    Host gitlab.xxx.com
    User git
    Port 458
    IdentityFile /home/yourname/.ssh/id_rsa(替换成你的id_rsa所在的路径,也可以不要)
    
  6. 更改remote
    使用git remote -v查看origin和upstream的url,

    把http更改为ssh地址,命令:git remote set-url origin(或者upstream) xxxx
    例如:

    git remote set-url origin git@gitlab.xxxx.com:xxx/server.git
    git remote set-url upstream git@gitlab.xxxx.com:enterprise/server.git
    
  7. 验证是否设置成功
    命令:ssh -T git@gitlab.xxxxxxxx.com
    显示Welcome to GitLab, yourname! 代表成功。

总结

以上是http改为ssh,若是一开始clone就是以ssh方式访问,方法见下:

同样第5步根据需要选择配置与否。

第6步改为

git clone git@gitlab.xxxx.com:xxx/server.git clone远程仓库到本地,称为origin

git remote add upstream http://gitlab.xxxx.com/enterprise/server.git 添加upstream

方案二

使用git clone命令clone项目时,如果repositorySSH端口不是标准22端口时(例如,SSH tunnel模式,等等),可以使用如下命令:

git clone ssh://git@hostname:port/.../xxx.git

举例如下:

git clone ssh://git@10.137.20.113:2222/root/test.git
# 后面的/var/opt/gitlab/git-data/repositories/developer/approve.git 是代码所有在的服务器路径
git clone ssh://root@42.62.11.190:4222/var/opt/gitlab/git-data/repositories/developer/approve.git
原文地址:https://www.cnblogs.com/mouseleo/p/13947863.html