Feature #87
Updated by Daniel Curtis almost 11 years ago
h2. Setup the client Start by creating a new SSH key pair: <pre> ssh-keygen -t ecdsa </pre> And then set the clients information: <pre> git config --global user.email "user@example.com" git config --global user.name "John Doe" </pre> h2. Set up the server These instructions were performed on a Debian system, so assume them to be the same on Ubuntu. Substitute the package installation commands as required if you’re on an alternative distribution. h3. Add your public key to the server: <pre> ssh myuser@server.com mkdir .ssh scp ~/.ssh/id_ecdsa.pub ~/.ssh/id_rsa.pub user@example.com:.ssh/authorized_keys </pre> h3. SSH into our server and install Git: <pre> ssh example.com sudo apt-get update sudo apt-get install git-core </pre> h2. Adding a user If you intend to share these repositories with any collaborators, at this point you’ll either: * Want to install something like Gitosis (outside the scope of "this article, but this is a good, if old, tutorial":http://scie.nti.st/2007/11/14/hosting-git-repositories-the-easy-and-secure-way ); or * Add a “shared” Git user. We’ll be following the latter option. h3. Add a Git user: <pre> sudo adduser git </pre> Now you’ll need to add your public key to the Git user’s authorized_keys: <pre> sudo mkdir /home/git/.ssh sudo cp ~/.ssh/authorized_keys /home/git/.ssh/ sudo chown -R git:git /home/git/.ssh sudo chmod 700 /home/git/.ssh !$ sudo chmod 600 /home/git/.ssh/* </pre> Now you’ll be able to authenticate as the Git user via SSH. Test it out: <pre> ssh git@example.com </pre> h2. Add your repositories If you were to not share the repositories, and just wanted to access them for yourself (like I did, since I have no collaborators), you’d do the following as yourself. Otherwise, do it as the Git user we added above. h3. If using the Git user, log in as the git user: them: <pre> login git </pre> h3. Now we can create the our repositories: <pre> mkdir myrepo.git cd !$ git --bare init </pre> The last steps creates an empty repository. We’re assuming you already have a local repository that you just want to push to a remote server. *Repeat that last step for each remote Git repository you want.* Log out of the server as the remaining operations will be completed on your local machine. Configure your development machine h3. First, we add the remotes to your local machine. If you’ve already defined a remote named origin (for example, if you followed GitHub’s instructions), you’ll want to delete the remote first: <pre> git remote rm origin </pre> h3. Add the new remote: <pre> git remote add origin git@server.com:myrepo.git git push origin master </pre> h3. You’ll probably also want to make sure you add a default merge and remote: <pre> git config branch.master.remote origin && git config branch.master.merge refs/heads/master </pre> Now you can push/pull from origin as much as you like, and it’ll be stored remotely on your own example.com remote repository. h2. Move Bonus points: Make SSH to a different port more secure This has been extensively covered by the "excellent Slicehost tutorial":http://articles.slicehost.com/2009/3/31/debian-lenny-setup-page-1 , but just to recap: h3. Edit the SSH config and change the following values: config: <pre> sudo vi /etc/ssh/sshd_config </pre> h3. And change the following values: > Port 2207 > ... > PermitRootLogin no > ... > AllowUsers myuser git > ... > PasswordAuthentication no Where @2207@ is a port of your choosing. Make sure to add this so your Git remote: <pre> git remote add origin ssh://git@example.com:2207/myrepo.git </pre>