我是否每次都与 sudoer 连接并创建 repo 然后 chown ?
是的,你可以在Rami Al-Ghanmi (alghanmi)的文章“Git Local Repository Setup Guide”中看到一个例子:
存储库设置和要点
首先,我们创建 git 用户并设置该帐户以进行 SSH 公钥身份验证和无终端登录。
这意味着,git 帐户无法使用密码登录(仅通过 PKA)并且没有常规的 shell 访问权限。 相反,它将使用带有有限命令集的特殊 git shell。
#Create git user account
sudo adduser --shell $(which git-shell) --gecos 'git version control' --disabled-password git
#Add git user to the appropriate groups
sudo usermod -a -G www-data git
sudo usermod -a -G developers git
#Setup authorized_keys file for access
sudo mkdir -p /home/git/.ssh
sudo touch /home/git/.ssh/authorized_keys
sudo chmod 600 /home/git/.ssh/authorized_keys
sudo chmod 700 /home/git/.ssh
#Copy the git-shell-commands to get limited shell access
sudo cp -r /usr/share/doc/git/contrib/git-shell-commands /home/git/
sudo chmod 750 /home/git/git-shell-commands/*
#Fix permissions
sudo chown -R git:git /home/git/
将您的SSH generated key 添加到授权密钥列表中。您可以对您希望授予访问权限的所有用户重复此步骤
cat ~/.ssh/id_rsa.pub | sudo tee -a /home/git/.ssh/authorized_keys
允许git用户通过SSH访问系统
echo "AllowUsers git" | sudo tee -a /etc/ssh/sshd_config
sudo service ssh restart
创建存储库的位置
sudo mkdir -p /home/repo
sudo chown -R git:www-data /home/repo
创建一个 HelloWorld 存储库
#Create the directory (always end with .git)
sudo mkdir /home/repo/helloworld.git
cd /home/repo/helloworld.git
#Initialize a bare repository
sudo git --bare init
#Some meta-data
echo "Hello World Repository. Testing system configuration" | sudo tee /home/repo/helloworld.git/description
echo "[gitweb]" | sudo tee -a /home/repo/helloworld.git/config
echo -e "\towner = \\"Rami Al-Ghanmi\\"" | sudo tee -a /home/repo/helloworld.git/config
#Fix ownership of repository
sudo chown -R git:www-data /home/repo/helloworld.git
克隆存储库,虽然是空的,然后添加一些代码。
忽略有关克隆空存储库的警告。
git clone git@$(hostname):/home/repo/helloworld.git
cd helloworld
wget https://raw.github.com/gist/3205222/HelloWorld.cpp
git add HelloWorld.cpp
git commit -m "Initial commit with HelloWorld in C++"
git push origin master