訪問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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM