在CentOS上怎么搭建Git服务器

发布时间:2022-04-20 16:43:04 作者:iii
来源:亿速云 阅读:202

本文小编为大家详细介绍“在CentOS上怎么搭建Git服务器”,内容详细,步骤清晰,细节处理妥当,希望这篇“在CentOS上怎么搭建Git服务器”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

一 确认服务器是否安装git

[root@iz25r8k6ifuz git]# rpm -qa git
git-1.7.1-3.el6_4.1.x86_64

这里也已经安装过了,如果没有安装可以用yum install git 安装。

二 创建git用户

这里你可以选择新建一个用户来测试,也可以直接使用你的root进行以下操作。笔者也是看着资料一步一步来的,这里创建一个新用户teslachen进行操作。

[root@iz25r8k6ifuz ~]# useradd tesla
[root@iz25r8k6ifuz ~]# passwd tesla

更改用户 tesla 的密码 。

新的 密码:

无效的密码: 它没有包含足够的不同字符

无效的密码: 过于简单

重新输入新的 密码:

passwd: 所有的身份验证令牌已经成功更新。

注1:创建用户权限不够请加上sudo;

注2:设置用户密码太过简单的话会有提示,但依旧可以设置成功。

三 生成ssh公钥

许多 git 服务器都使用 ssh 公钥进行认证。 为了向 git 服务器提供 ssh 公钥,如果某系统用户尚未拥有密钥,必须事先为其生成一份。

linux 可以在本机运行ssh-keygen -t rsa生成密钥,把.pub文件拷到服务器上。

[root@iz25r8k6ifuz ~]# su tesla
[tesla@iz25r8k6ifuz root]$ cd ~
[tesla@iz25r8k6ifuz ~]$ mkdir .ssh
[tesla@iz25r8k6ifuz ~]$ ssh-keygen -t rsa
generating public/private rsa key pair.
enter file in which to save the key (/home/tesla/.ssh/id_rsa):
enter passphrase (empty for no passphrase):
enter same passphrase again:
your identification has been saved in /home/tesla/.ssh/id_rsa.
your public key has been saved in /home/tesla/.ssh/id_rsa.pub.
the key fingerprint is:
13:bf:75:ba:67:7f:0e:a0:47:7a:fe:25:bc:81:85:c3 tesla@iz25r8k6ifuz
the key's randomart image is:
+--[ rsa 2048]----+
|     |
|     |
|  .  |
|   o . . |
|  s . e o |
|   . o |
|   + = = .|
|   + .o.|
|   o+oo+|
+-----------------+
[tesla@iz25r8k6ifuz ~]$ cd .ssh/
[tesla@iz25r8k6ifuz .ssh]$ cat id_rsa.pub >> ~/.ssh/authorized_keys
exit

四 添加tesla到sudoers文件

tesla用户现在对一些文件夹没有操作权限,修改/etc/sudoers文件来改变他的权限。最高管理员用户用下面命令打开。

[root@iz25r8k6ifuz ~]# visudo

然后我们在vim中找到下面这行

root all=(all) all

按i键开始插入,回车一下在下面一行加上

tesla all=(all) all

接着按下esc键,输入 :wq ,回车保存退出

五 创建git代码仓库

[root@iz25r8k6ifuz ~]# mkdir /teslarepo
[root@iz25r8k6ifuz ~]# cd /teslarepo/
[root@iz25r8k6ifuz teslarepo]# sudo mkdir teslaproject.git
[root@iz25r8k6ifuz teslarepo]# chown tesla:tesla /teslarepo/
[root@iz25r8k6ifuz teslarepo]# chown -r tesla:git /teslarepo/
[root@iz25r8k6ifuz teslarepo]# cd teslaproject.git/
[root@iz25r8k6ifuz teslaproject.git]# sudo git --bare init
initialized empty git repository in /teslarepo/teslaproject.git/

这样一个叫teslaproject得git仓库就创建好了

六 本地测试使用

你可以直接在服务器上进行本地测试,也可以直接用你的电脑来测试。下面我是使用自己的mbp来进行的测试。

localhost:~ okay$ cd desktop/git/
localhost:git okay$ mkdir teslarepo
localhost:git okay$ cd teslarepo/
localhost:teslarepo okay$ git init
initialized empty git repository in /users/okay/desktop/git/teslarepo/.git/
localhost:teslarepo okay$ git remote add origin tesla@123.57.159.74:/teslarepo/teslaproject.git

上面的命令在本地创建了一个文件夹并添加了服务器上的远程仓库

localhost:teslarepo okay$ touch a.txt
localhost:teslarepo okay$ git add a.txt
localhost:teslarepo okay$ git commit -m "init commit"
[master (root-commit) d14cd3b] init commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a.txt

上面的命令在本地创建了一个a.txt并在本地提交了一次

localhost:teslarepo okay$ git push origin master
tesla@123.57.159.74's password:
counting objects: 3, done.
writing objects: 100% (3/3), 202 bytes | 0 bytes/s, done.
total 3 (delta 0), reused 0 (delta 0)
to tesla@123.57.159.74:/teslarepo/teslaproject.git
 * [new branch]  master -> master

上面的命令将本地代码push到远程服务器上去了,下面我们在本地clone一次看下是否正确

七 本地clone

localhost:git okay$ mkdir ttt
localhost:git okay$ cd ttt
localhost:ttt okay$ git clone tesla@123.57.159.74:/teslarepo/teslaproject.git
cloning into 'teslaproject'...
tesla@123.57.159.74's password:
remote: counting objects: 3, done.
remote: total 3 (delta 0), reused 0 (delta 0)
receiving objects: 100% (3/3), done.
checking connectivity... done.

clone完成,让我们看一下文件夹目录

在CentOS上怎么搭建Git服务器

之前push到服务器上的a.txt文件已经被clone下来

------------分割线-------------  

1. 查看系统用户组

-d:指定字段的分隔符,默认的字段分隔符为“tab”;
-f:显示指定字段的内容;

cut -d: -f1 /etc/group

2. 查看系统用户

cut -d: -f1 /etc/passwd

3. clone仓库

git clone git@your_gitserver_ip:/home/gitrepo/sample.git

4. push已有仓库

// 以master分支示范
git checkout master
git remote rm origin
git remote add origin git@your_gitserver_ip:/home/gitrepo/sample.git
git push -u origin master

读到这里,这篇“在CentOS上怎么搭建Git服务器”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 在CentOS 7上搭建lamp环境
  2. 在CentOS上搭建基于Nodejs的Ghost博客

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

centos git

上一篇:因文件权限导致git fetch命令执行失败怎么解决

下一篇:Docke中如何搭建gitlab

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》