Ubuntu 搭建Git服务器

第一步,安装git:
$ sudo apt-get install git
第二步,创建一个git用户,用来运行git服务,并为 git 用户设置密码:
$ sudo adduser git

下面这样新建用户不会在/home/下生成用户文件夹

[root@localhost home]# useradd git
[root@localhost home]# passwd git
第三步,服务器端创建 Git 仓库

设置 /home/git/git.git 为 Git 仓库
然后把 Git 仓库的 owner 修改为 git

[root@localhost home]# mkdir -p git/git.git
[root@localhost home]# git init --bare git/git.git
Initialized empty Git repository in /home/git/git.git/
[root@localhost home]# cd git/
[root@localhost git]# chown -R git:git git.git/
第四步,从 Linux Git 服务器上 clone 项目:
$ git clone git@192.168.1.35:/home/git/git.git

如果SSH用的不是默认的22端口,则需要使用以下的命令(假设SSH端口号是7700):

$ git clone ssh://git@192.168.1.35:7700/home/git/git.git
第五步,禁用shell登录:

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

git:x:1001:1001:,,,:/home/git:/bin/bash

改为:

git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell

这样,git用户可以正常通过ssh使用git,但无法登录shell,因为我们为git用户指定的git-shell每次一登录就自动退出。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。