Linux 下搭建 Git 服务器

本贴最后更新于 2016 天前,其中的信息可能已经时移世异

Linux 下搭建 Git 服务器

一. 系统环境

[root@localhost home]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)

二. 安装 Git

服务器端:

[root@localhost home]# yum install -y git

安装完成后,查看 Git 版本号:

[root@localhost home]# git --version
git version 1.8.3.1

本地:略。

三. 创建 git 用户,用来管理 Git 服务,并为 git 用户设置密码

[root@localhost home]# id git
id: git: no such user
[root@localhost home]# useradd git
[root@localhost home]# passwd git
Changing password for user git.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.

四. 创建 Git 仓库

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

[root@localhost home]# mkdir -p data/git/test.git
[root@localhost home]# git init --bare data/git/test.git
Initialized empty Git repository in /home/data/git/test.git/
[root@localhost home]# cd data/git/
[root@localhost git]# chown -R git:git test.git/

五. 服务器端 Git 打开 RSA 认证

进入 /etc/ssh 目录,编辑 sshd_config,打开以下三个配置的注释,然后重启 sshd 服务:

[root@localhost ssh]# cd /etc/ssh/
[root@localhost ssh]# vim sshd_config
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile      .ssh/authorized_keys
[root@localhost ssh]# systemctl restart sshd

在 /home/git/ 下创建目录 .ssh:

[root@localhost ssh]# cd /home/git/
[root@localhost git]# mkdir .ssh

然后把 .ssh 文件夹的 owner 修改为 git:

[root@localhost git]# chown -R git:git .ssh

六. 添加客户端 ssh key 到服务器

Neuer@Neuer MINGW64 ~/.ssh
$ ssh git@192.168.118.140 'cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa.pub

查看服务器端是否存在 authorized_keys 文件:

[root@localhost git]# cd .ssh/
[root@localhost .ssh]# ll
total 4
-rw-rw-r--. 1 git git 398 Sep 21 17:22 authorized_keys

修改 .ssh 目录的权限为 700,修改 .ssh/authorized_keys 文件的权限为 600:

[root@localhost git]# chmod 700 .ssh
[root@localhost git]# cd .ssh/
[root@localhost .ssh]# chmod 600 authorized_keys

七. 客户端 clone 远程仓库

Neuer@Neuer MINGW64 ~/.ssh
$ git clone git@192.168.118.140:/home/data/git/test.git
Cloning into 'test'...
warning: You appear to have cloned an empty repository.

八. 禁止 git 用户 ssh 登录服务器

编辑 /etc/passwd,将 bash 修改为 git-shell:

[root@localhost .ssh]# vim /etc/passwd
/home/git:/bin/bash ->
/home/git:/bin/git-shell

不听乱世的耳语,只过自己想要的生活

  • Linux

    Linux 是一套免费使用和自由传播的类 Unix 操作系统,是一个基于 POSIX 和 Unix 的多用户、多任务、支持多线程和多 CPU 的操作系统。它能运行主要的 Unix 工具软件、应用程序和网络协议,并支持 32 位和 64 位硬件。Linux 继承了 Unix 以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统。

    914 引用 • 930 回帖 • 1 关注
  • Git

    Git 是 Linux Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。

    204 引用 • 357 回帖 • 1 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...