0

I have configured SSH on my Raspberry Pi 4 to only accept public-private key authentication. I am successfully able to SSH from a Window's 11 machine to the Pi.

First thing that I did was create an RSA public-private key pair in the Pi by keygen.

Then, I wrote the public key to the authorized_keys file by a cat command. In the Window's machine,

I copied (by scp) the private key from Pi and deposited it in the ~/.ssh directory.

Back in the Pi, I disabled password authentication by modifying the /etc/ssh/sshd_config file.

By pointing to the private key, I am now able to SSH into the Pi from Windows PowerShell without password authentication by:

ssh <username>@<IP> -i ~/.ssh/rpi_key

where rpi_key is the private key file.

Now, I want to be able to SSH from a ubuntu machine to the Pi.

I went back into the /etc/ssh/sshd_config file and re-enabled password authentication, but SSH is not working. It gives me a "port 22: connection time out" error. It also gave me a "port 22: Network unreachable" error.

It will also not allow me to SSH from the Pi to the ubuntu machine. I understand that this can be many things, but I suspect that it may be related to the previous changes that I made. Any suggestions as to how I should go about tracking down the problem?

6
  • 1
    Try writing in paragraphs. This is difficult to understand.
    – Milliways
    Commented Jan 9, 2022 at 21:40
  • did you restart the SSH daemon after enabling passwords? Commented Jan 9, 2022 at 22:25
  • @SteveRobillard Yes, I restarted it using "sudo service restart ssh". Commented Jan 10, 2022 at 3:15
  • @SteveRobillard Also tried "sudo servicectl restart sshd". Commented Jan 10, 2022 at 3:32
  • As @Seamus says, you should generate a private key on the client, that is your Windows machine, and you copy the public key fingerprint to your Pi (in authorized_keys) in order to SSH without password. If you also want to log in to the Pi from a Ubuntu machine, you repeat the same steps - it's a different machine with a different key pair. Regarding the other issue with port 22 it looks like a network issue, possibly missing gateway, incompatible IP range or even a firewall preventing outbound connections. Compare the network config on the Ubuntu machine vs the Windows machine.
    – Kate
    Commented Jan 10, 2022 at 7:38

2 Answers 2

1

You have made an error. I can't follow your entire question as it's difficult to read, but I did see one thing:

First thing that I did was create an RSA public-private key pair in the Pi

That's OK for the case when the Pi is a client. keygen is generally run on the client, and then the public key is transferred to the server. Here's a detailed procedure that seems to be maintained.

0

I assume all the devices: the Pi, the Windows computer, and the Ubuntu computer are connected by the same home network. If you are trying to ssh to your Pi from outside your home network, then please update your question.

You are a bit confused

When you create a public private key pair, the private key is for the secret key, and you should never copy it to another computer.

First thing that I did was create an RSA public-private key pair in the Pi by keygen.

This is fine. You should use the one private key to ssh from the your Pi to Windows, Ubuntu and all other computers.

Then, I wrote the public key to the authorized_keys file by a cat command. In the Window's machine,

This is not needed. you should copy the public key to your Windows, Ubuntu, and all other computers you want to ssh from Pi. This is normally done by:

ssh-copy-id -i ~/.ssh/rpi_key.pub [email protected]
ssh-copy-id -i ~/.ssh/rpi_key.pub [email protected]

These two commands will add the public key to the authorized_keys files of the two computers, Windows, and Ubuntu.

Afer this, you should be able to ssh from your Pi to Windows and Ubuntu computers without password by

ssh [email protected]
ssh [email protected]

Note: Ubuntu does not have the ssh server installed and running by default. You will need to install openssh-server in Ubuntu with this command (in Ubuntu):

sudo apt install openssh-server

I copied (by scp) the private key from Pi and deposited it in the ~/.ssh directory.

Sharing the private key with another computer is a security risk.

Since you want to ssh from your Windows computer to the Pi. You should generate a private public key pair in Windows using the same commands as above. Let's call this key windows_key. Don't copy this key to the Pi or to Ubuntu computer. Copy the windows_key.pub to the Pi using the command:

ssh-copy-id -i ~/.ssh/windows_key.pub [email protected]

ssh from Ubuntu to Pi

I went back into the /etc/ssh/sshd_config file and re-enabled password authentication (in Pi)

This is the right thing to do. Make sure you restart the ssh service after every edit with:

sudo service ssh restart

At this point you should be able to ssh from Ubuntu to Pi with:

ssh [email protected]

At this point ssh should ask for password authentication. If you are stuck here, please clarify in your question.

Once you can ssh from Ubuntu to Pi using password, you can generate a new private-public key pair in Ubuntu and send the public key from Ubuntu to Pi using the command:

ssh-copy-id -i ~/.ssh/ubuntu_key.pub [email protected]

After that, you try to ssh again using the command:

ssh [email protected]

This time it should not ask for the Pi password. If this works, you can disable password based authentication again. Remember to restart the sshd server after editing `sshd_config.

If you still have problems use the command:

ssh -vvv [email protected]

from the Ubuntu computer and copy and paste the output in your question. After pasting the output format the output as code using the {_} icon above the edit window.

Hope this helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.