linux下搭建git服务器全过程

xiaoxiao2021-02-28  85

环境: (centos版本可用lsb_release -a查看)

服务器: 1. CentOS Linux release 7.3.1611 (Core) 2. git version 1.8.3.1

客户端 3. window7 4. git 1.9.4

第一步:安装git

yum install -y git

第二步:创建git 用户

id git useradd git passwd git

第三步:服务器端创建 Git 仓库

[root@localhost home]# mkdir -p /home/git/gittest.git [root@localhost home]# git init --bare /home/git/gittest.git [root@localhost home]# cd /home/git [root@localhost git]# chown -R git:git gittest.git/

第四步:客户端 clone 远程仓库

mkdir gittest.git cd gittest.git git clone git@192.168.33.10:/home/vagrant/gittest.git 这个时候会报一个错误:无法连接

第五步: 客户端创建 SSH 公钥和私钥

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

第六步:服务器端 Git 打开 RSA 认证 进入/etc/ssh目录,编辑sshd_config,打开以下三个配置的注释

RSAAuthentication yes PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys #保存并重启sshd服务 /bin/systemctl restart sshd.service

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

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

mkdir .ssh chown -R git:git .ssh

第七步:将客户端公钥导入服务器端 /home/git/.ssh/authorized_keys 文件 将客户端~/.ssh/id_rsa.pub中的公钥复制到/home/git/.ssh/authorized_keys文件中

重要:

修改 .ssh 目录的权限为 700

修改 .ssh/authorized_keys 文件的权限为 600

chmod 700 .ssh chmod 600 .ssh/authorized_keys

第八步:客户端再次 clone 远程仓库

git clone git@192.168.33.10:/home/git/gittest.git git pull origin master git branch --set-upstream-to=origin/master ;这里可能会push不了,只需要建个文件,commit一把就可以了

第九步:禁止 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 登录系统。

git 设置快捷alias

git config --global alias.pl pull git config --global alias.ps push git config --global alias.ck checkout git config --global alias.co commit git config --global alias.st status

git 库迁移(由于旧库删除了,可以直接修改.git/config中的[remote “origin”] 下的url, 来修改指定的远程库) config文件如下所示:

[core] repositoryformatversion = 0 filemode = false bare = false logallrefupdates = true symlinks = false ignorecase = true [remote "origin"] **url = git@xx.xx.xx.xx:/home/git/loan_api** fetch = +refs/heads/*:refs/remotes/origin/* [branch "master"] remote = origin merge = refs/heads/master

参考:http://www.cnblogs.com/dee0912/p/5815267.html

转载请注明原文地址: https://www.6miu.com/read-26746.html

最新回复(0)