Securing NetBSD remote access: configuring sshd_config with ed25519 keys, disabling root and password login, SSH tunneling, and brute force protection with fail2ban.

In episode 12 we built the npf firewall guarding the network door. But one door must remain open if we want to manage the server remotely: SSH. In this episode we'll secure SSH and remote access on NetBSD — tuning sshd_config for ed25519 keys, disabling root and password login, using tunneling, and protecting against brute force attacks with fail2ban.
After a NetBSD installation, OpenSSH is already part of the base system and sshd is enabled. From episode 6 we know how to enable it:
sshd=YESStart the service if it isn't running yet:
service sshd startservice sshd status
sshd is running as pid 1234.Now let's lock the gate.
Key-based authentication is far more secure than passwords — and ed25519 is a modern key type that's both secure and fast. On the client side (your machine), create a key pair:
ssh-keygen -t ed25519 -C "arman@nbsd11"Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/arman/.ssh/id_ed25519):The public key (id_ed25519.pub) is what gets registered on the server. Copy it to the NetBSD server:
ssh-copy-id arman@192.168.1.50/usr/bin/ssh-copy-id: INFO: Number of key(s) added: 1If ssh-copy-id isn't available, add it manually:
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "ssh-ed25519 AAAA... arman@nbsd11" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keysTest logging in with the key:
ssh arman@192.168.1.50Enter passphrase for key '/home/arman/.ssh/id_ed25519':
NetBSD/nbsd11 (console) login: Warning
Make sure key-based login works before disabling passwords. If the key fails, you could lock yourself out of your own server. Test from a separate session first before changing the configuration.
The main OpenSSH configuration is in /etc/ssh/sshd_config. Let's secure it line by line:
PermitRootLogin noPasswordAuthentication noAllowUsers arman devopsMaxAuthTries 3
LoginGraceTime 30UseDNS noUseDNS no avoids login delays when the server attempts reverse DNS resolution — combined with a firewall, this makes connections feel much faster.
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
AllowUsers arman devops
MaxAuthTries 3
LoginGraceTime 30
UseDNS noAfter changing, apply the configuration:
sshd -t
service sshd restartsshd -t (no output means valid)Always test with sshd -t before reloading — a single typo can lock you out.
SSH isn't just for logging in; it can also create encrypted tunnels. This is very useful for securely accessing internal services.
Forwarding a local port to a service inside the server's network:
ssh -L 8080:localhost:80 arman@192.168.1.50# Open a browser on the local machine to http://localhost:8080
# and access is forwarded to port 80 on the remote serverCreating a local SOCKS proxy that routes all traffic through the server:
ssh -D 1080 arman@192.168.1.50# Set the browser/proxy to socks5://localhost:1080
# Traffic is encrypted up to the serverEven with passwords disabled, there's still a chance of attacks against other services that use passwords. fail2ban scans logs and blocks IP addresses that fail too many times. Install from pkgsrc:
cd /usr/pkgsrc/security/py-fail2ban
make install cleanThe basic jail configuration for sshd lives in /usr/pkg/etc/fail2ban/jail.local:
[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/authlog
maxretry = 5
bantime = 3600Enable and start the service:
echo 'fail2ban=YES' >> /etc/rc.conf
service fail2ban startfail2ban-client status sshdWith maxretry 5, an IP that fails 5 logins is banned for 1 hour. fail2ban can be integrated with npf to block at the firewall level.
| Rule | Reason |
|---|---|
| ed25519 keys only, no passwords | Passwords can be guessed/stolen |
| Root may not log in via SSH | Force login as a user, then doas/sudo |
Restrict with AllowUsers | Principle of least privilege |
| Use default or custom port | A custom port doesn't add security, but reduces noise |
Keep MaxAuthTries low | Slow down brute force |
| fail2ban active | Block attacking IPs |
In this episode 13, you've secured NetBSD remote access: creating ed25519 keys, tuning sshd_config to disable root and password login, using SSH tunneling, and protecting the server from brute force with fail2ban.
Key takeaways:
sshd_config: PermitRootLogin no, PasswordAuthentication no, AllowUsers, MaxAuthTries, and UseDNS no.sshd -t before reloading.-L/-D) for secure access to internal services.In the next episode, episode 14, we'll secure encrypted communication: TLS, OpenSSL, and certificates — creating keys and CSRs, self-signed certificates, managing trust with ca-certificates, and automating certificates with Let's Encrypt. See you in episode 14!