20

As part of a startup script I have to automatically open several gnome-terminal windows, one of those terminals automatically send:

ssh [email protected]

The limitation with this is that I still need to type a password to complete the SSH connection.

What I want to do is for my script to initiate the command and complete the connection. To that extent, I attempted to follow the instructions as outlined in the accepted answer here.

From the system I wish to connect from, I ran:

ssh-keygen
Enter file in which to save the key: /home/user/ssh/keys/server1key

It then prompted me to enter a passphrase. I left this empty, as I wasn't sure what it would be for, so I assumed setting one would require unlocking it with said passphrase each time I'd use it.

Continuing with the instructions linked above, I then ran and received the following:

ssh-copy-id user@IP
ERROR: No identities found

A quick search revealed that I needed to specify the location of the key, as it was not in the default save location, so I fixed that:

ssh-copy-id -i /home/user/ssh/keys/server1key.pub [email protected]

After asking for the server's password, it successfully added the key. However, upon attempting to log in with "ssh user@IP", I was still prompted for the password.

As far as I'm aware, I followed the linked instructions correctly, so either I'm missing something, or perhaps an existing configuration is preventing me for getting this to work?

Both systems use 18.04 and openssh.

2
  • So I need to specify the location of the .pub file when sending the SSH command?
    – hiigaran
    Aug 7, 2018 at 18:18
  • I've got a bit of a different way of organising my files, hence the non-default locations, but you guys have helped greatly. Thanks. Feel free to make it an answer I can accept.
    – hiigaran
    Aug 7, 2018 at 18:23

4 Answers 4

36

It fails for the same reason that ssh-copy-id failed the first time - i.e. because you have chosen a non-default location for the identity file.

You can resolve it in the same way, by adding -i /home/user/ssh/keys/server1key to your ssh command - note that the client side needs the location of the private key file.

From man ssh

 -i identity_file
         Selects a file from which the identity (private key) for public
         key authentication is read.  The default is ~/.ssh/identity for
         protocol version 1, and ~/.ssh/id_dsa, ~/.ssh/id_ecdsa,
         ~/.ssh/id_ed25519 and ~/.ssh/id_rsa for protocol version 2.

Alternatively, you may wish to create a ~/.ssh/config file entry for the host along the lines of

Host            somename
Hostname        192.168.1.3
User            user
IdentityFile    /home/user/ssh/keys/server1key
4
  • 1
    I always assumed that the config file required indentation - I see that's not the case. Intruiging. Aug 7, 2018 at 19:10
  • Anyone using OpenSSH should have a ~/.ssh/config, otherwise OpenSSH uses the system wide default /etc/ssh/ssh_config. In case anyone finds it helpful, I uploaded a pre-built ssh_config to my GitHub a while back as a starting point for users who may not have time to read the ssh_config man page
    – JW0914
    Aug 7, 2018 at 23:47
  • @JW0914 that's only half true: individual values in ~/.ssh/config will override those of the global ssh_config, but just having the file will not disable the system wide configuration. Aug 13, 2018 at 0:04
  • @SebastianStark Perhaps you misread my comment, as I never stated it would disable the system wide configuration. What I stated was 100% factually accurate.
    – JW0914
    Aug 14, 2018 at 0:24
3

Another reason that ssh-copy-id fails is that the key hasn't been added to the SSH agent.

First, check and start if ssh-agent is running:

eval "$(ssh-agent -s)"

If you get in process ID, you can add your key:

ssh-add -k /home/user/ssh/keys/server1key

With -k you add the key to the keychain.

Check if keys are added with:

ssh-add -l

ssh-copy-id should be working now.

-1

Hope this will work for you.. "sshpass -p yourpassword" it will automatically login to remote host.

[root@localhost .ssh]# sshpass -p password123 ssh -l root localhost

-1

Just execute 2 commands:

ssh-keygen
ssh-copy-id

Long answer:

user@ip:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa): temp_file
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in stemp.
Your public key has been saved in stemp.pub.
The key fingerprint is:
SHA256:5**M user@ip
The key's randomart image is:
+---[RSA 2048]----+
|  ...            |
| ...             |
| ..              |
| ..+.    o .     |
|                 |
|    B            |
|                 |
|=      *         |
|=+   o .         |
+----[SHA256]-----+

And then:

user@ip:~$ ssh-copy-id -i ~/.ssh/id_rsa.pub ipDestination -p portDest

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .