Setup Git on a Server

Table of Contents

Setup Git on a Server

First, ssh or otherwise login to your remote server:

Next, install git on the server.

sudo apt update && sudo apt install git

Next, create a new user that will manage the Git Repos:

sudo useradd -r -m -U -d /home/git -s /bin/bash git

Switch over to the git user with sudo su

sudo su - git

Make the folder you want to in the home directory:

mkdir project_name.git

Next, make the ~/.ssh directory for the user and set the correct permissions:

mkdir -p ~/.ssh && chmod 0700 ~/.ssh

Next, create the authorized keys file and set it to have permissions of 0600.

touch ~/.ssh/authorized_keys && chmod 0600 ~/.ssh/authorized_keys

Next, move your ssh keys to the server's authorized keys:

On your local machine:

cat ~/.ssh/id_rsa.pub

And copy paste it into the authorized keys file:

sudo vim /home/git/.ssh/authorized_keys

now add your git remote to the local repository you took the rsa key from:

git remote add server git@{server_ip}:project_name.git

and push!

git push -u server master