linux上部署git服务器,在Linux下搭建Git服务器步骤

环境:

服务器 CentOS7.4 + git(version 1.8.3.1)

客户端 Windows10 + git(version 2.16.1.windows.1)

安装 Git

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

服务器端:

yum install -y git

安装完后,查看 Git 版本

[root@localhost ~]# git --version

git version 1.8.3.1

客户端:

下载Git(选择windows版本),地址:https://git-scm.com/downloads

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

右键选择Git Bash 查看 Git 版本,命令同服务端。

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

[root@localhost home]# id git

id: git: no such user

[root@localhost home]# useradd git

[root@localhost home]# passwd git

服务器端创建 Git 仓库

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

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

[root@localhost home]# mkdir -p data/git/webllw.git

[root@localhost home]# git init --bare data/git/webllw.git

Initialized empty Git repository in /home/data/git/webllw.git/

[root@localhost home]# cd data/git/

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

客户端 clone 远程仓库

进入 Git Bash 命令行客户端,创建项目地址(设置在 d:/git/webllw)并进入:

webllw@DESKTOP-RRRQ3VQ MINGW64 /d

$ cd git

webllw@DESKTOP-RRRQ3VQ MINGW64 /d/git

$ mkdir webllw

webllw@DESKTOP-RRRQ3VQ MINGW64 /d/git

$ cd webllw

webllw@DESKTOP-RRRQ3VQ MINGW64 /d/git/webllw

$git clone git@[自己的linux IP 地址]:/home/data/git/webllw.git

客户端创建 SSH 公钥和私钥

webllw@DESKTOP-RRRQ3VQ MINGW64 /d/git/webllw

$ ssh-keygen -t rsa -C "你的邮箱地址"

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

id_rsa 是私钥

id_rsa.pub 是公钥

服务器端 Git 打开 RSA 认证

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

RSAAuthentication yes

PubkeyAuthentication yes

AuthorizedKeysFile .ssh/authorized_keys

保存并重启 sshd 服务:

[root@localhost ssh]# systemctl restart sshd.service

由 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 -ah

. .. .bash_logout .bash_profile .bashrc .ssh

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

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

[root@localhost git]# ll -a

total 24

drwx------ 3 git git 4096 Jan 30 17:38 .

drwxr-xr-x. 4 root root 4096 Jan 30 17:04 ..

-rw-r--r-- 1 git git 18 Sep 7 00:25 .bash_logout

-rw-r--r-- 1 git git 193 Sep 7 00:25 .bash_profile

-rw-r--r-- 1 git git 231 Sep 7 00:25 .bashrc

drwxr-xr-x 2 git git 4096 Jan 30 17:38 .ssh

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

回到 Git Bash 下,导入文件:

$ ssh git@[自己的linux IP 地址] 'cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa.pub

回到服务器端,查看 .ssh 下是否存在 authorized_keys 文件:

[root@localhost git]# cd .ssh

[root@localhost .ssh]# ll

total 4

-rw-rw-r-- 1 git git 402 Jan 30 17:43 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 远程仓库

webllw@DESKTOP-RRRQ3VQ MINGW64 /d/git

$ rm -rf webllw

webllw@DESKTOP-RRRQ3VQ MINGW64 /d/git

$ mkdir webllw

webllw@DESKTOP-RRRQ3VQ MINGW64 /d/git

$ cd webllw

webllw@DESKTOP-RRRQ3VQ MINGW64 /d/git/webllw

$git clone git@[自己的linux IP 地址]:/home/data/git/webllw.git

禁止 git 用户 ssh 登录服务器

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

编辑 /etc/passwd

找到

git:x:1000:1000::/home/git:/bin/bash

修改为

git:x:1000:1000::/home/git:/bin/git-shell

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值