All HowTo's Linux Redhat, Fedora and CentOS Linux Ubuntu, Mint & Debian Linux

Create Your Own Private Git Repo On Your Servers

Your Git repo doesn’t need to be on Bitbucket or Github. You can very easily host a repository on your own systems. This article demonstrates this. We’re using CentOS here but any Linux OS should work. You just need the “git” package.

As always, we’d love to read your comments. What do you think?

yum install git

Which is probably already installed. If not, complete the install. Then create a “git” user.

useradd git

For added security, you can change the “shell” in the “/etc/passwd” file to a limited “git specific” shell.

usermod -s /usr/bin/git-shell git

Now add your key. Of course, if you allow password-basesd logins via SSH, your key doesn’t need to be uploaded. But you should probably reconsider and switch to keys-only. You can do it with “ssh-copy-id” or manually like this:

On the git server.

# Create the directory.
mkdir /home/git/.ssh

# Add your public key to this file:
vi /home/git/.ssh/authorized_keys

# Set permissions:
chmod 700 /home/git/.ssh
chmod 600 /home/git/.ssh/authorized_keys

# Set ownership.
chown git.git -R /home/git/.ssh

Now we need to create the repository. I use a structure like “COMPANY / PROJECT” but use whatever structure you like.

mkdir /home/git/company1/project1
cd /home/git/company1/project1
git init --bare
chown git.git -R /home/git

Ok, we’re ready. On your client host (with the corresponding key), you can do a “git clone” to get the new/empty repository.

On the git client (your workstation):

git clone [email protected]:/home/git/company1/project1

Go into the new directory, create a new file, push it and confirm it works.

cd ~/project1
echo "This is a test file" > testfile
git add testfile
git commit -m "first push"
git push

If you got something looking like the following, you’re in good shape. You’re done. However, if you want to add other people to the repo (give others access), just append their SSH key to the server file “/home/git/.ssh/authorized_keys” file.

Leave a Reply

Your email address will not be published. Required fields are marked *