2. Harden your server-Coincashew

Quick steps to secure your node.

🤖 Pre-requisites

  • Ubuntu Server or Ubuntu Desktop installed

  • SSH server installed

  • a SSH client or terminal window access

In case you need to install SSH server, refer to:

In case you need a SSH client for your operating system, refer to:

🧙♂ Create a non-root user with sudo privileges

Make a habit of logging to your server using a non-root account. This will prevent the accidental deletion of files if you make a mistake. For instance, the command rm can wipe your entire server if run incorrectly using by a root user.

SSH to your server

Create a new user called cardano

Set the password for cardano user

Add cardano to the sudo group

****🔏 Disable SSH password Authentication and Use SSH Keys only

The basic rules of hardening SSH are:

  • No password for SSH access (use private key)

  • Don't allow root to SSH (the appropriate users should SSH in, then su or sudo)

  • Use sudo for users so commands are logged

  • Log unauthorized login attempts (and consider software to block/ban users who try to access your server too many times, like fail2ban)

  • Lock down SSH to only the ip range your require (if you feel like it)

Create a new SSH key pair on your local machine. Run this on your local machine. You will be asked to type a file name in which to save the key. This will be your keyname.

Your choice of ED25519 or RSA public key algorithm.

Transfer the public key to your remote node. Update the keyname.

Login with your new cardano user

Disable root login and password based login. Edit the /etc/ssh/sshd_config file

Locate ChallengeResponseAuthentication and update to no

Locate PasswordAuthentication update to no

Locate PermitRootLogin and update to no

Locate PermitEmptyPasswords and update to no

Optional: Locate Port and customize it your random port.

Use a random port # from 1024 thru 49141. Check for possible conflicts.

Validate the syntax of your new SSH configuration.

If no errors with the syntax validation, reload the SSH process

Verify the login still works

Alternatively, add the -p <port#> flag if you used a custom SSH port.

Optional: Make logging in easier by updating your local ssh config.

To simplify the ssh command needed to log in to your server, consider updating your local $HOME/.ssh/config file:

This will allow you to log in with ssh cardano-server rather than needing to pass through all ssh parameters explicitly.

****🤖 Update your system

Enable automatic updates so you don't have to manually install them.

🧸 Disable root account

System admins should not frequently log in as root in order to maintain server security. Instead, you can use sudo execute that require low-level privileges.

🛠 Setup Two Factor Authentication for SSH

SSH, the secure shell, is often used to access remote Linux systems. Because we often use it to connect with computers containing important data, it’s recommended to add another security layer. Here comes the two factor authentication (2FA).

To make SSH use the Google Authenticator PAM module, edit the /etc/pam.d/sshd file:

Add the follow line:

Now you need to restart the sshd daemon using:

Modify /etc/ssh/sshd_config

Locate ChallengeResponseAuthentication and update to yes

Locate UsePAM and update to yes

Save the file and exit.

Run the google-authenticator command.

It will ask you a series of questions, here is a recommended configuration:

  • Make tokens “time-base”": yes

  • Update the .google_authenticator file: yes

  • Disallow multiple uses: yes

  • Increase the original generation time limit: no

  • Enable rate-limiting: yes

You may have noticed the giant QR code that appeared during the process, underneath are your emergency scratch codes to be used if you don’t have access to your phone: write them down on paper and keep them in a safe place.

Now, open Google Authenticator on your phone and add your secret key to make two factor authentication work.

🧩 Secure Shared Memory

One of the first things you should do is secure the shared memory used on the system. If you're unaware, shared memory can be used in an attack against a running service. Because of this, secure that portion of system memory.

To learn more about secure shared memory, read this techrepublic.com article.

One exceptional case

Edit /etc/fstab

Insert the following line to the bottom of the file and save/close.

Reboot the node in order for changes to take effect.

****⛓ Install Fail2ban

Fail2ban is an intrusion-prevention system that monitors log files and searches for particular patterns that correspond to a failed login attempt. If a certain number of failed logins are detected from a specific IP address (within a specified amount of time), fail2ban blocks access from that IP address.

Edit a config file that monitors SSH logins.

Add the following lines to the bottom of the file.

🔥 Whitelisting IP address tip: The ignoreip parameter accepts IP addresses, IP ranges or DNS hosts that you can specify to be allowed to connect. This is where you want to specify your local machine, local IP range or local domain, separated by spaces.

Save/close file.

Restart fail2ban for settings to take effect.

****🧱 Configure your Firewall

The standard UFW firewall can be used to control network access to your node.

With any new installation, ufw is disabled by default. Enable it with the following settings.

  • Port 22 (or your random port #) TCP for SSH connection

  • Port 6000 TCP for p2p traffic

  • Port 3000 TCP for Grafana web server (if applicable)

  • Port 9100 tcp for Prometheus export data

  • Port 12798 tcp for Prometheus cardano-node metrics data

Only open the following ports on nodes behind a network firewall.

****🔥 It may be dangerous to open these ports on a VPS/cloud node.

Confirm the settings are in effect.

[ Optional but recommended ] Whitelisting (or permitting connections from a specific IP) can be setup via the following command.

🎊 Port Forwarding Tip: You'll need to forward and open ports to your validator. Verify it's working with https://www.yougetsignal.com/tools/open-ports/ or https://canyouseeme.org/ .

🧱 Extra Hardening Rules for your Block Producer Node

Only your Relay Node(s) should be permitted access to your Block Producer Node.

🧱 Extra Hardening Rules for your Relay Node(s)

In order to protect your Relay Node(s) from a novel "DoS/Syn" attack, Michael Fazio created iptables entry which restricts connections to a given destination port to 5 connections from the same IP.

Replace <RELAY NODE PORT> with your public relay port, replace the 5 with your preferred connection limit.

You can check you current connections with a sorted list. Change the relay node port number, if needed.

🔭 Verify Listening Ports

If you want to maintain a secure server, you should validate the listening network ports every once in a while. This will provide you essential information about your network.

🚀 References

https://gist.github.com/lokhman/cc716d2e2d373dd696b2d9264c0287a3#file-ubuntu-hardening-md

Last updated

Was this helpful?