#! /bin/bash ## SSH login without password ## You want to use Linux and OpenSSH to automize your tasks. Therefore you need an automatic login from user a / host A to user b / Host B. You don't ## want to enter any passwords, because you want to call ssh from a within a shell script. ## The first step is to generate a pair of authentication keys for user a in host A by issuing 'ssh-keygen -t rsa'. ## If 'ssh-keygen -t rsa' has ever been executed by user a, in the directory ~/.ssh of user a you will not find a pair if authentication keys, name respectively as 'id_rsa.pub' and 'id_rsa'. Only when after 'ssh-keygen -t rsa' is executed will these pair of authentication files be found there. ## In order to access host B from host A with username a, the authentication key id_rsa.pub (found in ~/.ssh of user a) has to be appended to host B's ~/.ssh/authorized_keys. ## As a result, ~/.ssh/ of user a in Host B, you will find a file named authorized_keys, which has the same content as id_rsa.pub in ~/.ssh of user a in Host A. ## The pairs of command lines below do just that. This script is to be run on your local PC (e.g., user / host A = tlyoon / 10.211.189.100) so that you can access remotely the intended remote computer. ################################ ssh-keygen -t rsa ssh-agent $SHELL ssh-add ssh-add -L ### Warning: When this command line is run, a fresh pair of id_rsa.pub' and 'id_rsa' will be generated if choose 'overwrite' when prompted. ### Overwritting the existing authentication pairs will change these keys. As a result, the previously configured password-free access to remote PC by tlyoon to the remote computers to fail. ### Conclusion: If prompted to overwrite existing keys, safest to choose 'no'. ################################ ## Replace XXX with the IP address of the remote PC you want to access ## Replace YYY with the userid ## Uncomment and modify the following command lines #ssh user@XXX mkdir -p .ssh #cat ~/.ssh/id_rsa.pub | ssh user@YYY 'cat >> .ssh/authorized_keys'