I know this is 5+ years old but there's no accepted answer to this popular question so here's what I consider the best way considering cleanliness and future readability:
ADD A DEPLOY USER TO YOUR TEAM
Step 1: Create a new email address in your organisation's domain for a new deploy user. Something like deploy@organisation.example.com.
Step 2: Use that mailbox to create a new GitHub account (GitHub calls these "machine users") give it a username like deploy-ExampleOrganisation so it's role is clear.
Step 3: Create a user on your server called "deploy" with a command like this:
useradd -d /home/deploy -m deploy
Generate an SSH key for deploy@servername, specifying no passphrase and /home/deploy/.ssh/id_rsa as the file location:
ssh-keygen -t rsa -b 4096 -C "deploy@servername"
Add the contents of /home/deploy/.ssh/id_rsa.pub as a SSH key on your new deploy-ExampleOrganisation GitHub account: Go to Settings > SSH and GPG keys > New SSH Key.
Step 4: Create a team in your organisation called something like "Read-only deploy users", add your new user to the team and give the team Read access to any repos that will be deployed. (If you don't have an organisation account you can still give this user access to multiple private repos)
Step 5: Add your own personal machine's SSH key to deploy user's authorized keys file (/home/deploy/.ssh/authorized_keys) so that you (or your deploy script) can login as deploy when deploying code.
Boom! That's it... You now have a clean and self-documenting flow.
P.S. I tried aculich's highly up-voted answer but it felt dirty messing around with fake host names and I thought, if I come back to this in a years time am I going to easily figure out what I did to create all the keys and understand how that SSH config file makes those funny non-existent remote addresses work? Probably not!
Advantages of a deploy user over fake host names method:
- No hacks! It's standard user accounts with clear names, accessing repos through real host names.
- Less keys floating around.
- If/when you do move to additional servers, it's easy to give your Deploy user an account on all of them and just by adding 1 new key to her GitHub account, her account on the new server is ready to deploy code.
- Deploy user only has low-privilege Read-only access to only the repos listed in the team and your personal SSH keys are kept off the server so if some nasty person does gain access to your server they can't wreak havock on all your repos as well.
- Deploy tool config files (eg Capistrano) do not get dirtied up containing those confusing fake host names. (It was when they started spreading beyond the server that I really became uncomfortable with that method.)
- If you forget how the hell you did this in a years time the file ownership will lead you to the deploy user
ls -la
, the SSH key will lead you to GitHub account name ssh -T git@github.com
and hopefully then you're fully up to speed again.
- And finally... it's the method recommended by GitHub.