SFTP chroot user gets "Permission denied (publickey)" with the right key
sshd checks keys before the chroot, in the real home folder from the account entry. Keys copied into the chroot are never read, and a group-writable home fails.
For a user locked into a folder with ChrootDirectory, the key check happens before the chroot. The sshd_config manual says the chroot is applied after authentication. So sshd reads authorized_keys from the server's real file system, at the path in AuthorizedKeysFile, which by default is .ssh/authorized_keys in the home folder from the account entry. The same ownership rules apply as for any key login. Most failures are one of the cases below.
Password logins for the same user may work, which makes the key look like the problem. It usually is not.
1. The key is inside the chroot, not in the home folder
A common setup puts the user in /srv/sftp/user1 and the key in /srv/sftp/user1/.ssh/authorized_keys. If the account's home is /home/user1, sshd never looks there. Check the home folder and the key path sshd will use:
getent passwd user1
sudo sshd -T -C user=user1,host=client.example.com,addr=192.0.2.10 | grep -i authorizedkeysfile
A missing file is logged only at debug level, so the normal log shows nothing about it. Either move the key to the real home, or point AuthorizedKeysFile at a fixed path, such as /etc/ssh/authorized_keys/%u (%u is the user name).
2. A folder on the way is group-writable
With StrictModes yes, the default, the authorized_keys file and every folder above it, up to the home folder, must be owned by the user or by root, and none may be writable by group or others. If the file is outside the home folder, the check goes all the way up to /.
Chroot setups often give the home folder a shared group with write access, such as owner root, group sftponly, mode 775. That fails. The server log shows:
Authentication refused: bad ownership or modes for directory /home/user1
Remove the group write bit. Root ownership itself is fine for this check:
sudo chmod 755 /home/user1
namei -l /home/user1/.ssh/authorized_keys
namei -l lists the owner and mode of each folder on the path. A file that others can write gives "bad ownership or modes for file" instead.
3. The user cannot read the file
sshd opens authorized_keys with the user's own permissions. A file owned by root with mode 600 passes the ownership check but cannot be opened. The log says:
Could not open user 'user1' authorized keys '/home/user1/.ssh/authorized_keys': Permission denied
Make the file readable by the user, for example owner user1 and mode 600, and the .ssh folder owner user1 with mode 700. The user also needs search permission on every folder above it.
What a working layout looks like
/home/user1 root:root 755
/home/user1/.ssh user1:user1 700
/home/user1/.ssh/authorized_keys user1:user1 600
/srv/sftp/user1 root:root 755 (the chroot)
/srv/sftp/user1/upload user1:user1 755
The chroot path has stricter rules of its own: every part must be owned by root. Those are checked after login, whatever StrictModes says. See SFTP chroot: bad ownership or modes for chroot directory.
Setting StrictModes no makes the key work, but it also stops sshd from noticing when someone else can change the key file. Fix the modes instead.
Still refused
Check the reasons that apply to every key login: the wrong key offered, a key type the server does not accept, or a locked account. See Authorized keys and permissions and SSH error messages, what each one means.
Related
- SFTP chroot: bad ownership or modes for chroot directory
- Authorized keys and permissions
- SFTP
- SSH error messages, what each one means
- SSH "This account is currently not available" (nologin shell)