Solved: ssh-copy-id port 22 Connection refused

( It would mean the world if you would consider following @colhountech on twitter. It won’t cost you anything but it gives me great encouragement to continue writing these posts)

This had me for a couple of minutes and a google search did not give me the answer, so I’m dropping it here.

I can sucessfully ssh into a new ubuntu20 vm, but I can’t ssh-copy-id

This works!

ssh -l username  username@hostname
Password: ********
Welcome to Ubuntu 20.04.2 LTS (GNU/Linux 5.4.0-70-generic x86_64)

But this doesn’t work:

ssh-copy-id  username@hostname
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/username/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/bin/ssh-copy-id: ERROR: ssh: connect to host hostname port 22: Connection refused

why? Answer below

First, I tried PasswordAuthentication

One person recommend

Try changing the PasswordAuthentication to yes

Edit ssh config:

sudo nano /etc/ssh/sshd_config

Change this line:

PasswordAuthentication no

to

PasswordAuthentication yes

Restart daemon:

sudo systemctl restart sshd

Do ssh-copy-id:

ssh-copy-id someuser@<my-ip>

This didn’t work so I reverted changes to ssh_config and restarted daemon.

I turned of the ufw firewall temporarily to see if it was a firwall issue

sudo ufw disable

Nope, still not working, better turn the firewall back on:

sudo ufw enable

The Solution is that I have both IP4 and IPV6 enabled, so I just had to use the IPV4 ip address to copy the ssh id:

ssh-copy-id  username@192.168.2.x
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/username/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
Password: ************

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'username@192.168.2.x'"
and check to make sure that only the key(s) you wanted were added.

Edit: this issue is probably related to a more recent posts on WSL not resolving DNS hostnames correctly on local network. See this post for possibly a better solution

References

Scroll to Top