Fixing “Permission denied (publickey)” after moving /etc/ssh/sshd_config

“Permission denied (publickey)” after moving /etc/ssh/sshd_config

Moving the SSH daemon’s config file is one of those quick‑fixes that turns into a full‑blown headache. The first thing I see in production is the classic “Permission denied (publickey)” on every login attempt. In practice, it’s almost always a permissions or path mis‑match.

1. Verify the new location

# Example: moving to /opt/sshd/sshd_config
sudo mkdir -p /opt/sshd
sudo mv /etc/ssh/sshd_config /opt/sshd/

The daemon still expects /etc/ssh/sshd_config unless you tell it otherwise. I usually edit the systemd unit with a drop‑in:

sudo systemctl edit sshd

Add:

[Service]
ExecStart=
ExecStart=/usr/sbin/sshd -D -f /opt/sshd/sshd_config

Then reload and restart:

sudo systemctl daemon-reload
sudo systemctl restart sshd

2. File ownership and permissions

sshd runs as root, but it refuses to load a config that anyone else can write. The defaults are 600 and root:root.

sudo chown root:root /opt/sshd/sshd_config
sudo chmod 600 /opt/sshd/sshd_config

If the file is world‑writable, sshd silently falls back to the default config, which is usually not what you want.

3. AuthorizedKeysFile path

If you moved the config, chances are you moved the keys directory too. The default is ~/.ssh/authorized_keys, but if you tweaked it, double‑check:

grep -i authorizedkeysfile /opt/sshd/sshd_config

Make sure the directory exists and is properly secured:

sudo mkdir -p /opt/sshd/authorized_keys
sudo chown -R $(whoami):$(whoami) /opt/sshd/authorized_keys
chmod 700 /opt/sshd/authorized_keys
chmod 600 /opt/sshd/authorized_keys/authorized_keys

4. SELinux / AppArmor considerations

On SELinux‑enabled boxes, moving files outside /etc/ssh can trigger denials. Label the new file:

sudo semanage fcontext -a -t sshd_config_t '/opt/sshd/sshd_config'
sudo restorecon -v /opt/sshd/sshd_config

For AppArmor, either switch the profile to complain mode or edit it to allow the new path:

sudo aa-complain /etc/apparmor.d/usr.sbin.sshd
# or edit the profile to permit /opt/sshd/sshd_config

5. Test the configuration

Before you hit reboot, validate the syntax:

sudo sshd -t -f /opt/sshd/sshd_config

A clean exit means the file is parseable. Then try a key‑based login from a client:

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

If you still get “Permission denied (publickey)”, look at the logs:

journalctl -u sshd | tail -n 20

Typical entries:

  • sshd[xxxx]: Authentication refused: bad ownership or modes for file /opt/sshd/sshd_config
  • sshd[xxxx]: Authentication refused: bad ownership or modes for file /opt/sshd/authorized_keys/authorized_keys

These point directly back to the permission checks.

6. Trade‑offs and best practice

  • Keep the config in /etc/ssh – it’s the standard spot and saves you from systemd tweaks and SELinux relabeling.

  • If you must move it – document the change in your CM system, and update any backup or DR scripts to include the new path.

  • Use immutable flags – on systems with chattr, you can lock the file to prevent accidental edits:

    sudo chattr +i /opt/sshd/sshd_config
    

    Remember to drop the flag (chattr -i) when you need to update it.

7. Quick checklist

StepCommandPurpose
Move configsudo mv /etc/ssh/sshd_config /opt/sshd/Relocate file
Update unitsystemctl edit sshdPoint daemon to new file
Ownershipchown root:root /opt/sshd/sshd_configCorrect owner
Permissionschmod 600 /opt/sshd/sshd_configSecure file
SELinux labelsemanage fcontext -a -t sshd_config_t '/opt/sshd/sshd_config'Avoid denials
Validatesshd -t -f /opt/sshd/sshd_configSyntax check
Testssh -i ~/.ssh/id_rsa user@hostVerify login

Follow this flow and the “Permission denied (publickey)” error will vanish after you move sshd_config. Keep your SSH service hardened and compliant with what the system expects.

See also