How do I add my RSA or DSA keyfile in shell script itself for the connection so that I need to carry only one file on my USB pen drive instead of $HOME/.ssh/id_rsa file under Unix / Linux operating systems?

Linux / Unix / Apple OS X / BSD operating system stores your rsa / dsa private and public keys in your $HOME/.ssh/ directory. You can use the following syntax to specific a file from which the identity (private key) for RSA or DSA authentication is used by the ssh command:

Now just run a script and it will get connected to remote server called server1.cyberciti.biz:$ ./path/to/your/script

Sample Shell Script

I use the following to rescue or connect to my home server called nas.cyberciti.biz:

WARNING! These examples may expose your private data as it contains the private key for authentication. These files (or shell script) contain sensitive data and should be readable by the user but not accessible by others (read/write/execute)

Because of the potential for abuse, this file must have strict permissions: read/write for the user, and not accessible by others. Use the chown and chmod commands as follows:chown vivek:vivek script chmod 0700 script Run the script as follows:$ ./script $ ./script uptime Sample outputs:

07:46:03 up 13 days, 1:07, 1 user, load average: 0.00, 0.00, 0.00

See also:

This will ask you to set a passphrase for your key. And incorporate the encrypted key into your shell script. It would look like this: —–BEGIN RSA PRIVATE KEY—– Proc-Type: 4,ENCRYPTED DEK-Info: DES-EDE3-CBC,34B03A2FC16BDFAD ………………………………. ………………………………. —–END RSA PRIVATE KEY—–