ubuntu git服务器的搭建

学习目标:

git学习五、


学习内容

一、ubuntu git服务器的安装


1、在家目录下创建一个用于用来运行git服务的新用户 git

在这里插入图片描述


2、收集所有需要登录的用户的公钥,就是他们自己的id_rsa.pub文件,把所有公钥导入到/home/git/.ssh/authorized_keys文件里,一行一个

  • 访问服务器用户主机的公钥
[root@192 ~]# cd .ssh
[root@192 .ssh]# cat id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDN9HYBmk45W4Cu3aEag9rnazft5tyeb8pe1nv2LcXijHGOdoB+rHOY9JMKtLk
Ynt0lUxBzJApqd6RI5pXh0q/+oTF7JTjHtUtdAlBEzOqhDTCCExPywiJNsEDWvLVolU3tykU9ENCNg/iEH/5RRSPOgqehRcihYb
z+fqxTb4ARvGHI9nrk+ahVPsrvZL+8z1OjU0zWdTM0iHZK2RpJRTZpcU4H12Q0zuqK39eTAYQ3ABwQz7WzlJUNBqu4TX0Un7sFF
2zo/42kLxweQnLI3D6yqk3M8JM4z7eYD3MSYF2kPNGf3VeBT6/FdzjdxRlaTW9bXp4Hfu6sgQnhENVyzpEu/g7y8jmg7Anjg2Bb
aqxq05zyiy7x+Vu2pRJ0T875WpIeb5uctd0adUU1aFejnK5QSjH4/uxDkCQC6sJeIj2PSdsR8pndcLF+IqLs+3Tug4GIkbZEZHp
wD2jzWip4ZjQBhoDwkoWYk9OcCSBlNuVcBPvCe856gySxLArIIZfb58=812277079@qq.com
  • 在服务器的home目录下进入git目录创建.ssh目录,并进入.ssh目录创建authorized—keys文件用于存放用户的公钥
root@ecs-e4c1:/home/git# mkdir .ssh
root@ecs-e4c1:/home/git# cd .ssh
root@ecs-e4c1:/home/git/.ssh# touch authorized_keys
root@ecs-e4c1:/home/git/.ssh# vi authorized_keys 

在这里插入图片描述


3、 更改git下所有文件及文件夹的用户组和用户属于git并且更改.ssh目录的权限必须是700,.ssh/authorized_keys文件权限必须是600

root@ecs-e4c1:/home/git# ls -la 
total 24
drwxr-xr-x 3 git  git  4096 Oct 29 14:56 .
drwxr-xr-x 3 root root 4096 Oct 29 14:51 ..
-rw-r--r-- 1 git  git   220 Oct 29 14:51 .bash_logout
-rw-r--r-- 1 git  git  3771 Oct 29 14:51 .bashrc
-rw-r--r-- 1 git  git   807 Oct 29 14:51 .profile
drwxr-xr-x 2 root root 4096 Oct 29 14:56 .ssh
root@ecs-e4c1:/home/git# cd .ssh
root@ecs-e4c1:/home/git/.ssh# ls -l
total 4
-rw-r--r-- 1 root root 570 Oct 29 14:59 authorized_keys
root@ecs-e4c1:/home/git/.ssh# cd ..
root@ecs-e4c1:/home/git# chown -R git:git ..
root@ecs-e4c1:/home/git# chown -R git:git .ssh
root@ecs-e4c1:/home/git# chomd 700 .ssh
root@ecs-e4c1:/home/git# chmod 600 .ssh/authorized_keys 
root@ecs-e4c1:/home/git# ls -la
total 24
drwxr-xr-x 3 git git 4096 Oct 29 14:56 .
drwxr-xr-x 3 git git 4096 Oct 29 14:51 ..
-rw-r--r-- 1 git git  220 Oct 29 14:51 .bash_logout
-rw-r--r-- 1 git git 3771 Oct 29 14:51 .bashrc
-rw-r--r-- 1 git git  807 Oct 29 14:51 .profile
drwx------ 2 git git 4096 Oct 29 14:59 .ssh

4、在Git创建一个裸仓库,裸仓库没有工作区,因为服务器上的Git仓库纯粹是为了共享,所以不让用户直接登录到服务器上去改工作区,并且服务器上的Git仓库通常都以.git结尾

  • 先选定一个目录作为Git仓库,假定是/srv/sample.git,在/srv目录下输入命令:

sudo git init --bare sample.git

  • 然后把owner改为git:

chown -R git:git sample.git

root@ecs-e4c1:/srv# git init --bare sample.git
Initialized empty Git repository in /srv/sample.git/
root@ecs-e4c1:/srv# chown git:git sample.git/
root@ecs-e4c1:/srv# ls -la
total 12
drwxr-xr-x  3 root root 4096 Oct 29 15:23 .
drwxr-xr-x 21 root root 4096 Oct 29 14:46 ..
drwxr-xr-x  7 git  git  4096 Oct 29 15:23 sample.git

5、禁用shell登录:出于安全考虑,第二步创建的git用户不允许登录shell,这可以通过编辑/etc/passwd文件下git那一行完成

/bin/bash改成/usr/bin/git-shell
在这里插入图片描述

root@ecs-e4c1:/# cd etc
root@ecs-e4c1:/etc# cat passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
...
git:x:1000:1000:,,,:/home/git:/bin/bash
root@ecs-e4c1:/etc# vi passwd
root@ecs-e4c1:/etc# cat passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
...
git:x:1000:1000:,,,:/home/git:/usr/bin/git-shell
  • git用户可以正常通过ssh使用git,但无法登录shell,因为我们为git用户指定的git-shell每次一登录就自动退出

6、可以通过git clone命令克隆远程仓库了,在各自的电脑上运行

git clone git@服务器的公网地址:/srv/sample.git

  • 家目录下的库文件夹
root@ecs-e4c1:/# cd srv/
root@ecs-e4c1:/srv# ls -l
total 4
drwxr-xr-x 7 git git 4096 Oct 29 15:23 sample.git
root@ecs-e4c1:/srv# cd sample.git/
root@ecs-e4c1:/srv/sample.git# ls -l
total 32
drwxr-xr-x 2 root root 4096 Oct 29 15:23 branches
-rw-r--r-- 1 root root   66 Oct 29 15:23 config
-rw-r--r-- 1 root root   73 Oct 29 15:23 description
-rw-r--r-- 1 root root   23 Oct 29 15:23 HEAD
drwxr-xr-x 2 root root 4096 Oct 29 15:23 hooks
drwxr-xr-x 2 root root 4096 Oct 29 15:23 info
drwxr-xr-x 4 root root 4096 Oct 29 15:23 objects
drwxr-xr-x 4 root root 4096 Oct 29 15:23 refs
  • 用户从服务器上克隆下此库文件夹,新建一个文件夹gittest把克隆的库文件夹放在这个文件夹下
[root@192 srv]# git clone git@124.71.152.52:/srv/sample.git
正克隆到 'sample'...
The authenticity of host '124.71.152.52 (124.71.152.52)' can't be established.
ECDSA key fingerprint is SHA256:xKkvIqy+B/W0GuygAfYQRv0i4DBx7Yo5iFKGutGIbBs.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '124.71.152.52' (ECDSA) to the list of known hosts.
warning: 您似乎克隆了一个空仓库。
[root@192 srv]# ls -l
总用量 0
drwxr-xr-x. 3 root root 18 1029 04:04 sample
[root@192 srv]# cd sample/
[root@192 sample]# ls -l
总用量 0
[root@192 sample]# cd .git
[root@192 .git]# ls -la
总用量 16
drwxr-xr-x. 7 root root  119 1029 04:04 .
drwxr-xr-x. 3 root root   18 1029 04:04 ..
drwxr-xr-x. 2 root root    6 1029 04:04 branches
-rw-r--r--. 1 root root  258 1029 04:04 config
-rw-r--r--. 1 root root   73 1029 04:04 description
-rw-r--r--. 1 root root   23 1029 04:04 HEAD
drwxr-xr-x. 2 root root 4096 1029 04:04 hooks
drwxr-xr-x. 2 root root   21 1029 04:04 info
drwxr-xr-x. 4 root root   30 1029 04:04 objects
drwxr-xr-x. 4 root root   31 1029 04:04 refs
  • 此时便完成了git服务器的搭建,以后任意一台需要访问服务器时,只需要将用户机的公钥编辑到/home/git/.ssh/authorized_keys,就可以访问了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值