Ubuntu 16.04本地Git服务器搭建及同网客户端配置步骤与命令
Hey there, let's get your local Git server up and running smoothly on Ubuntu 16.04, and make sure your client connects without issues. I've walked through this process dozens of times, so let's break it down into clear, actionable steps.
Server-Side Setup (Ubuntu 16.04)
1. Install Git on the Server
First, update your package list and install Git:
sudo apt-get update && sudo apt-get install git -y
2. Create a Dedicated Git User
For security, it's best to use a separate user for Git operations instead of your main account. Create the user and set a password when prompted:
sudo adduser git
3. Configure SSH Key Authentication (Skip Password Prompts)
Password-based auth is less secure and annoying to use. Set up key-based access:
- Switch to the git user:
sudo su - git - Create the SSH directory and set strict permissions (critical for SSH to work):
mkdir ~/.ssh && chmod 700 ~/.ssh touch ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys - Now, from your client machine, generate an SSH key (if you don't have one):
ssh-keygen -t rsa -b 4096 - Copy the client's public key to the server's
authorized_keysfile. The easiest way is:
Replacessh-copy-id git@your-server-ipyour-server-ipwith the actual IP address of your Git server.
4. Create a Bare Git Repository
A bare repository has no working directory—it's designed solely for sharing and pushing/pulling changes. Let's create one:
- Still as the git user, make a directory to store your repositories:
mkdir -p /home/git/repos - Navigate to the repo directory and initialize the bare repo:
Replacecd /home/git/repos git init --bare my-project.gitmy-project.gitwith your actual project name.
Client-Side Setup (Ubuntu 16.04)
1. Install Git on the Client
If you haven't already, install Git on your client machine:
sudo apt-get update && sudo apt-get install git -y
2. Clone the Repository & Test Basic Operations
Now connect to the server and clone the repo:
git clone git@your-server-ip:/home/git/repos/my-project.git
Once cloned, test pushing a commit to make sure everything works:
cd my-project echo "Test file" > test.txt git add test.txt git commit -m "First test commit" git push origin master
Troubleshooting Common Issues
If you were having problems before, check these common fixes:
- Permission Errors: Ensure the git user owns the repository directory:
sudo chown -R git:git /home/git/repos - SSH Connection Failures: Verify SSH settings on the server:
- Open the SSH config file:
sudo nano /etc/ssh/sshd_config - Make sure these lines are uncommented and set correctly:
PubkeyAuthentication yes PasswordAuthentication yes # Optional, keep if you want password fallback - Restart the SSH service:
sudo systemctl restart ssh
- Open the SSH config file:
- Firewall Blocking: If you're using UFW, ensure port 22 (SSH) is open:
sudo ufw allow ssh
内容的提问来源于stack exchange,提问作者Jijo

