$RodHat_/search
Rod's Tales

The key was right and sshd refused it without saying why.

Published by

This article may contain Amazon affiliate links. I may earn a commission if you buy through them. Disclosure.

Editorial card: The key was right and sshd refused it without saying why.
Editorial card: RodHat
On this page · 5 sections
  1. What the client tells you, which is nothing
  2. What StrictModes actually checks
  3. Who did it
  4. How to find this faster next time
  5. The rule

The deploy job failed at 06:40 with the line every sysadmin has learned to read as an insult:

deploy@build-02: Permission denied (publickey).

The key was the same key. Same fingerprint in the job’s secret store, same line in ~deploy/.ssh/authorized_keys, which I diffed against the copy in config management with my own eyes and then with cmp, because eyes are liars. Two years of green runs and then this.


What the client tells you, which is nothing

ssh -vvv is where everyone goes first and where this one dead-ends:

debug1: Offering public key: /home/ci/.ssh/id_ed25519 ED25519 SHA256:...
debug3: send packet: type 50
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey

Type 50 is the key offer. Type 51 is SSH_MSG_USERAUTH_FAILURE. The server looked at the key and said no, and the protocol does not carry a reason. That is deliberate: telling an unauthenticated stranger why you rejected them is a gift. So the client has no more to give. Quit staring at it.

The reason is on the server, in the auth log. On FreeBSD that is /var/log/auth.log. On a journald Linux box it is whatever your unit logs to, and I am not going to tell you the command because you know it and I do not enjoy typing it. The line was right there:

sshd: Authentication refused: bad ownership or modes for directory /home/deploy

That is StrictModes, on by default, and it is doing its job.


What StrictModes actually checks

Before sshd trusts authorized_keys, it checks that nobody but the user (and root) could have edited it. It looks at the file, at ~/.ssh, and at the user’s home directory. If any of them is owned by someone else, or is writable by group or other, the key is ignored as if it were not there. The logic is sound: if another account can write to your home directory, it can replace .ssh or rename it, and then it can log in as you. A world-writable path to a login key is a backdoor with extra steps.

So the key was fine. The directory above it had changed. stat on the home directory showed mode 775, group deploy. It had been 755 for two years.


Who did it

A teammate, trying to be helpful, had a script that let two people on the deploy team edit release files under ~deploy/releases/. The script ran chmod -R g+w /home/deploy on the afternoon before. Recursive, from the top, because the path was a variable and the variable was one directory shorter than anybody thought. It made the release tree group-writable, which was the intent, and made the home directory group-writable, which was the accident.

Nothing complained at the time. Existing SSH sessions are unaffected, since StrictModes is evaluated at authentication. Anything holding a connection stayed fine all night. The first fresh login after the chmod was the 06:40 job. That delay is why nobody connected the two events. Same family as the config file that changed and nothing reloaded, just inverted: here the change was real and the effect was held back until the next authentication.


How to find this faster next time

1. Run a second sshd with debug on. Don’t touch the production daemon. Start another one on a spare port in the foreground, as root:

/usr/sbin/sshd -ddd -p 2222

Then connect with ssh -p 2222 deploy@host. It prints the rejection reason on your terminal, including the exact path that failed the check. Kill it when you are done. Debug mode handles one connection and exits anyway.

2. Walk the path yourself. namei -l /home/deploy/.ssh/authorized_keys on Linux prints owner and mode for every component. On FreeBSD, ls -ld on the home directory, .ssh and the file does the same job in three commands. Anything group- or world-writable, or owned by the wrong uid, is your answer. Typical clean values are 755 or tighter on the home directory, 700 on .ssh, 600 on authorized_keys.

3. Fix the mode, not the setting. chmod g-w /home/deploy and the next login works with no restart. The tempting wrong answer is StrictModes no in sshd_config. Do not. That removes a check for the whole machine to excuse one script, and the next group-writable home directory will be a real problem instead of a loud one.

4. Fix the script. The chmod -R target should be the release tree, quoted, with a check that the variable is non-empty and not a prefix of $HOME. If two people need to share a tree, give them a shared group and a dedicated directory outside anyone’s home. The deploy user’s home directory belongs to the deploy user.


The rule

A refusal with no reason is not a mystery, it is a log you have not read yet. The client is the wrong end to debug an authentication failure from, by design. Go to the server, read what it said to itself, and look at the filesystem path before you look at the key. I have a health check story with the same moral about asking the right party the right question, and apparently I enjoy relearning it on Mondays.

More incidents where the error message and the cause live in different places are in the reliability hub.