I use SSH and SCP commands in the shell and in scripts, a lot. Entering a password every time gets tiresome. Fortunately SSH can authenticate automatically if you store the private key of the remote system on your computer. I've been using this "setuptrust" script for years, it works flawlessly on all systems with a Bash shell and ssh-keygen installed.
The setuptrust script
#!/bin/bash if echo $1 | grep -q @ then set $(echo $1 | tr '@' ' ') fi if ! [ $# -eq 2 -o $# -eq 4 ] then echo "usage: username ip [-p port]" && exit 1 fi [ ! -f ~/.ssh/id_rsa.pub ] && ssh-keygen -t rsa user=${1} pass=${2} shift 2 cat ~/.ssh/id_rsa.pub | ssh "${user}"@${pass} $* 'mkdir -p .ssh; cat >> .ssh/authorized_keys'
What setuptrust Does
Each computer must have a public key and a private key, they are both generated using ssh-keygen. This must be done on the computer you are running the "ssh" command on. The public key generated must be appended to the .ssh/authorized_keys file in the home directory of the target user on the remote system. The authorized_keys file and the .ssh directory it resides in must be read protected, otherwise sshd on the remote system will refuse to let you authenticate. The script does this automatically, resulting in this setup:
Your Computer | Remote Computer |
.ssh/id_rsa | .ssh/id_rsa |
.ssh/id_rsa.pub | .ssh/id_rsa.pub |
.ssh/authorized_keys + .ssh/id_rsa.pub |