Software & AppsOperating SystemLinux

Fixing SSH “Permission Denied (Public key)” Error in Ubuntu and Fedora

Ubuntu 3

In this article, we’ll explore how to fix the “Permission Denied (Public key)” error that you might encounter while trying to establish SSH communication between Ubuntu and Fedora systems. This error typically occurs when the SSH server refuses the public key during the authentication process. We’ll walk you through several solutions, step by step, to help you resolve this issue.

Understanding the Error

Before we dive into the solutions, it’s important to understand what this error means. SSH, or Secure Shell, is a protocol used to securely log onto remote systems. It uses public key cryptography to authenticate the remote computer and allow the remote computer to authenticate the user, if necessary.

When you see the “Permission Denied (Public key)” error, it means that the SSH server is not accepting the public key provided by the client. This could be due to several reasons such as incorrect permissions, incorrect public key, or the server might be configured to not accept public keys at all.

Solution 1: Enable Password Authentication

By default, Fedora is set up to allow only public key authentication. So, if you’re trying to connect using a password, you’ll encounter the error. To enable password authentication, you need to modify the SSH configuration file on the Fedora machine.

  1. Open the SSH configuration file with a text editor. Here, we’re using nano.
sudo nano /etc/ssh/sshd_config
  1. Find the line that says PasswordAuthentication and change it to yes.
PasswordAuthentication yes
  1. Save and close the file. If you’re using nano, you can do this by pressing Ctrl + X, then Y to confirm the changes, and finally Enter to exit.
  2. Restart the SSH service to apply the changes.
sudo systemctl restart sshd

Now, you should be able to log in using a password instead of a public key.

Solution 2: Add Public Key to Authorized Keys

Another solution is to add the public key from the Ubuntu machine to the authorized_keys file on the Fedora machine. Here’s how to do it:

  1. On the Ubuntu machine, open the public key file with a text editor. We’ll use nano for this example.
nano ~/.ssh/id_rsa.pub
  1. Copy the contents of this file.
  2. On the Fedora machine, append the copied public key to the authorized_keys file.
echo "copied_public_key" >> ~/.ssh/authorized_keys

Replace “copied_public_key” with the actual public key you copied from the Ubuntu machine. This will allow the Ubuntu machine to authenticate using the public key.

Solution 3: Use ssh-copy-id Command

If you have password authentication enabled on the Fedora machine, you can use the ssh-copy-id command on the Ubuntu machine to install the public key. This command copies the local host’s public key to the remote host’s authorized_keys file.

ssh-copy-id username@fedora_machine

Replace “username” with your actual username and “fedora_machine” with the actual hostname or IP address of the Fedora machine.

Additional Checks

If none of the above solutions work, here are a few additional things you can check:

  • SSH Service Status: Ensure that the SSH service is running on both machines. You can check this by running systemctl status sshd.
  • Firewall Settings: Check the firewall settings on both machines to ensure that port 22 (default port for SSH) is open for SSH connections.
  • File Permissions: Double-check the permissions of the ~/.ssh directory and the authorized_keys file on the Fedora machine. The directory should have permissions 700 (drwx——) and the file should have permissions 600 (-rw——-). You can change the permissions using the chmod command.

Conclusion

Fixing the “Permission Denied (Public key)” error in SSH involves checking several factors, from password authentication settings to public key settings. We hope this guide has helped you resolve the issue. If you’re still encountering problems, consider checking the SSH logs on the Fedora machine (/var/log/auth.log) for specific error messages that can help identify the issue.

What is SSH?

SSH stands for Secure Shell and is a protocol used to securely log onto remote systems. It provides authentication and encrypted communication between two computers over an insecure network.

Why am I getting the “Permission Denied (Public key)” error?

The "Permission Denied (Public key)" error occurs when the SSH server refuses the public key provided by the client during the authentication process. This can be due to incorrect permissions, an incorrect public key, or the server being configured to not accept public keys.

How can I enable password authentication in Fedora?

To enable password authentication in Fedora, you need to modify the SSH configuration file. Open the file /etc/ssh/sshd_config with a text editor, find the line that says PasswordAuthentication and change it to yes, save the file, and restart the SSH service using the command sudo systemctl restart sshd.

How can I add a public key to the authorized keys file?

To add a public key to the authorized keys file, open the public key file on the source machine, copy its contents, then on the destination machine, append the copied public key to the ~/.ssh/authorized_keys file using the command echo "copied_public_key" >> ~/.ssh/authorized_keys. Replace "copied_public_key" with the actual public key you copied.

How can I use the `ssh-copy-id` command?

The ssh-copy-id command is used to install a public key on a remote machine. You can use it by running ssh-copy-id username@remote_machine. Replace "username" with your actual username and "remote_machine" with the hostname or IP address of the remote machine.

Where can I find SSH logs on the Fedora machine?

The SSH logs on the Fedora machine can be found in the file /var/log/auth.log. Checking this log file can provide specific error messages that can help identify the issue if you’re still encountering problems.

Leave a Comment

Your email address will not be published. Required fields are marked *