当前位置: 首页 > Linux

七步搭建Git服务器

时间:2023-04-06 02:24:50 Linux

Github是最大的gaysocial,哦不,是代码托管的远程仓库。Github很棒,但是免费托管服务的代码是公开的,任何人都可以访问您的代码。如果需要私有远程仓库又不想付费,可以自己搭建Git服务器,当然前提是你需要有一台运行中的服务器,最好是Linux服务器。第一步:安装GitUbunt/Debian$sudoapt-getinstallgitCentOS$sudoyuminstallgit第二步:新建一个提供Git服务的用户为了安全起见,新建一个名为git的用户,将只允许执行Git服务相关命令。$sudoaddusergit$sudopasswdgit接下来要对git用户的家目录进行操作,需要切换到git用户或者root用户才能继续后续操作。$sugit第三步:上传用户公钥连接Git服务器时,需要无密码ssh连接,上传用户公钥id_rsa.pub(位于~/.ssh/id_rsa.pub)。如果没有,先生成一个新的公钥。将公钥复制到服务器,这里使用scp命令。$scp~/.ssh/id_rsa.pubuser@serverIP:/home/git/.ssh将公钥导入/home/git/.ssh/authroied_keys文件,有多个用户,一行插入一个公钥,$cat/home/git/.ssh/id_rsa.pub>>/home/git/.ssh/authorized_keys第四步:修改权限设置。git用户是authroied_keys文件的拥有者和读写权限。$chmod600authorized_keys$ll/home/git/.ssh/authorized_keys-rw------。1gitgit418Sep2106:08/home/git/.ssh/authorized_keys第五步:初始化git仓库选择一个目录作为Git仓库,并将目录所有者更改为git用户。$sudogitinit--baresample.git$sudochown-Rgit:gitsample.git第六步:使用git-shell登录对于服务器的git用户,应该只允许git相关的操作,所以换shell登录到git-shell登录。编辑/etc/passwd文件:git:x:1001:1001:,,,:/home/git:/bin/bash为:git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell还复制了一个名为git-shell-commands的目录:$cp/usr/share/doc/git-1.7.4.4/contrib/git-shell-commands/home/git-R$chowngit:developers/home/git/git-shell-commands/-R$chmod+x/home/git/git-shell-commands/help$chmod+x/home/git/git-shell-commands/list第七步:使用远程仓库$gitclonegit@server:/path/to/sample.gitException1.sshfailedPermissiondenied(publickey,gssapi-keyex,gssapi-with-mic)?首先尝试使用ssh-vuser@xxx.xxx.xxx.xxx在调试模式下连接。我有这个问题是因为authorized_keys文件的所有者是root,而git用户无法读取这个文件。所以遇到这个问题,建议检查一下git用户是否对authorized_keys有读写权限。另一种可能是客户端没有开启ssh密码认证。2.fatal:Interactivegitshellisnotenabled.hint:~/git-shell-commandsshouldexistandhavereadandexecuteaccess?根据一些教程没有复制git-shell-commands目录,导致gitshell报错,直接按Step6复制这个目录即可。参考:https://serverfault.com/questions/285324/git-shell-not-enabled参考https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137583770360579bc4b458f044ce7afed3df579123eca000https://jingyan.baidu.com/article/0f5fb099e2b6236d8334eaf9.htmlhttps://serverfault.com/questions/285324/git-shell未启用