cmc/cleberg.net
My personal web garden & blog.
clone: git clone https://gitbay.org/cmc/cleberg.net.git
main: content/blog/2023-10-15-alpine-ssh-hardening.org · raw
1#+date: [2023-10-15 Sun 00:00:00]
2#+title: Alpine Linux SSH Hardening
3#+description: Hardening SSH on Alpine Linux with config changes and key-only auth.
4#+slug: alpine-ssh-hardening
5#+filetags: :linux:security:
6
7* Overview
8
9This guide follows the standard
10[[https://www.ssh-audit.com/hardening_guides.html][ssh-audit]] hardening
11guide, tweaked for Alpine Linux.
12
13* Hardening Guide
14
15These steps must be performed as root. You can try to use =doas= or
16=sudo=, but there may be issues.
17
181. Re-generate the RSA and ED25519 keys
19
20#+begin_src sh
21rm /etc/ssh/ssh_host_*
22ssh-keygen -t rsa -b 4096 -f /etc/ssh/ssh_host_rsa_key -N ""
23ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""
24#+end_src
25
262. [@2] Remove small Diffie-Hellman moduli
27
28#+begin_src sh
29awk '$5 >= 3071' /etc/ssh/moduli > /etc/ssh/moduli.safe
30mv /etc/ssh/moduli.safe /etc/ssh/moduli
31#+end_src
32
333. [@3] Enable the RSA and ED25519 HostKey directives in the
34 /etc/ssh/sshd_{config} file
35
36#+begin_src sh
37sed -i 's/^\#HostKey \/etc\/ssh\/ssh_host_\(rsa\|ed25519\)_key$/HostKey \/etc\/ssh\/ssh_host_\1_key/g' /etc/ssh/sshd_config
38#+end_src
39
404. [@4] Restrict supported key exchange, cipher, and MAC algorithms
41
42#+begin_src sh
43echo -e "\n# Restrict key exchange, cipher, and MAC algorithms, as per sshaudit.com\n# hardening guide.\nKexAlgorithms sntrup761x25519-sha512@openssh.com,curve25519-sha256,curve25519-sha256@libssh.org,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha256\nCiphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr\nMACs hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,umac-128-etm@openssh.com\nHostKeyAlgorithms ssh-ed25519,ssh-ed25519-cert-v01@openssh.com,sk-ssh-ed25519@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,rsa-sha2-512,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256,rsa-sha2-256-cert-v01@openssh.com" > /etc/ssh/sshd_config.d/ssh-audit_hardening.conf
44#+end_src
45
465. [@5] Include the /etc/ssh/sshd_{config}.d directory
47
48#+begin_src sh
49echo -e "Include /etc/ssh/sshd_config.d/*.conf" > /etc/ssh/sshd_config
50#+end_src
51
526. [@6] Restart OpenSSH server
53
54#+begin_src sh
55rc-service sshd restart
56#+end_src
57
58* Testing SSH
59
60You can test the results with the =ssh-audit= python script.
61
62#+begin_src sh
63pip3 install ssh-audit
64ssh-audit localhost
65#+end_src
66
67If everything succeeded, the results will show as all green. If anything
68is yellow, orange, or red, you may need to tweak additional settings.