当前位置: 首页 > 工具软件 > Gitosis > 使用案例 >

使用gitosis搭建git服务

微生令雪
2023-12-01

新建git用户

[root@izuf692vlrxk6gog2zjns8z /]# useradd git

#为git用户设置密码
[root@izuf692vlrxk6gog2zjns8z /]# passwd git
更改用户 git 的密码 。
新的 密码:
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。

安装软件

#安装git
[root@izuf692vlrxk6gog2zjns8z /]# yum install git
#安装python环境
[root@izuf692vlrxk6gog2zjns8z /]# sudo yum install python python-setuptools
#下载gitosis
[root@izuf692vlrxk6gog2zjns8z /]# cd ~
[root@izuf692vlrxk6gog2zjns8z ~]# git clone git://github.com/res0nat0r/gitosis.git
正克隆到 'gitosis'...
remote: Enumerating objects: 734, done.
remote: Total 734 (delta 0), reused 0 (delta 0), pack-reused 734
接收对象中: 100% (734/734), 147.40 KiB | 120.00 KiB/s, done.
处理 delta 中: 100% (458/458), done.
[root@izuf692vlrxk6gog2zjns8z ~]# ls
gitosis

编译gitosis

[root@izuf692vlrxk6gog2zjns8z ~]# cd gitosis

# 在gitosis目录里有setup.py文件
[root@izuf692vlrxk6gog2zjns8z gitosis]# python setup.py install

生成当前用户的ssh公钥

[root@izuf692vlrxk6gog2zjns8z gitosis]# ssh-keygen -t rsa -C 'xxxx@xx.com'

拷贝公钥到 /tmp/ 目录下

#xx为你的昵称
[root@izuf692vlrxk6gog2zjns8z ~]# cp ~/.ssh/id_rsa.pub /tmp/xx.pub

使用公钥初始化gitosis

#创建git文件夹
[root@izuf692vlrxk6gog2zjns8z ~]# cd /home/
[root@izuf692vlrxk6gog2zjns8z home] mkdir git 

#赋予/home/git 文件夹 git用户权限
[git@izuf692vlrxk6gog2zjns8z ~]# chown -R git:git /home/git/
[root@izuf692vlrxk6gog2zjns8z ~]# cd ~/gitosis/

[root@izuf692vlrxk6gog2zjns8z gitosis]# sudo -H -u git gitosis-init < /tmp/xxx.pub
初始化空的 Git 版本库于 /home/git/repositories/gitosis-admin.git/
重新初始化现存的 Git 版本库于 /home/git/repositories/gitosis-admin.git/

#切换到git用户,正确的话会出现gitosis 和repositories两个文件夹
[root@izuf692vlrxk6gog2zjns8z git]# su git
[git@izuf692vlrxk6gog2zjns8z ~]$ ls
gitosis  repositories

[git@izuf692vlrxk6gog2zjns8z ~]# sudo chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update 

切换回root用户,尝试拉取gitosis-admin.git

[root@izuf692vlrxk6gog2zjns8z ~]# git clone git@localhost:gitosis-admin.git
正克隆到 'gitosis-admin'...
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 5 (delta 0), reused 5 (delta 0)
接收对象中: 100% (5/5), done.

#进入gitosis-admin文件夹会发现有如下文件
[root@izuf692vlrxk6gog2zjns8z ~]# cd gitosis-admin/
[root@izuf692vlrxk6gog2zjns8z gitosis-admin]# ls
gitosis.conf  keydir

#查看gitosis.conf文件
[root@izuf692vlrxk6gog2zjns8z gitosis-admin]# cat gitosis.conf 
[gitosis]

[group gitosis-admin]
members = xxx@xx.com
writable = gitosis-admin 

接下来开始新建项目和用户, 例如新建了同事A,允许读取项目demo, 那么首先将同事A的公钥上传到giosis-admin的keydir里,并且在members里加入同事A的名称

#上传公钥到服务器
scp id_rsa.pub root@(ip地址):gitosis-admin/keydir/userA.pub

#修改gitosis-admin/gitosis.conf
[root@izuf692vlrxk6gog2zjns8z ~] cd gitosis-admin
[root@izuf692vlrxk6gog2zjns8z gitosis-admin] vim gitosis.conf
[gitosis]

[group gitosis-admin]
members = xxx
writable = gitosis-admin

[group demo]
members = userA xxx
writable = demo 

#提交gitosis.conf和keydir的修改到git仓库
[root@izuf692vlrxk6gog2zjns8z gitosis-admin]# git add .
[root@izuf692vlrxk6gog2zjns8z gitosis-admin]# git commit -m 'commit config'
[root@izuf692vlrxk6gog2zjns8z gitosis-admin]# git push origin master

#创建demo项目
[root@izuf692vlrxk6gog2zjns8z ~]# mkdir demo
[root@izuf692vlrxk6gog2zjns8z ~]# cd demo
[root@izuf692vlrxk6gog2zjns8z demo]# git init
初始化空的 Git 版本库于 /root/demo/.git/
[root@izuf692vlrxk6gog2zjns8z demo]# touch README.md
[root@izuf692vlrxk6gog2zjns8z demo]# git add README.md 
[root@izuf692vlrxk6gog2zjns8z demo]# git commit -m 'first commit'
[root@izuf692vlrxk6gog2zjns8z demo]# git remote add origin git@localhost:demo.git
[root@izuf692vlrxk6gog2zjns8z demo]# git push origin master
初始化空的 Git 版本库于 /home/git/repositories/demo.git/
Counting objects: 3, done.
Writing objects: 100% (3/3), 208 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@localhost:demo.git
 * [new branch]      master -> master

测试同事A的账号在本地是否可以拉取demo项目

#在本地拉取远端的demo项目
➜  Desktop git clone git@(ip地址):demo.git
Cloning into 'demo'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.

➜  Desktop # cd demo 
➜  demo git:(master) # ls
README.md

#测试push功能
➜  demo git:(master) touch test.txt
➜  demo git:(master) ✗ git add .
➜  demo git:(master) ✗ git commit -m 'test'
➜  demo git:(master) git push origin master

#服务器
[root@izuf692vlrxk6gog2zjns8z /]# cd ~/demo/
[root@izuf692vlrxk6gog2zjns8z demo]# git pull origin master
[root@izuf692vlrxk6gog2zjns8z demo]# ls
README.md  test.txt


至此已经搭建完成.

参考:

https://blog.51cto.com/icola/1813763

 类似资料: