Some say that the good ol’ Linux and Unix (e.g. FreeBSD, OpenBSD, etc.) servers we used to connect to are out of style and that the way to go is running serverless workloads on cloud providers. This is definitely not the case for quite a lot of scenarios though. There are many valid reasons why it makes sense to own or rent a bare metal dedicated server. From running predictable workloads (e.g. development and staging environments or B2B back-office dashboards) to maintaining a legacy system that needs to stay on-premise.
In all of these cases you should take care of a few security best pratices when connecting to your servers via SSH — the de facto protocol for remote access.
SSH connections should not be accessible from anywhere in the world. SSH connections should be allowed only from an IP range that is controlled and belongs to a private network. This means that in order for anyone to connect to your server, they need to enter the private network first. This can be achieved either configuring the operating system of the server (e.g., with iptables or ufw) or by configuring the firewall rules on the network interface of the server on your hosting provider.
iptables
ufw
A great and easy way to allow access through the internet when needed, while maintaining the requirement for private network access is to set up a “bastion” server. It will only work as an intermmediate SSH server, accessible via the internet itself, but with no data on it.
Since the bastion server has no keys on it, you should set up SSH key forwarding so that it can forward the SSH keys from your local computer to the server you would like to eventually connect to. This can be achieved in two steps:
~/.ssh/config
Host bastion-00 HostName 192.168.0.11 User logic ForwardAgent yes Host swarm-11 HostName 192.168.0.42 User logic ForwardAgent yes ProxyCommand ssh -W %h:%p bastion-00
ssh-agent ssh-add -k
Now you can connect to the server swarm-11 via bastion-00, without storing any private keys on the bastion server by just running the following command in your terminal:
swarm-11
bastion-00
ssh swarm-11
Now, although we are not storing any private keys on any of those server, if your SSH public key (stored by default in ~/.ssh/id_rsa.pub) is not stored in your server by your hosting provider, you should copy it key to both of those servers, before you connect for the first time. This can be achieved by running the following command in your terminal:
~/.ssh/id_rsa.pub
ssh-copy-id bastion-00 ssh-copy-id swarm-1
This part will ask for password authentication.
Connecting to your SSH server should only take place via key authentication. Password authentication, if enabled and allowed by the hosting provider, should be disabled as soon as you set up a successful SSH connection via key authentication.
To disallow password authentication, edit the SSH daemon configuration (by default stored in /etc/ssh/sshd_config), look for the line with the PasswordAuthentication setting and set its value to no:
/etc/ssh/sshd_config
PasswordAuthentication
no
PasswordAuthentication no
Now, reload your SSH server. On environments using systemd (e.g. Ubuntu) you can do this by running:
systemctl restart ssh
root
SSH access should only be allowed to non-root users. Privilege escalation to the root user should only happen using su or sudo, by the user connected via SSH, requiring authentication via a strong password.
su
sudo
To disallow root login via SSH, update the PermitRootLogin SSH daemon setting to no:
PermitRootLogin
PermitRootLogin No
If this sounds important, yet a bit "too much" given that you are busy developing your business, we would love to help.
Contact us here to learn how we can help keep your infrastructure nice and neat.
Get notified when an article lands on the LOGIC blog.