
๐ 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:
- “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_keyto keep your old keys safe.
- Action: Press Enter. The default path (
- “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 OS | Command 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)
- Log in to the Git User: You must use the administrative user’s credentials to execute this, but you will switch to the dedicated
gituser.Bashsudo -i -u git - Create SSH Directory (if you missed it earlier):Bash
mkdir -p ~/.ssh chmod 700 ~/.ssh touch ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys - Add Your Public Key: Open the file using
viornanoand paste the key from your clipboard on a new line.Bashvi ~/.ssh/authorized_keys - Exit the
gituser:BashexitYou 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!
- Create the Base Directory and Set Ownership: (If you ran this before, running it again is safe.)Bash
sudo mkdir -p /srv/git sudo chown -R git:git /srv/git - Initialize the Repository (The Clean Way!):Bash
sudo -u git git init --bare /srv/git/my_awesome_project.gitThis command tells the system: “Run thegit initcommand, but pretend that thegituser 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:
- Move into the new local directory:
cd my_awesome_project
- Create a test file:
- For Bash/Linux users,
touch README.md - For Windows: type nul > README.md
- For Bash/Linux users,
- Add and commit locally:
git add . git commit -m "Initial commit on my private server!"
- Push to your new remote server!
- Bash
git push origin master # OR: git push origin main
- Bash
You now have a fully functional, secure, private Git server. Feel the power! What are you going to build first? Happy coding! ๐