之前搭建过一次git服务器,一直自己在用,没去详细了解git的用户登录的思路,配置了ssh但无密码登录一直不成功;最近有小伙伴要加入git使用,再次实际搭建过程中,终于又往前理解了一步,无密码ssh登录成功。分享下步骤和思路,不正确的地方还请批评指正。
yum install -y curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker vim
wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.30.0.tar.gz
tar -zxvf git-2.30.0.tar.gz
cd git-2.30.0
./configure --prefix=/usr/local/git
make
make install
git --version
如果有老的版本,比如git version 1.8.3.1,需要移除掉
yum remove git
vim /etc/profile
打开的文件中最后添加如下代码
# GIT_HOME
GIT_HOME=/usr/local/git
export PATH=$PATH:$GIT_HOME/bin
刷新环境变量
source /etc/profile
git --version
一切顺利的话会看到: git version 2.30.0
useradd zhangsan
passwd zhangsan
假设另外有普通登录维护账号“lisi”,把"zhangsasn"和"lisi"账户都加入到"gitgroup"组; zhangsan只能登录git服务器使用, lisi可以登录CentOS.
cd /home/zhangsan
mkdir .ssh
cd .ssh
touch authorized_keys
groupadd gitgroup
usermod -G gitgroup zhangsan
usermod -G gitgroup lisi
cd /home
mkdir git-server
chmod -R 774 /home/git-server/
chown -R zhangsan:gitgroup /home/git-server
git config --global user.name "zhangsan"
git config --global user.email "[email protected]"
ssh-keygen -t rsa -C "first_client"
chmod 700 /home/zhangsan/.ssh
chmod 600 /home/zhangsan/.ssh/authorized_keys
cd /home/zhangsan/
chown zhangsan:gitgroup .ssh
chown zhangsan:gitgroup .ssh/authorized_keys
cat id_rsa.pub >> .ssh/authorized_keys
vim /etc/ssh/sshd_config
上面文件中修改一下三行
RSAAuthentication yes
PubkeyAuthentication yes
GSSAPIAuthentication yes
然后重启sshd
systemctl restart sshd.service
cd /home/git-server/
git init --bare test.git
zhangsan:x:1001:1001::/home/zhangsan:/bin/bash
修改为
zhangsan:x:1001:1001::/home/zhangsan:/usr/local/git/bin/git-shell
然后重启sshd
systemctl restart sshd.service
git clone ssh://zhangsan@IP:8888/home/git-server/test.git
克隆的时候,使用自己的用户名登录
git clone ssh://用户名@IP:8888/home/git-server/test.git