搭建git服务器

搭建git服务器


环境:win7 x64作为客户端192.168.2.102,centos7作为服务端192.168.2.32
备注:centos7默认安装了git-1.8.3.1版本,所以无需再安装
win7 git客户端下载地址:https://git-for-windows.github.io/
win7 git客户端安装可以参考以下文档:【Git的安装步骤博文http://www.cnblogs.com/wj-1314/p/7993819.html】

服务端:

#查看git版本
[admin@TEST1 ~]$git --version
git version 1.8.3.1
#切换为root用户,不切换root用户后边无法进入新建入的git用户的家文件
[admin@TEST1 home]$ sudo su - root
[sudo] password for admin: 
 #查看git用户是否存在
[root@TEST1 home]# id git
id: git: no such user #提示git用户不存在
#创建git用户并设置密码
[root@TEST1 home]#useradd git
[root@TEST1 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仓库
[root@TEST1 home]$ sudo mkdir -p /home/git/repository/gittest.git
[root@TEST1 home]# ls
admin git
[root@TEST1 home]# cd git
[root@TEST1 git]# ls
repository
[root@TEST1 git]# cd /home/git/gitrepository/
[root@TEST1 repository]# ls
gittest.git
#初始化项目git仓库目录
[root@TEST1 repository]# git init --bare /home/git/repository/gittest.git 
Initialized empty Git repository in /home/git/repository/gittest.git/

#更改git仓库所有归属用户
[root@TEST1 repository]# cd ..
[root@TEST1 git]# ll
total 0
drwxr-xr-x 3 root root 25 May 22 04:19 repository
[root@TEST1 git]# chown -R git:git /home/git/repository
[root@TEST1 git]# ll
total 0
drwxr-xr-x 3 git git 25 May 22 04:19 repository

客户端:

#创建项目目录
cd /f
mkdir gittest
#在客户端clone远程仓库
cd /f/gittest
git clone [email protected]:/home/git/repository/gittest.git
首次clone仓库会出现以下提示,需要输入一次yes和一次git用户密码
Cloning into 'gittest'...
The authenticity of host '192.168.2.32 (192.168.2.32)' can't be established.
ECDSA key fingerprint is SHA256:D9Eoa+0UNCcPIPF4LKhr0nDE52fwbeuaDld8/4Rkie8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.2.32' (ECDSA) to the list of known hosts.
[email protected]'s password:
warning: You appear to have cloned an empty repository.

#设置公钥和私钥
ssh-keygen -t rsa -C "[email protected]"
#一路回车,公钥和私钥默认保存在C:/user/用户名/.ssh文件下,分别默认命名为id_rsa 和 id_rsa.pub

服务端:

#创建公钥存放目录
cd /home/git
mkdir .ssh
#更改.shh目录所有归属用户
chown -R git:git .ssh

客户端:

#将客户端公钥文件导入服务端.shh目录中
cd /f/gittest
ssh [email protected] 'cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa.pub
#需要输入一次git用户密码

服务端:

#修改公钥目录权限和公钥文件权限
chmod 700 /home/git/.ssh
chmod 600 /home/git/.ssh/authorized_keys
#再次用客户端clone远程仓库就可以不用密码直接连接了

客户端:

#配置本地git的名称和邮箱
cd /f/gittest
git config --global user.name "zaw"
git config --global user.email "[email protected]"
#禁止git用户ssh直接登录服务端系统
#编辑/etc/passwd文件
sudo vi /etc/passwd
找到:
git:x:1001:1001::/home/git:/bin/bash
修改为:
git:x:1001:1001::/home/git:/bin/git-shell

#删除远程仓库连接(客户端操作)
rm -rf .git

你可能感兴趣的:(搭建git服务器)