English
How to Harden Ubuntu Server
Quick steps to secure your node.
Thank you for your support and kind messages! It really energizes us to keep creating the best crypto guides. Use cointr.ee to find our donation addresses and share your message.
🙏

🤖
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:
How to install SSH server on Ubuntu
In case you need a SSH client for your operating system, refer to:
How to Connect to an SSH Server from Windows, macOS, or Linux
How-To Geek

🧙♂
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.
🔥
Tip: Do NOT routinely use the root account. Use su or sudo, always.
SSH to your server
Copied!
Create a new user called cardano
1
useradd -m -s /bin/bash cardano
Copied!
Set the password for cardano user
1
passwd cardano
Copied!
Add cardano to the sudo group
1
usermod -aG sudo cardano
Copied!

🔏
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.
ED25519
RSA
1
ssh-keygen -t ed25519
Copied!
1
ssh-keygen -t rsa -b 4096
Copied!
Transfer the public key to your remote node. Update the keyname.
1
ssh-copy-id -i $HOME/.ssh/<keyname>.pub [email protected]
Copied!
Login with your new cardano user
Copied!
Disable root login and password based login. Edit the /etc/ssh/sshd_config file
1
sudo nano /etc/ssh/sshd_config
Copied!
Locate ChallengeResponseAuthentication and update to no
1
ChallengeResponseAuthentication no
Copied!
Locate PasswordAuthentication update to no
1
PasswordAuthentication no
Copied!
Locate PermitRootLogin and update to prohibit-password
1
PermitRootLogin prohibit-password
Copied!
Locate PermitEmptyPasswords and update to no
1
PermitEmptyPasswords no
Copied!
Optional: Locate Port and customize it your random port.
Use a random port # from 1024 thru 49141. Check for possible conflicts.
1
Port <port number>
Copied!
Validate the syntax of your new SSH configuration.
1
sudo sshd -t
Copied!
If no errors with the syntax validation, restart the SSH process.
1
sudo systemctl restart sshd
Copied!
Verify the login still works
Standard SSH Port 22
Custom SSH Port
Copied!
1
ssh [email protected] -p <custom port number>
Copied!
Alternatively, add the -p <port#> flag if you used a custom SSH port.
1
ssh -i <path to your SSH_key_name.pub> [email protected]
Copied!
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:
1
Host cardano-server
2
User cardano
3
HostName <server.public.ip.address>
4
Port <custom port number>
Copied!
This will allow you to log in with ssh cardano-server rather than needing to pass through all ssh parameters explicitly.

🤖
Update your system

It's critically important to keep your system up-to-date with the latest patches to prevent intruders from accessing your system.
1
sudo apt-get update -y && sudo apt-get upgrade -y
2
sudo apt-get autoremove
3
sudo apt-get autoclean
Copied!
Enable automatic updates so you don't have to manually install them.
1
sudo apt-get install unattended-upgrades
2
sudo dpkg-reconfigure -plow unattended-upgrades
Copied!

🧸
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.
1
# To disable the root account, simply use the -l option.
2
sudo passwd -l root
Copied!
1
# If for some valid reason you need to re-enable the account, simply use the -u option.
2
sudo passwd -u root
Copied!

🛠
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).
1
sudo apt install libpam-google-authenticator -y
Copied!
To make SSH use the Google Authenticator PAM module, edit the /etc/pam.d/sshd file:
1
sudo nano /etc/pam.d/sshd
Copied!
Add the follow line:
1
auth required pam_google_authenticator.so
Copied!
Now you need to restart the sshd daemon using:
1
sudo systemctl restart sshd.service
Copied!
Modify /etc/ssh/sshd_config
1
sudo nano /etc/ssh/sshd_config
Copied!
Locate ChallengeResponseAuthentication and update to yes
1
ChallengeResponseAuthentication yes
Copied!
Locate UsePAM and update to yes
1
UsePAM yes
Copied!
Save the file and exit.
Run the google-authenticator command.
1
google-authenticator
Copied!
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.
Note: If you are enabling 2FA on a remote machine that you access over SSH you need to follow steps 2 and 3 of this tutorial to make 2FA 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

There may be a reason for you needing to have that memory space mounted in read/write mode (such as a specific server application like Chrome that requires such access to the shared memory or standard applications like Google Chrome). In this case, use the following line for the fstab file with instructions below.
1
none /run/shm tmpfs rw,noexec,nosuid,nodev 0 0
Copied!
The above line will mount the shared memory with read/write access but without permission to execute programs, change the UID of running programs, or to create block or character devices in the namespace. This a net security improvement over default settings.

Use with caution

With some trial and error, you may discover some applications(like Chrome) do not work with shared memory in read-only mode. For the highest security and if compatible with your applications, it is a worthwhile endeavor to implement this secure shared memory setting.
Source: techrepublic.com
Edit /etc/fstab
1
sudo nano /etc/fstab
Copied!
Insert the following line to the bottom of the file and save/close.
1
tmpfs /run/shm tmpfs ro,noexec,nosuid 0 0
Copied!
Reboot the node in order for changes to take effect.
1
sudo reboot
Copied!

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.
1
sudo apt-get install fail2ban -y
Copied!
Edit a config file that monitors SSH logins.
1
sudo nano /etc/fail2ban/jail.local
Copied!
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.
1
# Exampleignoreip = 192.168.1.0/24 127.0.0.1/8
Copied!
1
[sshd]
2
enabled = true
3
port = <22 or your random port number>
4
filter = sshd
5
logpath = /var/log/auth.log
6
maxretry = 3
7
# whitelisted IP addresses
8
ignoreip = <list of whitelisted IP address, your local daily laptop/pc>
Copied!
Save/close file.
Restart fail2ban for settings to take effect.
1
sudo systemctl restart fail2ban
Copied!

🧱
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 123 UDP for chrony ntp
  • Port 6000 TCP for p2p traffic
  • Port 3000 TCP for Grafana web server (if applicable)
  • Port 9100 tcp for Prometheus node data
  • Port 12798 tcp for Prometheus cardano-node metrics data
1
# By default, deny all incoming and outgoing traffic
2
sudo ufw default deny incoming
3
sudo ufw default allow outgoing
4
# Allow ssh access
5
sudo ufw allow ssh #<port 22 or your random ssh port number>/tcp
6
# Allow cardano-node p2p port
7
sudo ufw allow 6000/tcp
8
# Enable firewall
9
sudo ufw enable
Copied!
1
# Verify status
2
sudo ufw status numbered
Copied!
Do not expose Grafana (port 3000) and Prometheus endpoint (port 9100 and 12798) to the public internet as this invites a new attack surface!
Better idea - SSH tunnel to Grafana server
Setup a SSH tunnel with the following command:
1
ssh -L 3000:localhost:3000 <user>@<your-server-ip-or-dns>
Copied!
Alternatively, If using Putty for SSHing, you can configure the tunnel as follows. Make sure to click "Add" and save your settings.
Now you can access the Grafana server from your local machine's browser by visiting http://localhost:3000
Only open the following ports on nodes behind a network firewall. This is not required if using the above SSH tunnel method.
🔥
It is dangerous to open these ports on a VPS/cloud node.
1
# Allow grafana web server port
2
sudo ufw allow 3000/tcp
3
# Allow prometheus endpoint port
4
sudo ufw allow 9100/tcp
5
# Allow prometheus cardano-node metric data port
6
sudo ufw allow 12798/tcp
Copied!
Confirm the settings are in effect.
1
To Action From
2
-- ------ ----
3
[ 1] 22/tcp ALLOW IN Anywhere
4
[ 2] 3000/tcp ALLOW IN Anywhere
5
[ 3] 6000/tcp ALLOW IN Anywhere
6
[ 4] 22/tcp (v6) ALLOW IN Anywhere (v6)
7
[ 5] 3000/tcp (v6) ALLOW IN Anywhere (v6)
8
[ 6] 6000/tcp (v6) ALLOW IN Anywhere (v6)
Copied!
[ Optional but recommended ] Whitelisting (or permitting connections from a specific IP) can be setup via the following command.
1
sudo ufw allow from <your local daily laptop/pc>
2
# Example
3
# sudo ufw allow from 192.168.50.22
Copied!
🎊
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.
1
sudo ufw allow proto tcp from <RELAY NODE IP> to any port <BLOCK PRODUCER PORT>
2
# Example
3
# sudo ufw allow proto tcp from 18.58.3.31 to any port 6000
Copied!

🧱
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.
1
sudo iptables -I INPUT -p tcp -m tcp --dport <RELAY NODE PORT> --tcp-flags FIN,SYN,RST,ACK SYN -m connlimit --connlimit-above 5 --connlimit-mask 32 --connlimit-saddr -j REJECT --reject-with tcp-reset
Copied!
Set the connection limit high enough so that your internal relay/block producer node topology remains functional.
You can check you current connections with a sorted list. Change the relay node port number, if needed.
1
sudo netstat -enp | grep ":6000" | awk {'print $5'} | cut -d ':' -f 1 | sort | uniq -c | sort
Copied!

🔭
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.
1
netstat -tulpn
Copied!
1
ss -tulpn
Copied!
Congrats on completing the guide.
Did you find our guide useful? Send us a signal with a tip and we'll keep updating it.
It really energizes us to keep creating the best crypto guides.
Any feedback and all pull requests much appreciated.
🌛
Hang out and chat with fellow stake pool operators on Discord @
Hang out and chat with our stake pool community on Telegram @ https://t.me/coincashew

🚀
References

How to Harden your Ubuntu 18.04 Server
Medium
Ubuntu system hardening guide for desktops and servers
Linux Audit
How To Harden OpenSSH on Ubuntu 18.04 | DigitalOcean
DigitalOcean
Configure SSH to use two-factor authentication | Ubuntu
Ubuntu
How to Harden Ubuntu Server 18.04 in 5 Easy Steps
Lifewire
The 50 Best Linux Hardening Security Tips: A Comprehensive Checklist
UbuntuPIT
Last modified 1mo ago