Generating the SSH key
ssh-keygen -t rsa
[[email protected]]$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/droidzone/.ssh/id_rsa): /home/droidzone/.ssh/id_rsa_hostgator_ubuntu Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/droidzone/.ssh/id_rsa_hostgator_ubuntu. Your public key has been saved in /home/droidzone/.ssh/id_rsa_hostgator_ubuntu.pub. The key fingerprint is: 1a:24:ff:8d:f0:8a:64:c4:7a:de:d1:8f:15:5a:55:c5 [email protected] The key's randomart image is: +--[ RSA 2048]----+ +-----------------+
Now, you can view your public key with:
cat /home/droidzone/.ssh/id_rsa_hostgator_ubuntu.pub
Logging in to the server
You can login to the server using your password with:
ssh -p 2222 [email protected]
Here, myusername is my user name (obviously), and 174.12.1.7 is the ip address of the host.
[[email protected]]$ ssh -p 2222 [email protected] [email protected]'s password: Last login: Thu Sep 2 12:06:19 2012 from 9.3.34.8 [hostgator ~]$
Adding the key to the remote server
Now, that you can confirm the server credentials and generated a public/private key pair, you wish to create a passwordless login to access the server quickly from your machine.
You have already created the pair, so the next step is to transfer your public key to the server. You can do this by manually adding the public key to your server’s ~/.ssh/authorized_keys2 or ~/.ssh/authorized_keys (newer). Just do a:
ls -l ~/.ssh
and see which file your server uses.
You should now copy the entire text shown by:
cat /home/droidzone/.ssh/id_rsa_hostgator_ubuntu.pub[/code] and append it to the end of the remote server's ~/.ssh/authorized_keys2 or ~/.ssh/authorized_keys. For this you can open the relevant file in emacs.emacs ~/.ssh/authorized_keys2At the end of the current last line (last letter), press Enter.
Paste with Ctrl-Shift-V (i.e paste the new public key as the last line).
Save the file with Ctrl-X Ctrl-C.
Now, you can do a passwordless login to the server with:
ssh -i ~/.ssh/id_rsa_hostgator_ubuntu -p 2222 [email protected]Here, id_rsa_hostgator_ubuntu is your private key file. You can of course automate this with this a bash script.
Adding a ssh key to remote server with a single command:
Generate key:
ssh-keygen -t rsaAdd the key:
cat ~/.ssh/id_rsa.pub | ssh [email protected] 'cat >> .ssh/authorized_keys'Joel G Mathew, known in tech circles by the pseudonym Droidzone, is an opensource and programming enthusiast.
He is a full stack developer, whose favorite languages are currently Python and Vue.js. He is also fluent in Javascript, Flutter/Dart, Perl, PHP, SQL, C and bash shell scripting. He loves Linux, and can often be found tinkering with linux kernel code, and source code for GNU applications. He used to be an active developer on XDA forums, and his tinkered ROMS used to be very popular in the early 2000s.
His favorite pastime is grappling with GNU compilers, discovering newer Linux secrets, writing scripts, hacking roms, and programs (nothing illegal), reading, blogging. and testing out the latest gadgets.
When away from the tech world, Dr Joel G. Mathew is a practising ENT Surgeon, busy with surgeries and clinical practise.