114

I have few client systems where I need to push the ssh key and login from my server without authentication prompts.

First, on the server, I created ssh key as below which was successful

]# ssh-keygen -t rsa -N "" -f my.key

Second, tried copying the pub key but fails with no identity error. Am i doing a wrong step here?

]# ssh-copy-id my.key.pub 10.10.1.1
/usr/bin/ssh-copy-id: ERROR: No identities found
shellter
  • 36,525
  • 7
  • 83
  • 90
user3331975
  • 2,647
  • 7
  • 28
  • 30
  • did you confirm that `my.key` is there with `ls -l my.key`? Is there a step you're not showing us that did a `cd`? Probably not, but just asking as your "2nd set of eyes" ;-). Good luck. – shellter Mar 20 '14 at 12:00

16 Answers16

84

You need to use the -i flag:

ssh-copy-id -i my.key.pub 10.10.1.1

From the man page:

If the -i option is given then the identity file (defaults to ~/.ssh/id_rsa.pub) is used, regardless of whether there are any keys in your ssh-agent. Otherwise, if this: ssh-add -L provides any output, it uses that in preference to the identity file

Josh Jolly
  • 11,258
  • 2
  • 39
  • 55
  • 4
    I tried with -i option and pushed the key successfully to target. Also verified authorized_keys file and the key appears to be there.. Then after i tried ssh root@10.10.1.1 but still then prompts for a password.. is there anything more required here. ? ssh-copy-id -i my.key.pub 10.10.1.1 Now try logging into the machine, with "ssh '10.10.1.1'", and check in: .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting. ~]# ssh root@10.10.1.1 root@10.10.1.1's password: – user3331975 Mar 20 '14 at 12:40
  • 4
    ssh by default looks in `~/.ssh` for your keys etc. Since your `my.key` files appear to be in another directory, try `ssh -i /location/of/my.key 10.10.1.1` (or consider moving your my.key files into `~/.ssh`) – Josh Jolly Mar 20 '14 at 13:06
  • "Then after i tried ssh root@10.10.1.1 but still then prompts for a password." The first ssh-copy-id command is for logging in as you, not as root. You need another ssh-copy-id command for logging in as root. ssh-copy-id -i root@my.key.pub 10.10.1.1 – Underhill Sep 16 '22 at 13:10
81

Generating ssh keys on the client solved it for me

$ ssh-keygen -t rsa
endumiuz
  • 911
  • 6
  • 4
34

Run following command

# ssh-add

If it gives following error: Could not open a connection to your authentication agent

To remove this error, Run following command:

# eval `ssh-agent`
SMSM
  • 1,509
  • 3
  • 19
  • 34
23

The simplest way is to:

ssh-keygen
[enter]
[enter]
[enter]

cd ~/.ssh
ssh-copy-id -i id_rsa.pub USERNAME@SERVERTARGET

Att:

Its very very simple.

In manual of "ss-keygen" explains:

"DESCRIPTION ssh-keygen generates, manages and converts authentication keys for ssh(1). ssh-keygen can create RSA keys for use by SSH protocol version 1 and DSA, ECDSA or RSA keys for use by SSH protocol version 2. The type of key to be generated is specified with the -t option. If invoked without any arguments, ssh-keygen will generate an RSA key for use in SSH protocol 2 connections."

Túlio Ricardo
  • 339
  • 2
  • 2
  • Thanks. This worked. I didn't have to generate a key as I already had one. It was changing to the .ssh directory and using that command that did it. – Sean Rasmussen Oct 04 '20 at 16:00
10

The ssh-copy-id is not able to locate the id_rsa.pub file that is generated by ssh-keygen in your system, Use the following command to complete:

  1. find the path of the .pub file: locate *.pub
  2. copy the path (ex: /home/user_name/.ssh/id_rsa.pub) and run the following command: ssh-copy-id -i /home/user_name/.ssh/id_rsa.pub hostname
Martijn
  • 13,225
  • 3
  • 48
  • 58
Guru
  • 2,739
  • 1
  • 25
  • 27
8

You need to specify the key by using -i option.

ssh-copy-id -i your_public_key user@host

Thanks.

Ashish Bhosle
  • 609
  • 5
  • 18
  • This is the same answer as the one posted on [March 20,2014](https://stackoverflow.com/a/22532600/3276962). – Jerry May 10 '22 at 14:58
7

Old post but I came up with this problem today, ended up googling and had found myself here. I had figured it out on my own but thought I'd share my issue & solution in my case to help out anyone else who may have the same issue.

Issue:

[root@centos [username]]# ssh-keygen -t rsa

Enter file in which to save the key (/root/.ssh/id_rsa):I HAD JUST HIT ENTER

/usr/bin/ssh-copy-id: ERROR: No identities found

Solution:

Enter file in which to save the key (/root/.ssh/id_rsa): **/home/[username]/id_rsa**

Be sure if you are doing this as root you are coping the key into the user directory you wish to login with. NOT the root user directory.

I was sshing into the machine when performing this operation, so I guess ssh-copy-id just point to the dir you are logged in as by default.

Hope this helps anyone.

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
n1c0
  • 89
  • 1
  • 2
5

came up across this one, on an existing account with private key I copied manually from elsewhere. so the error is because the public key is missing

so simply generate one from private

 ssh-keygen -y -f ~/.ssh/id_rsa > ~/.ssh/id_rsa.pub
Dan Kuida
  • 1,017
  • 10
  • 18
4

Actually issues in one of Ubuntu machine is ssh-keygen command was not run properly. I tried running again and navigated into /home/user1/.ssh and able to see id_rsa and id_rsa.pub keys. then tried command ssh-copy-id and it was working fine.

2

FWIW, the -i option was a red herring for me. ssh-copy-id will use ~/.ssh/id_rsa.pub by default.

What was keeping it from working for me was the permissions on the ~ directory, the ~/.ssh directory, and the ~/.ssh/authorized_keys file on the remote computer.

All three need to be set with chmod 755 ~ ~/.ssh ~/.ssh/authorized_keys, then ssh-copy-id your-remote-server.com will work.

Mark Moore
  • 195
  • 2
  • 7
  • according to the [documentation](https://docs.digitalocean.com/products/droplets/resources/troubleshooting-ssh/authentication/#fixing-key-permissions-and-ownership), permissions should be `700` and `600` accordingly. – lepe Oct 04 '21 at 03:57
1

I had the same issue. The problem was my ~/.ssh/id_rsa.pub file being empty. I overwritten it somehow while messing around with my ssh config. The problem is solved after correcting the file.

Asocia
  • 5,935
  • 2
  • 21
  • 46
0

I had faced this problem today while setting up ssh between name node and data node in fully distributed mode between two VMs in CentOS.

The problem was faced because I ran the below command from data node instead of name node ssh-copy-id -i /home/hduser/.ssh/id_ras.pub hduser@HadoopBox2

Since the public key file did not exist in data node it threw the error.

  • My issue was that I was running the command from `root` (via sudo, as I wanted `root` to login without password to a remote server). Ubuntu apparently was looking the key in the user's home which was missing the `.ssh` directory. It seems that `ssh-copy-id` uses that `.ssh` directory temporally in order to upload the key. Thanks to `-i /home/.../id_rsa.pub` argument, I was able to notice that. After creating that directory, I was able to upload the key and to login from `root` successfully. – lepe Oct 04 '21 at 04:11
0

Use simple ssh-keyscan hostname to find if key(s) exists on both sites:

ssh-keyscan rc1.localdomain
[or@rc2 ~]$ ssh-keyscan rc1
# rc1 SSH-2.0-OpenSSH_5.3
rc1 ssh-rsa AAAAB3NzaC1yc2EAAAABI.......==

ssh-keyscan rc2.localdomain
[or@rc2 ~]$ ssh-keyscan rc2
# rac2 SSH-2.0-OpenSSH_5.3
rac2 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAys7kG6pNiC.......==
fcdt
  • 2,371
  • 5
  • 14
  • 26
HankerPL
  • 21
  • 4
0
ssh-keygen

then

sudo service ssh restart

after this, you should be able to use ssh-copy-id without having to provide any additional commands

Amir Khalil
  • 187
  • 2
  • 14
0

Might sound stupid, but for some reason the file I needed to copy did not have the correct name. It was called id_rsa.pu. Be sure the file name is exactly id_rsa.pub or tell ssh-copy-id to pickup on a specific identity file with ssh-copy-id -i path/to/identity_file.pub

HotFix
  • 157
  • 1
  • 11
-3

In my case it was the missing .pub extension of a key. I pasted it from clipboard and saved as mykey. The following command returned described error:

ssh-copy-id -i mykey localhost

After renaming it with mv mykey mykey.pub, works correctly.

ssh-copy-id -i mykey.pub localhost
fracz
  • 20,536
  • 18
  • 103
  • 149