git仓库服务器SSH认证示例

简介:

git在用户管理及管理上,下面上三种解决办法:

 如果需要团队里的每个人都对仓库有写权限,又不能给每个人在服务器上建立账户,那么提供 SSH 连接就是唯一的选择了。我们假设用来共享仓库的服务器已经安装了 SSH 服务,而且你通过它访问服务器。  
  
有好几个办法可以让团队的每个人都有访问权。


第一个办法是给每个人建立一个账户,直截了当但过于繁琐。反复的运行 adduser 并且给所有人设定临时密码可不是好玩的。  
   
第二个办法是在主机上建立一个 git 账户,让每个需要写权限的人发送一个 SSH 公钥,然后将其加入 git 账户的 ~/.ssh /authorized_keys 文件。这样一来,所有人都将通过 git 账户访问主机。这丝毫不会影响提交的数据——访问主机用的身份不会影响 commit的记录。  
   
另一个办法是让 SSH 服务器通过某个 LDAP 服务,或者其他已经设定好的集中授权机制,来进行授权。只要每个人都能获得主机的 shell 访问权,任何可用的 SSH 授权机制都能达到相同效  # 如果需要团队里的每个人都对仓库有写权限,又不能给每个人在服务器上建立账户,那么提供 SSH 连接就是唯一的选择了。我们假设用来共享仓库的服务器已经安装了 SSH 服务,而且你通过它访问服务器。  
   

git 共享仓库服务器: Aries.lansgg.com 192.168.100.128

git 客户测试机:    node1.lansgg.com    192.168.100.129


方法一 示例、

git 仓库服务器,新建仓库,测试机获取git仓库,修改,远程上传。ssh 方式

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@Aries ~] # useradd -d /opt/gitServer gitServer
[root@Aries ~] # echo "git"|passwd --stdin gitServer
更改用户 gitServer 的密码 。
passwd : 所有的身份验证令牌已经成功更新。
[root@Aries ~] # yum install git -y
[root@Aries ~] # su - gitServer
[gitServer@Aries ~]$  ls
[gitServer@Aries ~]$  mkdir  TestProject.git
[gitServer@Aries ~]$  cd  TestProject.git/
[gitServer@Aries TestProject.git]$ git --bare init
Initialized empty Git repository  in  /opt/gitServer/TestProject .git/
[gitServer@Aries TestProject.git]$  ls
branches  config  description  HEAD  hooks  info  objects  refs

客户测试机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[root@node1 ~] # useradd -d /opt/gitServer gitServer
[root@node1 ~] # echo "gitServer" |passwd --stdin gitServer
更改用户 gitServer 的密码 。
passwd : 所有的身份验证令牌已经成功更新。
[root@node1 ~] # su - gitServer
[root@node1 ~] # git clone gitServer@192.168.100.128:/opt/gitServer/TestProject.git
Initialized empty Git repository  in  /root/TestProject/ .git/
The authenticity of host  '192.168.100.128 (192.168.100.128)'  can't be established.
RSA key fingerprint is 9f:32:3a:b0:db:03:b6:c8:fc:a0:47:6c:e5:d1:b0:6a.
Are you sure you want to  continue  connecting ( yes /no )?  yes
Warning: Permanently added  '192.168.100.128'  (RSA) to the list of known hosts.
gitServer@192.168.100.128's password: 
warning: You appear to have cloned an empty repository.
[root@node1 ~] # ls
anaconda-ks.cfg   install .log   install .log.syslog  TestProject
[root@node1 ~] # cd TestProject/
[root@node1 TestProject] # echo "test file" > test.file
[root@node1 TestProject] # git add test.file 
[root@node1 TestProject] # git config --global user.name "gitServer"
[root@node1 TestProject] # git config --global user.email git@lansgg.com
[root@node1 TestProject] # git commit -m "test commit" test.file
[master 96bf273]  test  commit
  1 files changed, 1 insertions(+), 1 deletions(-)
[gitServer@node1 TestProject]$ git remote add test_remote_origin  ssh : //192 .168.100.128 /opt/gitServer/TestProject .git
[gitServer@node1 TestProject]$ git push test_remote_origin master
gitServer@192.168.100.128's password: 
Counting objects: 5,  done .
Writing objects: 100% (3 /3 ), 252 bytes,  done .
Total 3 (delta 0), reused 0 (delta 0)
To  ssh : //192 .168.100.128 /opt/gitServer/TestProject .git
    7e2e4a4..96bf273  master -> master

git 仓库服务端

1
2
3
4
5
6
[gitServer@Aries TestProject.git]$ git log
commit 96bf2738c6602283ea91778b999f7adf66c0082c
Author: gitServer <gitServer@lansgg.com>
Date:   Tue Sep 22 17:05:12 2015 +0800
 
     test  commit

我们可以随便找个目录clone下,看看是否存在 刚刚提交的 test.file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@Aries ~] # mkdir /opt/tt
[root@Aries ~] # cd /opt/tt
[root@Aries tt] # git clone gitServer@192.168.100.128:/opt/gitServer/TestProject.git
Initialized empty Git repository  in  /opt/tt/TestProject/ .git/
The authenticity of host  '192.168.100.128 (192.168.100.128)'  can't be established.
RSA key fingerprint is 9f:32:3a:b0:db:03:b6:c8:fc:a0:47:6c:e5:d1:b0:6a.
Are you sure you want to  continue  connecting ( yes /no )?  yes
Warning: Permanently added  '192.168.100.128'  (RSA) to the list of known hosts.
gitServer@192.168.100.128's password: 
remote: Counting objects: 6,  done .
remote: Compressing objects: 100% (2 /2 ),  done .
Receiving objects: 100% (6 /6 ), 435 bytes,  done .
remote: Total 6 (delta 0), reused 0 (delta 0)
[root@Aries tt] # ls
TestProject
[root@Aries tt] # cd TestProject/
[root@Aries TestProject] # ls
test . file
[root@Aries TestProject] # cat test.file 
test  file  abc
[root@Aries TestProject] #

方法二  示例、

在测试机创建两个账户 user1 user2,分别将秘钥上传至git 仓库侧,

1
2
3
4
5
6
7
8
9
[root@node1 ~] # useradd -d /opt/user1 user1
[root@node1 ~] # echo "user1" |passwd --stdin user1
更改用户 user1 的密码 。
passwd : 所有的身份验证令牌已经成功更新。
[root@node1 ~] # useradd -d /opt/user2 user2
[root@node1 ~] # echo "user2" |passwd --stdin user2
更改用户 user2 的密码 。
passwd : 所有的身份验证令牌已经成功更新。
[root@node1 ~] #
1
2
3
4
[root@node1 ~] # su - user1
[user1@node1 ~]$  ssh -keygen -t rsa
[root@node1 ~] # su - user2
[user1@node1 ~]$  ssh -keygen -t rsa

分别将user1、user2 用户的公钥新增git仓库服务器侧的

1
2
3
4
5
6
7
8
9
[user2@node1 . ssh ]$  pwd
/opt/user2/ . ssh
[user2@node1 . ssh ]$ ll
总用量 8
-rw-------. 1 user2 user2 1671 9月  22 17:18 id_rsa
-rw-r--r--. 1 user2 user2  404 9月  22 17:18 id_rsa.pub
[user2@node1 . ssh ]$  cat  id_rsa.pub 
ssh -rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAp0Im8iL7UR2b0PWrJ98YY /nqvjnuYWNc2F52SYn1/WA8rwGBWW0WBmKMoyW8YfSpCVk7QbyhX48Y3KF/Gf16CWRMm8xuyA +S5Seq3ZGnLbbVhb0OMO8VDAldovnIuPdI6005+ux /WbG1FKr3WxGs5k92ZO9hbXxpcVAwpvHY47t1v2LH5fW2jThypWMolUdp9TaNy7FkD2zaUNhbdqM1w67OSydiHAMfj183sEso9TykiXJvwlJeLdUMFywPTwfVqu2rxV0lY68B2mwr1pl5mcGPA4/0ruX8vSFsFLev8 +yi7LjccChAu /suPIFGLqRXrkW8ymsN/l3CkldnS9Y0BQ == user2@node1.lansgg.com
[user2@node1 . ssh ]$

git 仓库服务侧

1
2
[gitServer@Aries ~]$  mkdir  . ssh  &&  chmod  700 . ssh
[gitServer@Aries ~]$  touch  . ssh /authorized_keys  &&  chmod  600 . ssh /authorized_keys
1
2
3
4
[gitServer@Aries ~]$  cat  . ssh /authorized_keys 
ssh -rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA1pII1U64N /wl1OXotWdcU8d8 +ad0q6tkqdBgXLcR5zqXIq9PPe1NeLJ5HS9UIvZeN /LEyXGYh +fyg8tFQ+2PN3CmxnVwwcciyl1AKAgTeKUdleh8qcXPZkI0YZBpgTbuYWYHNjA6Qd9cvJSdKe9cVvwsv7N1z17Mx1uIfNSuSZ9e4XqUsJksBAzAYEGar4S13+Y /il7lquwkrdVBiWfWHmf/WoeY2RnzNBe9YtPVFUPL8HEoYyYaU +YXXMZKOZ8JwuLu1CPDJHTquSTyqdEwmgJWDdoiipgtyVOEVGZC0CqV16M2YpVqw26rrZ+nXUQYEnTrWyIiqt8 /xvzmeDIf0Q == user1@node1.lansgg.com
ssh -rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAp0Im8iL7UR2b0PWrJ98YY /nqvjnuYWNc2F52SYn1/WA8rwGBWW0WBmKMoyW8YfSpCVk7QbyhX48Y3KF/Gf16CWRMm8xuyA +S5Seq3ZGnLbbVhb0OMO8VDAldovnIuPdI6005+ux /WbG1FKr3WxGs5k92ZO9hbXxpcVAwpvHY47t1v2LH5fW2jThypWMolUdp9TaNy7FkD2zaUNhbdqM1w67OSydiHAMfj183sEso9TykiXJvwlJeLdUMFywPTwfVqu2rxV0lY68B2mwr1pl5mcGPA4/0ruX8vSFsFLev8 +yi7LjccChAu /suPIFGLqRXrkW8ymsN/l3CkldnS9Y0BQ == user2@node1.lansgg.com
[gitServer@Aries ~]$

客户测试机  无需用户名、密码

1
2
3
4
5
6
7
8
9
10
11
[user1@node1 ~]$ git clone gitServer@192.168.100.128: /opt/gitServer/TestProject .git
Initialized empty Git repository  in  /opt/user1/TestProject/ .git/
The authenticity of host  '192.168.100.128 (192.168.100.128)'  can't be established.
RSA key fingerprint is 9f:32:3a:b0:db:03:b6:c8:fc:a0:47:6c:e5:d1:b0:6a.
Are you sure you want to  continue  connecting ( yes /no )?  yes
Warning: Permanently added  '192.168.100.128'  (RSA) to the list of known hosts.
remote: Counting objects: 9,  done .
remote: Compressing objects: 100% (3 /3 ),  done .
remote: Total 9 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (9 /9 ),  done .
[user1@node1 ~]$


如果提交过程:提示如下错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Counting objects: 3,  done .
Writing objects: 100% (3 /3 ), 247 bytes | 0 bytes /s done .
Total 3 (delta 0), reused 0 (delta 0)
remote: error: refusing to update checked out branch: refs /heads/master
remote: error: By default, updating the current branch  in  a non-bare repository
remote: error: is denied, because it will  make  the index and work tree inconsistent
remote: error: with what you pushed, and will require  'git reset --hard'  to match
remote: error: the work tree to HEAD.
remote: error:
remote: error: You can  set  'receive.denyCurrentBranch'  configuration variable to
remote: error:  'ignore'  or  'warn'  in  the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed  in  some
remote: error: other way.
remote: error:
remote: error: To squelch this message and still keep the default behaviour,  set
remote: error:  'receive.denyCurrentBranch'  configuration variable to  'refuse' .

试着将如下配置新增到

1
2
3
[git@JumpServer1 pl.git]$  cat  .git /config 
[receive]
     denyCurrentBranch = ignore

再次提交



本文转自 西索oO 51CTO博客,原文链接:http://blog.51cto.com/lansgg/1697247

相关文章
|
27天前
|
存储 开发工具 git
[Git] 深入理解 Git 的客户端与服务器角色
Git 的核心设计理念是分布式,每个仓库既可以是客户端也可以是服务器。通过 GitHub 远程仓库和本地仓库的协作,Git 实现了高效的版本管理和代码协作。GitHub 作为远程裸仓库,存储项目的完整版本历史并支持多客户端协作;本地仓库则通过 `.git` 文件夹独立管理版本历史,可在离线状态下进行提交、回滚等操作,并通过 `git pull` 和 `git push` 与远程仓库同步。这种分布式特性使得 Git 在代码协作中具备强大的灵活性和可靠性。
51 18
[Git] 深入理解 Git 的客户端与服务器角色
|
28天前
|
网络安全 Apache 开发工具
图解Git——服务器上的Git《Pro Git》
Git 远程仓库及通信协议简介:远程仓库为团队协作提供平台,支持共享代码。常见形式为裸仓库,仅保存 Git 元数据。Git 支持多种协议,包括本地协议(适合局域网)、HTTP/HTTPS(推荐智能 HTTP,安全易用)、SSH(企业内部协作首选)和 Git 协议(高效只读访问)。选择协议需根据协作需求、安全性和配置难度权衡。此外,搭建 Git 服务器涉及创建裸仓库、上传至服务器、初始化共享仓库等步骤。生成 SSH 公钥、配置服务器及使用 GitWeb 或 GitLab 等工具可进一步增强功能。第三方托管服务如 GitHub 提供便捷的托管选项,适合快速启动和开源项目。总结而言,自行运行服务器提
38 11
|
1月前
|
Devops Shell 网络安全
git使用之如何将一套代码同时推送至github|gitee|gitcode|gitlab等多个仓库-含添加ssh-优雅草央千澈完美解决-提供整体提交代码
git使用之如何将一套代码同时推送至github|gitee|gitcode|gitlab等多个仓库-含添加ssh-优雅草央千澈完美解决-提供整体提交代码
85 16
git使用之如何将一套代码同时推送至github|gitee|gitcode|gitlab等多个仓库-含添加ssh-优雅草央千澈完美解决-提供整体提交代码
|
4月前
|
前端开发 开发工具 git
如何清理 docker 磁盘空间+修改 Gitea 服务器的 Webhook 设置+前端一些好学好用的代码规范-git hook+husky + commitlint
如何清理 docker 磁盘空间+修改 Gitea 服务器的 Webhook 设置+前端一些好学好用的代码规范-git hook+husky + commitlint
59 5
|
5月前
|
Linux 开发工具 数据安全/隐私保护
搭建 Git 私人服务器完整指南
本文详细介绍了如何从零开始搭建一个私人的 `Git` 服务器。首先,文章解释了 `Git` 的概念及其优势,并阐述了搭建私人 `Git` 服务器的重要性,包括数据隐私、定制化需求及成本效益。接着,文章分步骤指导读者完成服务器的准备工作,包括操作系统、硬件和网络要求。随后,详细介绍了在不同操作系统上安装 `Git` 的方法,并演示了如何创建 `git` 用户、部署仓库以及设置免密登录。此外,还提供了客户端连接远程仓库的具体步骤,包括 Linux 和 Windows 的操作方式。最后,文章探讨了迁移仓库的方法以及搭建 `Git` 服务器的一些进阶选项。
1049 0
搭建 Git 私人服务器完整指南
|
6月前
|
开发工具 git iOS开发
服务器配置Huggingface并git clone模型和文件
该博客提供了在服务器上配置Huggingface、安装必要的工具(如git-lfs和huggingface_hub库)、登录Huggingface以及使用git clone命令克隆模型和文件的详细步骤。
582 1
|
3天前
|
机器学习/深度学习 人工智能 程序员
阿里云出手DeepSeek拒绝服务器繁忙,程序员直呼:真香!
阿里云PAI平台支持一键部署DeepSeek-V3和DeepSeek-R1大模型,用户无需编写代码即可完成从训练到部署的全过程。通过PAI Model Gallery,开发者可轻松选择并部署所需模型版本,享受高效、便捷的AI开发体验。教程详细介绍了开通PAI、选择模型及一键部署的具体步骤,帮助用户快速上手。
|
4天前
|
机器学习/深度学习 存储 弹性计算
阿里云gpu云服务器租用价格:最新收费标准及活动价格参考
阿里云gpu云服务器多少钱?A10卡GN7i GPU云服务器32核188G3213.99/1个月起,V100卡GN6v GPU云服务器8核32G3830.00/1个月起,阿里云GPU云服务器是基于GPU应用的计算服务,多适用于视频解码,图形渲染,深度学习,科学计算等应用场景,该产品具有超强计算能力、网络性能出色、购买方式灵活、高性能实例存储( GA1和GN5特有)等特点。下面小编来介绍下阿里云gpu云服务器最新的收费标准及活动价格。
|
4天前
|
存储 机器学习/深度学习 人工智能
2025年阿里云GPU服务器租用价格、选型策略与应用场景详解
随着AI与高性能计算需求的增长,阿里云提供了多种GPU实例,如NVIDIA V100、A10、T4等,适配不同场景。2025年重点实例中,V100实例GN6v单月3830元起,适合大规模训练;A10实例GN7i单月3213.99元起,适用于混合负载。计费模式有按量付费和包年包月,后者成本更低。针对AI训练、图形渲染及轻量级推理等场景,推荐不同配置以优化成本和性能。阿里云还提供抢占式实例、ESSD云盘等资源优化策略,支持eRDMA网络加速和倚天ARM架构,助力企业在2025年实现智能计算的效率与成本最优平衡。 (该简介为原文内容的高度概括,符合要求的字符限制。)
|
4天前
|
机器学习/深度学习 存储 人工智能
2025年阿里云GPU服务器的租赁价格与选型指南
随着AI、深度学习等领域的发展,GPU服务器成为企业及科研机构的核心算力选择。阿里云提供多种GPU实例类型(如NVIDIA V100、A100等),涵盖计算型、共享型和弹性裸金属等,满足不同场景需求。本文详解2025年阿里云GPU服务器的核心配置、价格策略及适用场景,帮助用户优化选型与成本控制,实现高效智能计算。