阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Git仓库服务器SSH认证示例

461次阅读
没有评论

共计 7863 个字符,预计需要花费 20 分钟才能阅读完成。

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

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

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

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

方法一 示例、

git 仓库服务器,新建仓库,测试机获取 git 仓库,修改,远程上传。ssh 方式
[root@Linuxidc ~]# useradd -d /opt/gitServer gitServer
[root@Linuxidc ~]# echo “git”|passwd –stdin gitServer
更改用户 gitServer 的密码。
passwd:所有的身份验证令牌已经成功更新。
[root@Linuxidc ~]# yum install git -y
[root@Linuxidc ~]# su – gitServer
[gitServer@Linuxidc ~]$ ls
[gitServer@Linuxidc ~]$ mkdir TestProject.git
[gitServer@Linuxidc ~]$ cd TestProject.git/
[gitServer@Linuxidc TestProject.git]$ git –bare init
Initialized empty Git repository in /opt/gitServer/TestProject.git/
[gitServer@Linuxidc TestProject.git]$ ls
branches  config  description  HEAD  hooks  info  objects  refs

客户测试机

[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 仓库服务端

[gitServer@Linuxidc 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

[root@Linuxidc ~]# mkdir /opt/tt
[root@Linuxidc ~]# cd /opt/tt
[root@Linuxidc 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@Linuxidc tt]# ls
TestProject
[root@Linuxidc tt]# cd TestProject/
[root@Linuxidc TestProject]# ls
test.file
[root@Linuxidc TestProject]# cat test.file
test file abc
[root@Linuxidc TestProject]#

方法二  示例、

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

[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 ~]#

[root@node1 ~]# su – user1
[user1@node1 ~]$ ssh-keygen -t rsa
[root@node1 ~]# su – user2
[user1@node1 ~]$ ssh-keygen -t rsa

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

[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 仓库服务侧

[gitServer@Linuxidc ~]$ mkdir .ssh && chmod 700 .ssh
[gitServer@Linuxidc ~]$ touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys

[gitServer@Linuxidc ~]$ 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@Linuxidc ~]$

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

[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 ~]$

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

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’.

试着将如下配置新增到

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

再次提交

GitHub 教程系列文章

GitHub 使用教程图文详解  http://www.linuxidc.com/Linux/2014-09/106230.htm 

Git 标签管理详解 http://www.linuxidc.com/Linux/2014-09/106231.htm 

Git 分支管理详解 http://www.linuxidc.com/Linux/2014-09/106232.htm 

Git 远程仓库详解 http://www.linuxidc.com/Linux/2014-09/106233.htm 

Git 本地仓库(Repository)详解 http://www.linuxidc.com/Linux/2014-09/106234.htm 

Git 服务器搭建与客户端安装  http://www.linuxidc.com/Linux/2014-05/101830.htm 

Git 概述 http://www.linuxidc.com/Linux/2014-05/101829.htm 

分享实用的GitHub 使用教程 http://www.linuxidc.com/Linux/2014-04/100556.htm 

Ubuntu 下 Git 服务器的搭建与使用指南  http://www.linuxidc.com/Linux/2015-07/120617.htm

Git 的详细介绍:请点这里
Git 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-10/123850.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计7863字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中

星哥玩云

星哥玩云
星哥玩云
分享互联网知识
用户数
4
文章数
19350
评论数
4
阅读量
7961505
文章搜索
热门文章
星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛 NAS-6:抖音视频同步工具,视频下载自动下载保存 前言 各位玩 NAS 的朋友好,我是星哥!...
星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛 NAS-3:安装飞牛 NAS 后的很有必要的操作 前言 如果你已经有了飞牛 NAS 系统,之前...
我把用了20年的360安全卫士卸载了

我把用了20年的360安全卫士卸载了

我把用了 20 年的 360 安全卫士卸载了 是的,正如标题你看到的。 原因 偷摸安装自家的软件 莫名其妙安装...
再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见 zabbix!轻量级自建服务器监控神器在 Linux 的完整部署指南 在日常运维中,服务器监控是绕不开的...
飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛 NAS 中安装 Navidrome 音乐文件中文标签乱码问题解决、安装 FntermX 终端 问题背景 ...
阿里云CDN
阿里云CDN-提高用户访问的响应速度和成功率
随机文章
告别Notion焦虑!这款全平台开源加密笔记神器,让你的隐私真正“上锁”

告别Notion焦虑!这款全平台开源加密笔记神器,让你的隐私真正“上锁”

  告别 Notion 焦虑!这款全平台开源加密笔记神器,让你的隐私真正“上锁” 引言 在数字笔记工...
12.2K Star 爆火!开源免费的 FileConverter:右键一键搞定音视频 / 图片 / 文档转换,告别多工具切换

12.2K Star 爆火!开源免费的 FileConverter:右键一键搞定音视频 / 图片 / 文档转换,告别多工具切换

12.2K Star 爆火!开源免费的 FileConverter:右键一键搞定音视频 / 图片 / 文档转换...
一句话生成拓扑图!AI+Draw.io 封神开源组合,工具让你的效率爆炸

一句话生成拓扑图!AI+Draw.io 封神开源组合,工具让你的效率爆炸

一句话生成拓扑图!AI+Draw.io 封神开源组合,工具让你的效率爆炸 前言 作为天天跟架构图、拓扑图死磕的...
我把用了20年的360安全卫士卸载了

我把用了20年的360安全卫士卸载了

我把用了 20 年的 360 安全卫士卸载了 是的,正如标题你看到的。 原因 偷摸安装自家的软件 莫名其妙安装...
开源MoneyPrinterTurbo 利用AI大模型,一键生成高清短视频!

开源MoneyPrinterTurbo 利用AI大模型,一键生成高清短视频!

  开源 MoneyPrinterTurbo 利用 AI 大模型,一键生成高清短视频! 在短视频内容...

免费图片视频管理工具让灵感库告别混乱

一言一句话
-「
手气不错
仅2MB大小!开源硬件监控工具:Win11 无缝适配,CPU、GPU、网速全维度掌控

仅2MB大小!开源硬件监控工具:Win11 无缝适配,CPU、GPU、网速全维度掌控

还在忍受动辄数百兆的“全家桶”监控软件?后台偷占资源、界面杂乱冗余,想查个 CPU 温度都要层层点选? 今天给...
手把手教你,购买云服务器并且安装宝塔面板

手把手教你,购买云服务器并且安装宝塔面板

手把手教你,购买云服务器并且安装宝塔面板 前言 大家好,我是星哥。星哥发现很多新手刚接触服务器时,都会被“选购...
星哥带你玩飞牛NAS-11:咪咕视频订阅部署全攻略

星哥带你玩飞牛NAS-11:咪咕视频订阅部署全攻略

星哥带你玩飞牛 NAS-11:咪咕视频订阅部署全攻略 前言 在家庭影音系统里,NAS 不仅是存储中心,更是内容...
星哥带你玩飞牛NAS-12:开源笔记的进化之路,效率玩家的新选择

星哥带你玩飞牛NAS-12:开源笔记的进化之路,效率玩家的新选择

星哥带你玩飞牛 NAS-12:开源笔记的进化之路,效率玩家的新选择 前言 如何高效管理知识与笔记,已经成为技术...
安装并使用谷歌AI编程工具Antigravity(亲测有效)

安装并使用谷歌AI编程工具Antigravity(亲测有效)

  安装并使用谷歌 AI 编程工具 Antigravity(亲测有效) 引言 Antigravity...