To secure the image for GCE deployment, apply these SSH and user settings:
- Root User: Lock the
root account and set an invalid password. - SSH Password Auth: Disable password authentication in
/etc/ssh/sshd_config. - SSH Root Login: Disable root login via SSH.
- SSH Timeouts: Set
ServerAliveInterval and ClientAliveInterval to 420 to prevent disconnections.
Example SSH hardening commands:
sed -i -e '/^PasswordAuthentication /s/ yes$/ no/' /etc/ssh/sshd_config
sed -i -e $'/^ ServerAliveInterval/d' /etc/ssh/ssh_config
sed -i -e $'/^Host \*$/a \ ServerAliveInterval 420' /etc/ssh/ssh_config
sed -i '/ClientAliveInterval/s/^.*/ClientAliveInterval 420/' /etc/ssh/sshd_config
sed -i '/PermitRootLogin yes/s/^.*/PermitRootLogin no/' /etc/ssh/sshd_config
# Disable password authentication by default.
sed -i -e '/^PasswordAuthentication /s/ yes$/ no/' /etc/ssh/sshd_config
# Set ServerAliveInterval and ClientAliveInterval to prevent SSH disconnections.
sed -i -e $'/^ ServerAliveInterval/d' /etc/ssh/ssh_config
sed -i -e $'/^Host \*$/a \ ServerAliveInterval 420' /etc/ssh/ssh_config
sed -i '/ClientAliveInterval/s/^.*/ClientAliveInterval 420/' /etc/ssh/sshd_config
# Disable root login via SSH by default.
sed -i '/PermitRootLogin yes/s/^.*/PermitRootLogin no/' /etc/ssh/sshd_config