(techy music)
- There are times where you may want to host
your remote Git repository within your network.
The reasons for this are varied.
Perhaps you'll be storing sensitive data
which you don't want to trust to a cloud provider,
or maybe you want the speed advantage
of having your Git server on your local network.
I recommend setting up your own private Git repository.
This way you don't have to worry about
what is being stored in your repository
as your repository is private,
nor do you have to worry about storage
as local storage is large and cheap.
To do this properly, we need two hosts.
I will set this up on my rhhost2 VM
and have the rhhost1 client contacted through the network.
However, this will also work just fine on one host.
It's just not we'd build it in a real environment.
To run it on one host,
we'll just have the client host
contact itself over the network.
To create a remote Git repository easily
we'll be using SSH keys,
which means you'll probably want a Git repository
on an operating system that has an SSH server
such as Linux, Unix, or macOS.
Although it can also work on Windows using Win32_OpenSSH.
Go to the host with a remote repository will reside,
for me, this is rhhost2.
If you plan on doing this exercise on one host,
just follow along as these instructions will work
for that scenario as well.
Let's start by creating a Git user.
Type in sudo useradd git
and hit enter.
Type in your password if prompted.
Now let's give the Git user a password.
Type in sudo passwd git
and hit enter.
Now give the Git user a password.
Now make a directory for the Git repository.
We'll call it GitProjectTwo.git
and place it /srv/git.
Where you put this is not that important,
as long as the git user has access to it.
Type in sudo mkdir -p /srv/git/GitprojectTwo.git
Make sure you use the -p option
so it creates the parent directories.
Notice that I've used the .git naming convention
for this directory.
This is to tell me that this is a remote repository
being served at two other hosts.
We will not see the into this directory and do commits.
Now, change into the GitProject2.git directory by typing in
cd /srv/git/GitProjectTwo.git
and hit enter.
Now we'll initialize a bare Git repository.
Type in sudo git init --bare and hit enter.
That's the basic Git server setup.
Now let's set permissions,
cd up two levels by typing in cd /srv and hit enter.
And we'll change the ownership of the Git directory
to the Git user and the Git group.
Type in sudo chown -R git.git git
and hit enter.
All files should now be owned
by the Git user and the Git group.
Let's verify.
Type in ls -lR git
and hit enter.
And we can see that they are.
Before we move to the client,
we need to know the IP address of this host.
Type in clear, and then type in ifconfig, and hit enter.
My IP address is 192.168.1.125.
Even if you're using one host for this exercise,
you'll still need to know the IP address,
as you'll still be using it.
Now let's move over to the client host.
In my case, this is rhhost1.
Again, if you're doing this on one host, that's fine,
we'll just have our client contact our server,
which happens to be the same host with same IP address.
Let's create a new local project called GitProjectTwo.
Type in mkdir -p ~/RemoteRepos/GitProjectTwo
and hit enter.
Notice that I didn't include the .git portion of the name.
This tells me this is a Git project directory
that I'll cd into and make commits
The naming convention is actually up to you.
Now change into it,
type in cd ~/RemoteRepos/GitProjectTwo/
and hit enter.
And we'll initialize the repository
by typing git init
Now let's copy a few files to it.
Type in cp /etc/passwd /etc/hosts /etc/resolv.cof .
Make sure you add the . at the end.
This has to copy these files to the current directory
which is the GitProjectTwo directory.
If you're using an OS without these files
choose some other files to copy.
Now, hit enter.
Now stage the files for a commit.
Type in git add .
the dot matches all files in the directory.
Go ahead and hit enter.
Now we'll commit them with
git commit -m "Initial Commit"
and hit enter.
Now let's add our Git server as a remote.
For me this would be
git remote add origin git@
and then my IP address of my rhhost2 VM
192.168.1.125
You want to use your IP address here,
:/srv/git/git/GitProjectTwo.git
now press enter.
Now verify with git remote -v
and hit enter.
We can see that we have one remote host
for fetching and pushing to.
Now let's push our files to the remote.
git push origin master
and hit enter.
Say yes to accept the fingerprint
and now type in the Git user's password and hit enter.
If successful, you'll see it in the message on the screen.
Now other hosts can clone this repository
by tipping and get clone followed by the same URL.
Again, adjust the IP address to match your host.
Now, what we want to do is use SSH keys for authentication.
Once this is done, you can commit and push
to the remote repository without being bothered
with typing in a password,
you will want to be on your Git client.
I'm logged into my rhhost1 VM.
To use SSH keys for authentication,
we need to create our key pairs on our client
and then copy the public key to the Git server.
On your client, type in
ssh -keygen -t rsa -b 4096 -C "
and then your email address.
Be sure to use your email address here.
Also, if you plan on using these keys for GitHub
make sure the email address will match.
Now, hit enter.
It will ask us the name of the file to save the keys as,
you can take the default here.
For the passphrase, leave it blank and hit enter twice.
We can view the key pair by listing the .ssh directory.
To have an ls ~/.ssh and hit enter
We can see that we have an id_rsa key,
that's our private key and an id_rsa.pub,
which is our public key.
Now using the ssh-copy-id command
we can copy the public key to the Git server.
Type in clear, type in ssh-copy-id git@
and your get server IP address.
In my case, it's 192.168.1.125
You want to make sure you use your IP address here
and hit enter.
It will prompt you for the Git password
on the get server type that in and hit enter.
If successful, it'll say Number of key(s) added: 1,
it'll also say to test it.
Type in ssh git@ and your IP address.
which you see on the screen.
Let's go ahead and do that.
Type in ssh git@
in my case, it's 192.168.1.125
and hit enter.
And if it's a successful, you can log in
without a password which I did.
Once you've done this, type in exit.
Note, you can do this manually and append this public key
to the .ssh/authorized_keys file on the remote host,
but the ssh_copy_id command takes care of it for us
as well as setting permissions.
If your operating system has the ssh_copy_id command,
save yourself from trouble and use it.
You may not want people logging in interactively
as the Git user with SSH,
so you may want to change the shell
for the Git user to git-shell.
I won't go into that here, but it's a good idea.
Check the git-shell man page for more information
I have mentioned that you can also use these keys
for the GitHub repository too.
First, let's copy the SSH key into our clipboard.
Type in clear, and then type in cat ~/.ssh/id_rsa.pub.
Make sure you have the .pub file as it's the public key.
And then highlight it and copy it into your clipboard.
Now bring up a web browser and log into your GitHub account.
Click on the photo on the top right,
and then click on Settings.
In the left hand pane, click on SSH and GPG keys,
and then click on New SSH key,
give it a title,
I'm going to call mine rhhost1
and then paste the key into the next box.
And then lastly, click on, Add SSH key.
Now the key is on GitHub.
The last thing we need to do is change our remotes
from HTTPS to the Git protocol,
click on the GitHub logo and then click on your repository,
and then click on Clone or download
and click on Use SSH.
You want a copy that URL into your clipboard.
Now go back to your terminal,
type in clear, and then change into your
GitProjectOne directory.
Type in cd GitProjectOne/ and hit enter.
And let's look at the remotes,
type in git remote -v and hit enter.
You'll notice that both remotes use the HTTPS protocol.
We want to change this to Git.
type in git remote set-url origin
and then paste the URL you copied from GitHub and hit enter.
And we can verify with git remote -v and hit enter.
You could also test it by committing a change
and trying to push it.
(pleasant music)