First, lets update our packages list by running:
sudo apt update
To install Git, run the following command:
sudo apt install git
Verify that Git was successfully installed on your system:
git version
It’s highly recommended to create a new Linux user for managing the Git repositories, run:
sudo useradd -m -r -U -d /home/git-repos git
The home directory of git user is located at /home/git-repos
. To increase security, we’ll create an ssh key to log in to the git user.
Switch to the log-in session of git user by running:
sudo su - git
To create the SSH directory and file for holding the authorized ssh key for git user, run the following commands:
mkdir -p ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
Once, the server was successfully set-up, it’s time to create a new git repository:
git init --bare ~/test-teo.git
Now, we have to add our local user’s public SSH key to the authorized_keys file of the git user. If we already have generated an SSH key for our local user, we can skip the following step:
ssh-keygen -t rsa
Now, we can copy our public SSH key to the server by running (replace <user>
and <host>
):
ssh-copy-id -i ~/.ssh/id_rsa.pub <user>@<host>
-
Sub-step 1: Get the public key
Ask the end user to provide the public key by typing the following command:
cat ~/.ssh/id_rsa.pub
It will show a long random string starting with ssh-rsa:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ3GIJzTX7J6zsCrywcjAM/7Kq3O9ZIvDw2OFOSXAFVqilSFNkHlefm1iMtPeqsIBp2t9cbGUf55xNDULz/bD/4BCV43yZ5lh0cUYuXALg9NI29ui7PEGReXjSpNwUD6ceN/78YOK41KAcecq+SS0bJ4b4amKZIJG3JWm49NWvoo0hdM71sblF956IXY3cRLcTjPlQ84mChKL1X7+D645c7O4Z1N3KtL7l5nVKSG81ejkeZsGFzJFNqvr5DuHdDL5FAudW23me3BDmrM9ifUmt1a00mWci/1qUlaVFft085yvVq7KZbF2OP2NQACUkwfwh+iSTP username@hostname
Now we can copy the above public SSH key into the
authorized_keys
file of the git user. -
Sub-step 2: Create ssh directory in the git user’s home directory (as a sysadmin)
Keep in mind that we have to create these new directories and files in the git user’s home directory, not your own (root/sysadmin).
mkdir -p /home/git-repos/.ssh && touch /home/git-repos/.ssh/authorized_keys
Now open this
/home/git-repos/.ssh/authorized_keys
file with a text editor like Vim and add the public key of the user here:vim /home/git-repos/.ssh/authorized_keys
Save and close the file. It’s almost ready.
-
Sub-step 3: Set appropriate permission to the file Having appropriate file permission on the ssh file is very important otherwise we’ll see errors like Permission denied (publickey).
First, make sure to set the correct file permissions:
chmod 700 /home/git-repos/.ssh && chmod 600 /home/git-repos/.ssh/authorized_keys
We created those file with either
root
or our own admin accounts for some other user. So we need to change the ownership to the git user:chown -R git:git /home/git-repos/.ssh
On our local Ubuntu 20.04 machine, assuming that you already had an unversioned directory, for example, ~/go. Change the current directory to it:
cd ~/go
git init .
Next, you have to add a git remote to track your local repository on Git server:
git remote add origin git@git-server-ip-address:test-teo.git
Verify that your Git server was successfully installed and configured, run the following command:
cd ~/go
touch README
git add .
git commit -m "Add file Readme"
git push origin master