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
Run the following script to set mode, ownership, and group on the private SSH host key files:
#!/usr/bin/env bash
{
l_skgn="ssh_keys" # Group designated to own openSSH keys
l_skgid="$(awk -F: '($1 == "'"$l_skgn"'"){print $3}' /etc/group)"
[ -n "$l_skgid" ] && l_cga="$l_skgn" || l_cga="root"
awk '{print}' <<< "$(find -L /etc/ssh -xdev -type f -exec stat -L -c "%n %#a %U %G %g" {} +)" | (while read -r l_file l_mode l_owner l_group l_gid; do
if file "$l_file" | grep -Pq ':h+OpenSSHh+privateh+keyb'; then
[ "$l_gid" = "$l_skgid" ] && l_pmask="0137" || l_pmask="0177"
l_maxperm="$( printf '%o' $(( 0777 & ~$l_pmask )) )"
if [ $(( $l_mode & $l_pmask )) -gt 0 ]; then
echo -e " - File: \"$l_file\" is mode \"$l_mode\" changing to mode: \"$l_maxperm\""
if [ -n "$l_skgid" ]; then
chmod u-x,g-wx,o-rwx "$l_file"
else
chmod u-x,go-rwx "$l_file"
fi
fi
if [ "$l_owner" != "root" ]; then
echo -e " - File: \"$l_file\" is owned by: \"$l_owner\" changing owner to \"root\""
chown root "$l_file"
fi
if [ "$l_group" != "root" ] && [ "$l_gid" != "$l_skgid" ]; then
echo -e " - File: \"$l_file\" is owned by group \"$l_group\" should belong to group \"$l_cga\""
chgrp "$l_cga" "$l_file"
fi
fi
done
)
}