Script to create Public/Private-key SSH login without password (UPDATE: added port settings)

SSH_Communications_Security_logo.svgTo increment your servers security one position might by to disable ssh login with password and use certificated login instead. But if you are not familiar withe the given tools, creating the requested certificates and how to install them on the server and your client the following script will help you. Just run it on your Mac or *nix system. 

More details: http://andreasprang.de/2010/06/22/ssh-login-mit-public-key-ohne-passwort-tutorial/

echo ""
echo ""
echo "SSH_Private_Key_Login 1.1 by Andreas Prang"

read -p "Please enter target ssh server (FQN or IP w/o port!): " server_name
read -p "Please enter target ssh server port (22): " server_port
server_port=${server_port:-22}

read -p "Please enter user on target ssh server (root): " server_user
server_user=${server_user:-root}

read -p "Please enter a local path where to save keys (~/Documents/myKeys/$server_name""_$server_port/): " local_key_path
local_key_path=${local_key_path:-~/Documents/myKeys/$server_name"_$server_port"/}

echo "-----------------------------------------------------------------"

# check for slash at the end
if [ "${local_key_path:LEN - 1}" != "/" ]; then
  echo "Adding trailing slash"
  local_key_path=$local_key_path"/"
fi

echo "Creating Path "$local_key_path
mkdir -p $local_key_path

echo "Creating Certificates"
ssh-keygen -t dsa -f $local_key_path$server_name

echo "-----------------------------------------------------------------"
echo "Copy public key to "$server_user"@"$server_name":"$server_port
scp -P $server_port $local_key_path""$server_name".pub" $server_user@""$server_name":~/"

echo "-----------------------------------------------------------------"

echo "Setup key on "$server_user"@"$server_name":"$server_port
ssh -p $server_port $server_user@$server_name "mkdir -p ~/.ssh/;touch ~/.ssh/authorized_keys2;cat ~/"$server_name".pub >> ~/.ssh/authorized_keys2;rm ~/"$server_name".pub"

echo "Add private key to known hosts"
mkdir -p ~/.ssh			
touch ~/.ssh/known_hosts
cat $local_key_path""$server_name >> ~/.ssh/known_hosts

echo "Register private key on local machine"
ssh-add $local_key_path""$server_name

Download: SSH_Private_Key_Login.sh