๐Ÿ”‘ Keys, Clones, and Clean Setups for Your Private Git Server!

๐Ÿ”‘ Keys, Clones, and Clean Setups for Your Private Git Server!

Welcome back, fellow coders! If you followed our last guide, your powerful CentOS Stream 9 server is patiently waiting, ready to host your code. But to truly unlock its power, we need to bridge the gap between your laptop and your shiny new Git hostโ€”and that means creating the perfect SSH key pair!

This post is all about making the final connection seamless, secure, and ensuring you never run into those frustrating permission errors again. Get ready to run git push without a hitch!


๐Ÿ”‘ Step 1: Forge Your Digital Key (Local Machine)

SSH key authentication is the gold standard for Git access. It replaces tedious passwords with two cryptographically linked files: a Private Key (which stays secret on your computer) and a Public Key (which is safe to share with your server).

We’re going to use the Ed25519 algorithm. Why? Because itโ€™s the most modern, highly secure, and lightning-fast standard!

Open your terminal (PowerShell, Command Prompt, or Terminal) and run this universal command. Remember to replace the email with your own identifier!

Bash

ssh-keygen -t ed25519 -C "your_key_comment@your-computer-name"

Prompt Breakdown:

  1. “Enter file in which to save the key…”
    • Action: Press Enter. The default path (~/.ssh/id_ed25519) is perfect. Accepting the default means your system can find the key automatically.
    • Caution: If you already have a key at that location, the utility will ask if you want to overwrite it. Choose ‘n’ (no) and enter a new filename like ~/.ssh/git_server_key to keep your old keys safe.
  2. “Enter passphrase…”
    • Action (Highly Recommended): Enter a strong passphrase. This encrypts your private key on your disk. Even if your laptop is compromised, the attacker canโ€™t use the key without this password.

When it’s finished, you’ll see a unique “randomart image”โ€”that means your secure key pair is ready!


๐Ÿ“‹ Step 2: Grab the Public Key Contents

Now, we need to copy the public key file (.pub extension) so we can paste it into the CentOS server. Don’t open it in a word processor; use the terminal to copy the clean text directly!

Your OSCommand to Copy Key to Clipboard
Windows (PowerShell)`type $env:USERPROFILE.ssh\id_ed25519.pub
macOS (Terminal)`cat ~/.ssh/id_ed25519.pub
Linux (Terminal)cat ~/.ssh/id_ed25519.pub or `cat ~/.ssh/id_ed25519.pub

The entire key (starting with ssh-ed25519 ...) is now on your clipboard!


๐Ÿ›ก๏ธ Step 3: Finalizing Server Access (The “Dubious Ownership” Fix)

You must now log back into your CentOS server as your administrative user, switch to the dedicated git user, and paste the key.

A. Add the Key to the Server (CentOS Terminal)

  1. Log in to the Git User: You must use the administrative user’s credentials to execute this, but you will switch to the dedicated git user.Bashsudo -i -u git
  2. Create SSH Directory (if you missed it earlier):Bashmkdir -p ~/.ssh chmod 700 ~/.ssh touch ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys
  3. Add Your Public Key: Open the file using vi or nano and paste the key from your clipboard on a new line.Bashvi ~/.ssh/authorized_keys
  4. Exit the git user:Bashexit You should now be back in your administrative user’s shell.

B. Create the Repository (The Flowless Fix!)

In the previous guide, we may have used sudo to initialize the repository, which can cause Git to throw the dreaded “dubious ownership” error because the directory was created by root instead of the git user.

The fix is simple: we initialize the repository as the git user from the start using the sudo -u git wrapper!

  1. Create the Base Directory and Set Ownership: (If you ran this before, running it again is safe.)Bashsudo mkdir -p /srv/git sudo chown -R git:git /srv/git
  2. Initialize the Repository (The Clean Way!):Bashsudo -u git git init --bare /srv/git/my_awesome_project.git This command tells the system: “Run the git init command, but pretend that the git user is executing it.”

๐Ÿš€ Step 4: The Grand Finale: Clone and Go!

Now, switch back to your local machine and run the clone command. This is where all the hard work pays off!

Bash

git clone git@<Your-Server-IP>:/srv/git/my_awesome_project.git

(Replace <Your-Server-IP> with the actual IP address of your CentOS server, e.g., git@192.168.50.22)

Success Confirmation:

If successful, you will see a message:

Cloning into 'my_awesome_project'...
warning: You appear to have cloned an empty repository.

That is the sound of success! You are connected and ready to rock!

Try Your First Git Commands:

  1. Move into the new local directory:
    • cd my_awesome_project
  2. Create a test file:
    • For Bash/Linux users, touch README.md
    • For Windows: type nul > README.md
  3. Add and commit locally:
    • git add . git commit -m "Initial commit on my private server!"
  4. Push to your new remote server!
    • Bashgit push origin master # OR: git push origin main

You now have a fully functional, secure, private Git server. Feel the power! What are you going to build first? Happy coding! ๐ŸŽ‰

Leave a Reply

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


Translate ยป