As a developer, you may often use an SSH client to login into the shell of a remote machine for editing code or executing a command. Within an open source world, Github is the most popular platform to share and manage your code. It provider two methods (HTTPS or SSH) to clone the code:
- If you use https method to communicate with github, you will need to enter your github password to push the code every time. This is a very very boring thing.
- In contrary, it is unnecessary to enter password any more when you use ssh to communicate with github.
Now, i will show you how to generate a pair of ssh private and public keys to use it with your github account.
Step 1. Run following command to generate ssh key:
ssh-keygen -t rsa -f ~/.ssh/mykeyfile -C 'my new ssh keys'
- The -t option to specify the type of the key to create. Just use rsa for it.
- The -f option to specify the file to store the keys. I named it mykeyfile. You could just use your own name for it.
- The -C option to specify a comment for this key.
Once you run above command in your shell, you will see following content:
Just press enter to use empty for passphrase. You will ask again to enter same passphrase. Just press enter again to leave it empty.
As you can see, the private key is stored in mykeyfile. And, the public key is stored in mykeyfile.pub. You could find them under ~/.ssh fold.
Step 2. Copy the content of mykeyfile.pub into your github account. You could open the file with your favourite editor and copy the all of content within it. I often use vim to open such kind of file.
- Go to github settings section.
- Choose ssh key section and click add ssh key button.
- Copy the public key into it as follow.
After saving the key into your github account, you could use ssh to push the code into your repository. You don’t need to enter the password anymore.
In addition, if you need to name the remote machine for ssh, you just need to add following configuration into ~/.ssh/config file.
Host jackgitub User jack HostName github.com PreferredAuthentications publickey IdentityFile ~/.ssh/mykeyfile
- Host – specify the name of a host machine.
- User – it’s a user of a host machine. For the github host, it should be your github username.
- HostName – it’s the domain of the machine. You could also use a ip address for it.
- PreferredAuthentications – specify use the public key to authorization.
- IdentityFile – specify the ssh key file for this host.
Now, you could use following command to login into shell of remote machine via ssh.
If you didn’t set above ssh configuration, you could also type following command to login into shell of remote machine.
That’s it. SSH is a very powerful tool you should learn to use.