Server Hardening with Puppet: SSH, Firewall, Users, sudo, Auditing and CIS Benchmarks
Why Harden with Puppet?
A security standard is a long checklist. Doing it by hand on 100 servers means mistakes and drift. With Puppet, each item becomes a resource: it is applied once, checked every 30 minutes, and fixed if someone changes it. Reports show which servers are not compliant.
Plan: A Hardening Profile
puppet
class profile::hardening {
contain profile::hardening::ssh
contain profile::hardening::firewall
contain profile::hardening::accounts
contain profile::hardening::sudo
contain profile::hardening::kernel
contain profile::hardening::services
contain profile::hardening::audit
contain profile::hardening::updates
}
Always test first: Hardening can lock you out (SSH changes, firewall, PAM). Use a lab VM with a console, run with --noop first, and roll out in waves. Keep a second way in (cloud console, out-of-band) until you trust the code.
1. SSH
puppet
class profile::hardening::ssh (
Array[String] $allow_groups = ['ssh-users'],
Integer $port = 22,
) {
file { '/etc/ssh/sshd_config.d/10-hardening.conf':
ensure => file,
owner => 'root',
group => 'root',
mode => '0600',
content => epp('profile/hardening/sshd.conf.epp', {
'port' => $port,
'allow_groups' => $allow_groups,
}),
notify => Service['sshd'],
}
service { 'sshd':
ensure => running,
enable => true,
}
group { $allow_groups: ensure => present }
}
text
<%# templates/hardening/sshd.conf.epp %>
<%- | Integer $port, Array[String] $allow_groups | -%>
# Managed by Puppet
Port <%= $port %>
Protocol 2
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
PermitEmptyPasswords no
X11Forwarding no
MaxAuthTries 4
LoginGraceTime 30
ClientAliveInterval 300
ClientAliveCountMax 2
AllowGroups <%= $allow_groups.join(' ') %>
LogLevel VERBOSE
KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com
MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com
Validate SSH changes: validate_cmd checks a temporary copy of the file, so it works best when you manage the whole sshd_config (validate_cmd => '/usr/sbin/sshd -t -f %'). A drop-in file is not a complete config, so test drop-ins on a lab server first and keep a second session open.
2. Firewall
puppet
# Using firewalld on RHEL family (puppet-firewalld module)
class profile::hardening::firewall (
Array[Stdlib::Port] $open_ports = [22],
Array[String] $trusted_sources = ['10.0.0.0/8'],
) {
class { 'firewalld': }
firewalld_zone { 'public':
ensure => present,
target => 'DROP', # drop everything not allowed
}
$open_ports.each |$port| {
firewalld_port { "allow ${port}/tcp":
ensure => present,
zone => 'public',
port => $port,
protocol => 'tcp',
}
}
}
puppet
# nftables on Debian/Ubuntu (puppet-nftables module)
include nftables
nftables::rule { 'default_in-ssh':
content => 'tcp dport 22 accept',
}
nftables::rule { 'default_in-https':
content => 'tcp dport 443 accept',
}
Do not purge the firewall blindly: Managing all rules (including removal of unknown ones) is the goal, but start by only adding rules, then check with nft list ruleset or firewall-cmd --list-all before turning on purging.
3. Accounts and Passwords
puppet
class profile::hardening::accounts {
# Lock system accounts that should never log in
['games', 'ftp', 'news'].each |$u| {
user { $u:
ensure => present,
shell => '/sbin/nologin',
password => '!',
}
}
# Password aging defaults
file_line { 'pass_max_days': path => '/etc/login.defs', line => 'PASS_MAX_DAYS 90', match => '^PASS_MAX_DAYS' }
file_line { 'pass_min_days': path => '/etc/login.defs', line => 'PASS_MIN_DAYS 1', match => '^PASS_MIN_DAYS' }
file_line { 'pass_warn_age': path => '/etc/login.defs', line => 'PASS_WARN_AGE 14', match => '^PASS_WARN_AGE' }
file_line { 'umask': path => '/etc/login.defs', line => 'UMASK 027', match => '^UMASK' }
# Password quality
file { '/etc/security/pwquality.conf':
ensure => file,
mode => '0644',
content => "minlen = 14\nminclass = 4\nretry = 3\n",
}
}
4. sudo
puppet
class profile::hardening::sudo {
package { 'sudo': ensure => installed }
file { '/etc/sudoers.d/ops-team':
ensure => file,
owner => 'root',
group => 'root',
mode => '0440',
content => "%ops ALL=(ALL) ALL\nDefaults:%ops timestamp_timeout=5\n",
validate_cmd => '/usr/sbin/visudo -c -f %', # never install a broken sudoers file
require => Package['sudo'],
}
file { '/etc/sudoers.d/deploy':
ensure => file,
mode => '0440',
content => "deploy ALL=(root) NOPASSWD: /bin/systemctl restart myapp, /bin/systemctl status myapp\n",
validate_cmd => '/usr/sbin/visudo -c -f %',
}
}
sudoers errors lock out root tasks: A syntax error in sudoers breaks sudo. Always use validate_cmd => 'visudo -c -f %' for any file in /etc/sudoers.d.
5. Kernel and Network Settings
puppet
class profile::hardening::kernel {
$settings = {
'net.ipv4.ip_forward' => 0,
'net.ipv4.conf.all.send_redirects' => 0,
'net.ipv4.conf.all.accept_redirects' => 0,
'net.ipv4.conf.all.accept_source_route'=> 0,
'net.ipv4.conf.all.rp_filter' => 1,
'net.ipv4.icmp_echo_ignore_broadcasts' => 1,
'net.ipv4.tcp_syncookies' => 1,
'kernel.randomize_va_space' => 2,
'fs.suid_dumpable' => 0,
}
$content = $settings.map |$k, $v| { "${k} = ${v}" }.join("\n")
file { '/etc/sysctl.d/90-hardening.conf':
ensure => file,
mode => '0644',
content => "# Managed by Puppet\n${content}\n",
notify => Exec['apply-sysctl'],
}
exec { 'apply-sysctl':
command => '/sbin/sysctl --system',
refreshonly => true,
}
}
6. Remove or Disable Unneeded Services and Packages
puppet
class profile::hardening::services {
package { ['telnet', 'rsh-server', 'talk', 'xinetd', 'tftp-server']:
ensure => absent,
}
service { ['avahi-daemon', 'cups', 'rpcbind']:
ensure => stopped,
enable => false,
}
# Disable USB storage and uncommon filesystems
file { '/etc/modprobe.d/hardening.conf':
ensure => file,
content => "install usb-storage /bin/true\ninstall cramfs /bin/true\ninstall squashfs /bin/true\n",
}
}
7. Important File Permissions
puppet
file { '/etc/passwd': mode => '0644', owner => 'root', group => 'root' }
file { '/etc/shadow': mode => '0000', owner => 'root', group => 'root' }
file { '/etc/gshadow': mode => '0000', owner => 'root', group => 'root' }
file { '/etc/crontab': mode => '0600', owner => 'root', group => 'root' }
file { '/etc/cron.d': ensure => directory, mode => '0700', owner => 'root', group => 'root' }
file { '/boot/grub2/grub.cfg': mode => '0600', owner => 'root', group => 'root' }
8. Auditing and Logging
puppet
class profile::hardening::audit {
package { 'audit': ensure => installed }
file { '/etc/audit/rules.d/hardening.rules':
ensure => file,
mode => '0640',
content => @(RULES),
-w /etc/passwd -p wa -k identity
-w /etc/group -p wa -k identity
-w /etc/shadow -p wa -k identity
-w /etc/sudoers -p wa -k sudo
-w /etc/sudoers.d/ -p wa -k sudo
-w /var/log/lastlog -p wa -k logins
-a always,exit -F arch=b64 -S execve -F euid=0 -k root-commands
| RULES
notify => Service['auditd'],
require => Package['audit'],
}
service { 'auditd':
ensure => running,
enable => true,
hasrestart => true,
restart => '/usr/sbin/service auditd restart', # auditd needs a service command, not systemctl restart
}
}
9. Updates
puppet
# RHEL family: automatic security updates only
package { 'dnf-automatic': ensure => installed }
file_line { 'dnf-automatic-security':
path => '/etc/dnf/automatic.conf',
line => 'upgrade_type = security',
match => '^upgrade_type',
}
service { 'dnf-automatic-install.timer': ensure => running, enable => true }
Use a Ready-Made Baseline
You do not need to translate a whole benchmark by hand. Forge and the community offer modules that implement CIS or DISA STIG baselines. Search the Forge for CIS, STIG or hardening modules and check who maintains them, which OS versions and benchmark versions they cover, and the licence. Also look at Puppet Comply for Puppet Enterprise, which scans against CIS.
| Tool | Idea |
| Hardening module from the Forge | Classes with parameters for each section, enable what you need |
| OpenSCAP + scap-security-guide | Scans systems against profiles and can generate remediation. Many teams scan first, then enforce with Puppet |
| Inspec / Chef InSpec / Goss | Test that servers meet rules after Puppet runs (separate verification) |
| Puppet Enterprise Comply | Compliance scanning in the product (check current availability) |
Measure Compliance with noop
bash
# What would be changed on this server to match our standard?
sudo puppet agent -t --noop
# Fleet-wide: query PuppetDB for resources that report "corrective changes"
curl -s -G http://localhost:8080/pdb/query/v4 --data-urlencode \
'query=events[certname, resource_title, property] { status = "noop" and latest_report? = true }' | jq -r '.[] | [.certname, .resource_title] | @tsv' | head
Corrective vs intentional changes: Puppet reports mark a change as corrective when something drifted back (someone edited a file by hand) and intentional when new code changed it. Corrective changes are a useful security signal.
Practice Tasks
- Apply the SSH hardening template on a lab VM, keep a second session open, and test a login
- Add a sudoers rule with
validate_cmd and test a broken file to see it rejected
- Apply sysctl hardening and check values with
sysctl -a
- Run
puppet agent --noop against the hardening profile and read the proposed changes
- Run an OpenSCAP scan before and after Puppet and compare the results
Conclusion
Treat each hardening rule as a resource and group them in a profile. Validate risky files, test in a lab, roll out in waves, use --noop to report non-compliance, and reuse community baselines instead of writing every rule yourself.
Quick Summary: hardening profile; ssh/firewall/sudo/sysctl/auditd; validate_cmd; test not to lock yourself out; noop = compliance report.