Information
An SSH private key is one of two files used in SSH public key authentication. In this authentication method, the possession of the private key is proof of identity. Only a private key that corresponds to a public key will be able to authenticate successfully. The private keys need to be stored and handled carefully, and no copies of the private key should be distributed.
If an unauthorized user obtains the private SSH host key file, the host could be impersonated
Solution
Update the access to the private keys being used by the open SSH server.
- Mode 0640, owned by the user root and group owned by the group ssh_keys.
- OR -
- Mode 0600, owned by the user root and group owned by the group root.
Run the following script to update the access on the private keys used by the open SSH server:
#!/usr/bin/env bash
{
l_sshd_cmd="$(readlink -e /usr/sbin/sshd || readlink -e /sbin/sshd)"
l_keygen="$(readlink -e /usr/bin/ssh-keygen || readlink -e /bin/ssh-keygen)"
if grep -Psoq -- '^ssh_keys\b' /etc/group; then
l_group="ssh_keys";l_mode="u-x,g-wx,o-rwx"
else
l_group="root";l_mode="u-x,go-rwx"
fi
while IFS= read -r l_file; do
if "$l_keygen" -lf &>/dev/null "$l_file"; then
chown root:"$l_group" "$l_file"
chmod "$l_mode" "$l_file"
done < <("$l_sshd_cmd" -T | awk '$1=="hostkey" {print $2".pub"}' 2>/dev/null)
}
Impact:
If the private keys used by the openSSH server have the incorrect mode, owner, or group owner the server service may not start.