linux下搭建git服务器

 

安装 Git

Linux 做为服务器端系统,Windows 作为客户端系统,分别安装 Git

服务器端:

#yum install -y git

安装完后,查看 Git 版本

[root@localhost ~]# git --version
git version 1.7.1

客户端:

下载 Git for Windows,地址:https://git-for-windows.github.io/

安装完之后,可以使用 Git Bash 作为命令行客户端。

安装完之后,查看 Git 版本

$ git --version
git version 2.8.4.windows.1

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

[root@localhost home]# id git
id: git:无此用户
[root@localhost home]# useradd git
[root@localhost home]# passwd git

 注意:密码用字母加数字

服务器端创建 Git 仓库

设置 /home/data/git/gittest.git 为 Git 仓库

然后把 Git 仓库的 owner 修改为 git

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

客户端 clone 远程仓库

进入 Git Bash 命令行客户端,创建项目地址并进入:

从 Linux Git 服务器上 clone 项目(输入你的刚设置的密码):

 

 当第一次连接到目标 Git 服务器时会得到一个提示:

 一路 yes

此时 C:\Users\用户名\.ssh 下会多出一个文件 known_hosts,以后在这台电脑上再次连接目标 Git 服务器时不会再提示上面的语句。

客户端创建 SSH 公钥和私钥

$ ssh-keygen -t rsa -C "123456789@qq.com"

此时 C:\Users\用户名\.ssh 下会多出两个文件 id_rsa 和 id_rsa.pub

id_rsa 是私钥

id_rsa.pub 是公钥

服务器端 Git 打开 RSA 认证

进入 /etc/ssh 目录,编辑 sshd_config,打开以下三个配置的注释:

[root@iZ2zegxsbwybwfq0sv1ragZ test]# cd /etc/ssh
[root@iZ2zegxsbwybwfq0sv1ragZ ssh]# ls
moduli      sshd_config       ssh_host_dsa_key.pub  ssh_host_key.pub  ssh_host_rsa_key.pub
ssh_config  ssh_host_dsa_key  ssh_host_key          ssh_host_rsa_key
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

保存并重启 sshd 服务:

[root@localhost ssh]# /etc/rc.d/init.d/ssd restart

由 AuthorizedKeysFile 得知公钥的存放路径是 .ssh/authorized_keys,实际上是 $Home/.ssh/authorized_keys,由于管理 Git 服务的用户是 git,所以实际存放公钥的路径是 /home/git/.ssh/authorized_keys

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

[root@localhost git]# pwd
/home/git
[root@localhost git]# mkdir .ssh
[root@localhost git]# ls -a
. .. .bash_logout .bash_profile .bashrc .gnome2 .mozilla .ssh

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

[root@iZ2zegxsbwybwfq0sv1ragZgit]# chown -R git:git .ssh
[root@iZ2zegxsbwybwfq0sv1ragZ git]# ll -a
total 24
drwx------  3 git  git  4096 Aug  8 11:41 .
drwxr-xr-x. 4 root root 4096 Aug  8 11:00 ..
-rw-r--r--  1 git  git    18 Mar 23 08:11 .bash_logout
-rw-r--r--  1 git  git   176 Mar 23 08:11 .bash_profile
-rw-r--r--  1 git  git   124 Mar 23 08:11 .bashrc
drwxr-xr-x  2 git  git  4096 Aug  8 11:41 .ssh

将客户端公钥导入服务器端 /home/git/.ssh/authorized_keys 文件

回到windows的 Git Bash 下,导入文件:

$ ssh git@你的远程服务器ip地址 'cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa.pub

需要输入服务器端 git 用户的密码

 

 回到服务器端,查看 .ssh 下是否存在 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 远程仓库

$ git clone git@47.93.222.78:/var/www/test/sample.git

禁止 git 用户 ssh 登录服务器

之前在服务器端创建的 git 用户不允许 ssh 登录服务器

编辑 /etc/passwd

找到:

git:x:502:504::/home/git:/bin/bash

修改为

git:x:502:504::/home/git:/bin/git-shell

此时 git 用户可以正常通过 ssh 使用 git,但无法通过 ssh 登录系统。

posted @ 2017-08-08 12:08  丶老中医  阅读(3092)  评论(0编辑  收藏  举报
一切已经开始©2018 丶老中医