FreeBSD 13 Base System OpenSSH Server Hardening

> posts > 2022 > Mar

Published: | Modified:

Default sshd configs tend to focus more on compatibility instead security. Therefore hardening should be one of the first things after setup a new system.

I'm using the OpenSSH Daemon which comes with the FreeBSD Base System. When you want to use the openssh-portable port instead, skip Step 3, the rest should be identical.

Step 1) Delete existing host keys, generate new rsa and ed25519 key:

rm /etc/ssh/ssh_host_*
ssh-keygen -q -t rsa -b 4096 -f ssh_host_rsa_key -N ""
ssh-keygen -q -t ed25519 -f ssh_host_ed25519_key -N ""

Step 2) Create new Diffie-Hellman groups and avoid small moduli

ssh-keygen -G moduli-3072.candidates -b 3072
ssh-keygen -T moduli-3072 -f moduli-3072.candidates
mv moduli-3072 /etc/ssh/moduli
rm moduli-3072.candidates

Step 3) Disable DSA and ECDSA host keys, only use RSA and ED25519

sysrc sshd_dsa_enable="NO"
sysrc sshd_ecdsa_enable="NO"
sysrc sshd_ed25519_enable="YES"
sysrc sshd_rsa_enable="YES"

Step 4) Optimize /etc/ssh/sshd_config, improve security, restrict allowed key exchange, cipher and MAC algorithms

# Hardening
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
KexAlgorithms curve25519-sha256,,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha256
HostKeyAlgorithms ssh-ed25519,

# Security
PermitRootLogin no
AuthenticationMethods publickey
ChallengeResponseAuthentication no
UsePAM no
VersionAddendum none
X11Forwarding no
AuthorizedKeysFile .ssh/authorized_keys
Subsystem sftp /usr/libexec/sftp-server

Run service sshd restart to apply the new settings, to verify the results of your hardening, you can use the CLI Tool ssh-audit which is also available as Online Version.

Following a Custom Policy for ssh-audit based on the above recommendations:

name = "Custom Policy - FreeBSD 13 Base System OpenSSH Daemon (2022/03/17)"
version = 1
dh_modulus_size_diffie-hellman-group-exchange-sha256 = 2048
host keys = ssh-ed25519
key exchanges = curve25519-sha256,, diffie-hellman-group16-sha512, diffie-hellman-group18-sha512, diffie-hellman-group-exchange-sha256
ciphers =,,, aes256-ctr, aes192-ctr, aes128-ctr
macs =,,

Copy the policy into a file and run ssh-audit -P=<policy.txt> <servername>. The result should be similar to following example without warnings or errors:

# general
(gen) banner: SSH-2.0-OpenSSH_8.8
(gen) software: OpenSSH 8.8
(gen) compatibility: OpenSSH 7.4+, Dropbear SSH 2018.76+
(gen) compression: enabled (

# key exchange algorithms
(kex) curve25519-sha256                     -- [info] available since OpenSSH 7.4, Dropbear SSH 2018.76
(kex)          -- [info] available since OpenSSH 6.5, Dropbear SSH 2013.62
(kex) diffie-hellman-group16-sha512         -- [info] available since OpenSSH 7.3, Dropbear SSH 2016.73
(kex) diffie-hellman-group18-sha512         -- [info] available since OpenSSH 7.3
(kex) diffie-hellman-group-exchange-sha256 (2048-bit) -- [info] available since OpenSSH 4.4

# host-key algorithms
(key) ssh-ed25519                           -- [info] available since OpenSSH 6.5

# encryption algorithms (ciphers)
(enc)         -- [info] available since OpenSSH 6.5
                                            `- [info] default cipher since OpenSSH 6.9.
(enc)                -- [info] available since OpenSSH 6.2
(enc)                -- [info] available since OpenSSH 6.2
(enc) aes256-ctr                            -- [info] available since OpenSSH 3.7, Dropbear SSH 0.52
(enc) aes192-ctr                            -- [info] available since OpenSSH 3.7
(enc) aes128-ctr                            -- [info] available since OpenSSH 3.7, Dropbear SSH 0.52

# message authentication code algorithms
(mac)         -- [info] available since OpenSSH 6.2
(mac)         -- [info] available since OpenSSH 6.2
(mac)              -- [info] available since OpenSSH 6.2

# fingerprints
(fin) ssh-ed25519: SHA256:uN9Oton+VmLL793KirVFB+ilD3Bndra4I/3yFntgX8k

# algorithm recommendations (for OpenSSH 8.8)
(rec) +diffie-hellman-group14-sha256        -- kex algorithm to append
(rec) +rsa-sha2-256                         -- key algorithm to append
(rec) +rsa-sha2-512                         -- key algorithm to append


[ Show Source | Download PDF ]