Setting Up a Personal Git Server on a VPS
I’ve been sitting on an under-used VPS for too long. Time to backup all my git repos that I don’t feel like hosting on GitHub!
- Create ssh keys on host
- Setup git user on vps
- Add ssh key to access list
- log in to VPS as git user
- create
~/.ssh/authorized_keys
file - add public ssh key of user that wants to push to the git repo
-
from that user, run the following command replacing
git@vps_domain
with the git user we created and the VPS addresscat ~/.ssh/id_rsa.pub | ssh git@vps_domain "cat >> ~/.ssh/authorized_keys"
- back in your git user’s ssh session, verify that the key of your local user is there with
cat ~/.ssh/authorized_keys
- Optional: create user folders in your git user’s home directory
- similar to github’s http://github.com/user_folder/repo_name.git file structure
- Create bare git repo on the vps
- either in the git user’s home directory:
/home/git/
-
or in the separate user directories:
/home/git/user_folder/
git init --bare repo_name.git
- either in the git user’s home directory:
-
Point the local git repo at the new remote
git remote set-url origin git@vps_domain:user_folder/repo_name.git
-
Double check your work!
git remote -v
Now you can push and pull like usual, except the code is no longer on GitHub for all the world to grab your precious secrets.
Sources: