ssh logins using keys

I have wanted to experiment and expand upon different ways of ssh’ing into one linux box from another using the ssh-keygen.  This article demonstrates several different methods that I have tested and used throughout the years.

Actually what has happened, is that over time, scripting evolved and always seem to make tweaks along the way.

yum install -y sshpass
echo -e | ssh-keygen
echo "password" > password.txt
sshpass -f password.txt ssh-copy-id 10.0.0.1 -f
sshpass -f password.txt ssh-copy-id 10.0.0.2 -f

Another variation.

echo -e | ssh-keygen
ssh-copy-id -i .ssh/id_rsa.pub root@10.0.01
ssh root@10.0.0.1

A more lengthy variation.

ssh-keygen -t rsa
ssh testuser@10.0.0.1 mkdir -p .ssh
cat .ssh/id_rsa.pub | ssh testuser@10.0.0.1 'cat >> .ssh/authorized_keys'
ssh testuser@10.0.0.1 "chmod 700 .ssh; chmod 640 .ssh/authorized_keys"
ssh testuser@10.0.0.1

My favorite.

yum install -y sshpass
yes 'y' | ssh-keygen -t rsa -N ""
sshpass -p mypassword ssh-copy-id 10.0.0.1 -f

A small variation to the previous one is to use a password file as seen in the first cli arguments.

CentOS Linux release 7.6.1810 (Core)

Successfully Tested February 12, 2019..

yum install -y sshpass
echo 'StrictHostKeyChecking no' > ~/.ssh/config
echo -e | ssh-keygen -t rsa -N ""
sshpass -p mypassword ssh-copy-id 10.0.0.1 -f

CentOS Linux release 8.1.1911 (Core)

Successfully Tested April 28, 2020.

cat /dev/zero | ssh-keygen -m PEM -t rsa -q -N ""
echo 'StrictHostKeyChecking no' > ~/.ssh/config
sshpass -p mypassword ssh-copy-id 10.0.0.1

Source(s)

  • https://stackoverflow.com/questions/3659602/automating-enter-keypresses-for-bash-script-generating-ssh-keys
  • https://bencane.com/2013/07/22/ssh-disable-host-checking-for-scripts-automation/
  • https://serverfault.com/questions/939909/ssh-keygen-does-not-create-rsa-private-key