On my home SSH server, which is exposed to the internet, I do four things:
- Lock down SSH access to private keys only (no passwords)
- Use a non-standard port (e.g., 2299)
- Alert via Email whenever anyone successfully logs in
- Log all SSH session activity to files
Disallowing passwords ensures that it's virtually impossible for a random SSH scan to guess your username/password. The few times I've seen servers "hacked" it's usually due to password authentication being enabled. Assuming your users' private keys are not leaked, you can be reasonably sure that only authorized users can login to your server.
To lock down access to private keys, make sure that you have configured /etc/ssh/sshd_config with the following option:
PasswordAuthentication no
Changing the default port makes it less likely that an internet port scanner will find your service and possibly exploit some newly found vulnerability in OpenSSH. They will still find you, but this reduces the likelihood.
To use a non-standard port I have my router act as a NAT gateway and map the port to the internal server port 22. However, you could alter the port in the sshd_config file as well.
To setup alerting and session logging, I use a free tool called SSHLog (https://github.com/sshlog/agent/) -- I'm a contributor
You can configure it to send alerts whenever there's a successful login into the server. I use the following config for email in /etc/sshlog/conf.d/email_alert.yaml:
events:
- event: email_on_login
triggers:
- connection_established
filters:
ignore_existing_logins: True
actions:
- action: send_email
actions:
- action: send_email
plugin: email_action
sender: [email protected]
recipient: [email protected]
subject: 'User {{username}} logged into {{hostname}}'
smtp_server: smtp.mydomain.com
smtp_port: 587
username: 'user'
password: 'pass'
Whenever a user logs in, I get an e-mail. If anyone did login, I can jump into the server and see every command entered by inspecting the logs in /var/log/sshlog/sessions/