I am having troubles setting an ssh connection between two laptops of mine. I tried different solutions posted on-line, but nothing worked. Since I am pretty new with SSH, I might be missing something important. I am using Ubuntu 14.04 LTS on the client, and Ubuntu 16.04 LTS on the server.

Here are the steps that I followed:

On the client:

  • Specified host configuration options in ~/.ssh/config:

    Host [hostname]

      User [username]
      Hostname [IP address of host]
      ServerAliveInterval 10
    
  • Generated RSA key by running:

ssh-keygen -t rsa -b 4096 -o -a 100

  • I supplied a password to ssh-keygen. Private key was saved in ~/.ssh/id_rsa, whereas public key was saved in ~/.ssh/id_rsa.pub

  • I manually copied ~/.ssh/id_rsa.pub to a USB key.

  • At this point, file modes are as follows:

In ~/.ssh:

   -rw-rw-r--    config
   -rw-------    id_rsa
   -rw-r--r--    id_rsa.pub

On the server:

  • installed openssh-server;

  • created a new file ~/.ssh/authorized_keys by doing as follows

cat /media/daniele/disk/id_rsa.pub >> ~/.ssh/authorized_keys

  • set file mode of ~/.ssh/authorized_keys to

-rw-rw-r--

  • manually edited /etc/ssh/sshd_config to have

in /etc/ssh/sshd_config

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile     %h/.ssh/authorized_keys
PasswordAuthentication yes

Finally, on the client, when I try:

ssh [username]@[hostname]

the server asks for the password

[username]@[hostname]'s password:

but, even if I enter the correct one, the server does not accept it:

Permission denied, please try again

and, after three attempts, it closes the connection. Please find here a more descriptive output I get by using

ssh -v -v -v [username]@[hostname]

Any help would be greatly appreciated.

Thank you very much for your time

  • 2
    IIRC the authorized_keys file should be -rw------- (octal mode 600) and the ~/.ssh directories themselves should be drwx------ (octal mode 700) on both client and server – steeldriver Aug 1 '17 at 14:04
  • Thank you @steeldriver, but that did not work – Daniele Prada Aug 1 '17 at 14:28
  • Just to be clear, are you entering your Unix password for the remote account, or the passphrase that you entered when generating the keypair? Is your home directory on the remote host encrypted? – steeldriver Aug 1 '17 at 14:42
  • Good point. I am entering the passphrase entered when generating the keypair. Concerning my home directory on the remote host, I did: ls -a /home and found no .encryptfs folder, so I assume it is not encrypted. Am I correct? Sorry for the stupid question, but am I supposed to run some combination of adduser/passwd on the host? – Daniele Prada Aug 1 '17 at 14:54
  • If that helps, entering any other random combination of characters, always returns Permission denied, please try again. I just tried – Daniele Prada Aug 1 '17 at 15:08

Instead of using the hostname of the server, try using the ip-adres.
I ran into the same problem when setting up my server and this seemed to resolve the problem.

ssh [username]@[host_ip-adres]

If you want to use the hostname you might need to set up a dns-server.
but you can do without.

  • Thank you @Bjorn, but it did not work – Daniele Prada Aug 1 '17 at 14:57
  • the authorized keys are user specific, if you did not make the directory in the home folder of the user you want to remotely connect to the rsa key will not be found – Bjorn Aug 1 '17 at 15:37
  • That makes sense, thanks @Bjorn – Daniele Prada Aug 1 '17 at 15:41

When you define Host with Host [hostname] you have to use ssh [hostname]

Permission is denieed because you're not actually using the keyfile when ssh [user]@[hostname]

You should decide whether you want connection authentication by key/id or by password then focus on that to find the issue. I recommend using key/id since when managed correctly, it is the more secure method.

SSH will always fall back to asking for a password even in cases where it will never work. To avoid this use -o 'batchmode yes' (these quotes are needed) on the client ssh command making the connection. Then if the key cannot be accepted, it bypasses the password prompt (this is normally for use of ssh in a script that needs to avoid getting stuck at the password prompt). SSH will only try 3 keys at one time, so if the valid key is the 4th, it will not be tried and the connection will be aborted. It is best to try only one key per connection attempt if you are certain that it is the correct one. Then you can repeat the connection attempts while making changes at the server. It will help to have log file information from the server. Do grep sshd /var/log/auth.log to see what sshd is trying to tell you.

  • set all file modes to the most secure, 0600 (-rw-------) for regular files, 0700 (drwx------) for directories. you have no reason to allow anyone else to read these. – Skaperen Jul 11 at 0:58

If you didn't load your private key (the one without .pub) into the ssh-agent then you need to specify it in your ssh connection.

SSH -i ~/.ssh/id_rsa user@host

It looks like it isn't asking you for your ssh key pass, but your user pass which is why your password is failing

New contributor
SteamerJ is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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