【问题标题】:git-shell - New repositoriesgit-shell - 新的存储库
【发布时间】:2014-02-12 07:22:41
【问题描述】:

我有一个带有专用 git 用户和存储库的服务器
我正在尝试使用git-shell method 来让我的开发人员从事多个项目。

我即将将我的git 用户的外壳更改为git-shell
如果我这样做,我将无法再与标准 shell 连接(这就是想法)。

问题:那么我将如何创建新的存储库?

我是否每次都与 sudoer 连接并创建 repo 然后 chown ?

【问题讨论】:

    标签: git repository git-shell


    【解决方案1】:

    您可以使用git-shell-commands 目录通过自定义命令扩展git-shell,然后可以使用 ssh 远程执行这些命令。

    例如使用此内容创建~git/git-shell-commands/newgit

    #!/bin/sh
    mkdir -p ~/$1
    cd ~/$1
    git init --bare
    

    然后chmod +x ~git/git-shell-commands/newgit 允许执行。

    要使用它,运行:

    ssh git@server newgit newrepo.git
    

    这足以创建一个新的裸 git 存储库,可以使用以下方法克隆它:

    git clone git@server:newrepo.git
    

    【讨论】:

    • 在更公共的服务器上使用此命令时要小心 - 如果有人连接并输入 ../../../../etc/password 或类似的东西,它可能会搞砸您的系统。在对它们进行操作之前始终验证字符串/规范化路径!
    【解决方案2】:

    我是否每次都与 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
    

    【讨论】:

      【解决方案3】:

      看看http://planzero.org/blog/2012/10/24/hosting_an_admin-friendly_git_server_with_git-shell

      您可以在您的 git 用户的主目录中创建一个 git-shell-commands 目录,然后从页面中复制示例工具以允许通过 guest-shell 创建 repo。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-28
        • 1970-01-01
        • 1970-01-01
        • 2022-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多