Hero Image

Linux Shell SSH/No Root One-Liner

Having a scripts for repeatable system administration saves times and reduces human errors. The following lines can be used in shell scripts (I tend to use bash) or in the cloud-init data of a cloud provider. I tend to have this script and the public key of an SSH key in a public Git repo that I just clone into a new VPS and execute. This should work on modern Debian and Red Hat Linux variants such as Ubuntu 22.02 and CentOS (RIP).

# Force SSH Key Auth and Deny Password Auth
sudo grep -q "^[^#]*PasswordAuthentication" /etc/ssh/sshd_config && sed -i "/^[^#]*PasswordAuthentication[[:space:]]yes/c\PasswordAuthentication no" /etc/ssh/sshd_config || echo "PasswordAuthentication no" >> /etc/ssh/sshd_config
sudo systemctl restart ssh