knowledge

Overview

Linux hardening reduces the attack surface of a system through access control, firewall configuration, service minimization, and log monitoring. While Linux systems present a smaller attack surface than Windows — especially outside of Active Directory environments — misconfigurations in services, permissions, and network exposure still create exploitable vulnerabilities.


Terminology

TermDefinition
HardeningProcess of reducing a system’s attack surface through configuration and restriction
iptablesLinux firewall utility that uses Netfilter to filter, NAT, and mangle network packets
NetfilterLinux kernel framework providing hooks to intercept and modify network traffic
nftablesModern replacement for iptables with improved syntax and performance
UFWUncomplicated Firewall — simplified front-end for iptables
FirewallDDynamic firewall manager with zone and service-based configuration
TCP WrappersHost-based access control tool restricting services by source IP or hostname
fail2banTool that blocks hosts after a configurable number of failed login attempts
SELinuxMandatory Access Control system integrated into the Linux kernel
AppArmorMAC system using application profiles; simpler alternative to SELinux
LynisSecurity auditing tool for Linux systems
rkhunter / chkrootkitRootkit detection tools

Core Concepts

Security Hardening Practices

Linux systems are less prone to mass malware than Windows, but require deliberate hardening — particularly when internet-exposed.

Essential Hardening Steps

PracticeDescription
Keep OS and packages updatedsudo apt update && sudo apt dist-upgrade
Disable unnecessary servicesReduces the number of open attack surfaces
Remove unencrypted authentication mechanismsEnforce SSH keys over passwords
Disable root SSH loginEdit /etc/ssh/sshd_configPermitRootLogin no
Disable password-based SSHPasswordAuthentication no in sshd_config
Enforce least privilegeUsers should only have access they need; restrict sudo to specific commands
Enable fail2banBlock IPs after repeated failed logins
Enable NTP and SyslogAccurate timestamps for logs; ensure logs are running
Enforce unique user accountsNo shared accounts
Enforce strong passwords with agingPrevent reuse; expire passwords periodically
Lock accounts after failuresPrevent brute-force via PAM lockout
Disable unwanted SUID/SGID binariesAudit with find / -perm -4000 and cross-reference GTFOBins (https://gtfobins.github.io/)
Use SELinux or AppArmorAdd MAC layer on top of standard permissions
Run periodic auditsTools like Lynis scan for misconfigurations and vulnerabilities

Security is a continuous process — this list is not exhaustive.


TCP Wrappers

TCP Wrappers provide host-based access control for network services based on IP address or hostname.

Configuration files:

  • /etc/hosts.allow — specifies which services and hosts ARE allowed
  • /etc/hosts.deny — specifies which services and hosts are NOT allowed

Rule precedence: /etc/hosts.allow is checked first. First matching rule wins.

ExampleMeaning
sshd : 10.129.14.0/24Allow SSH from this subnet
ALL : .inlanefreight.comAllow all services from this domain
ftpd : 10.129.22.0/24Allow FTP from this IP range
ALL : ALL (in hosts.deny)Deny everything not explicitly allowed

Firewall Setup

Linux Firewall Ecosystem

ToolDescription
NetfilterKernel framework — base layer; all others build on it
iptablesDirect rule-based interface to Netfilter
nftablesModern replacement for iptables — better syntax, better performance; not compatible with iptables rules
UFWSimplified iptables front-end — see Uncomplicated Firewall (UFW)
FirewallDDynamic firewall with zones and services; supports complex configurations

iptables

iptables provides direct rule-based access to Netfilter using tables, chains, targets, and matches for precise packet filtering, NAT, and packet manipulation — see iptables for full syntax reference.


System Logs and Monitoring

Logs are critical for detecting intrusions, diagnosing issues, and supporting forensic investigations. On Linux, logs are stored as flat files under /var/log/.

Log Types and Locations

Log TypeLocationContents
Kernel/var/log/kern.logKernel events, hardware drivers, system calls — useful for detecting unusual kernel-level activity
System/var/log/syslogSystem-level events: service start/stop, reboots, login attempts
Authentication/var/log/auth.logUser authentication attempts — more detailed than syslog for auth events
fail2ban/var/log/fail2ban.logFailed login attempts and blocked IPs
Apache access/var/log/apache2/access.logWeb server requests
Apache error/var/log/apache2/error.logWeb server errors and application issues
Nginx/var/log/nginx/access.logNginx web server requests
OpenSSH (Ubuntu)/var/log/auth.logSSH login attempts
OpenSSH (RHEL)/var/log/secureSSH login attempts
MySQL/var/log/mysql/mysql.logMySQL database activity
PostgreSQL/var/log/postgresql/postgresql-version-main.logPostgreSQL activity
systemd journal/var/log/journal/Unified binary log for all systemd-managed services

Kernel logs can reveal vulnerable or outdated drivers, suspicious system calls, and early indicators of malware or rootkit activity. Application access logs show who accessed a service, what was requested, and from where.

Log Analysis Tools

CommandPurpose
tail -f /var/log/syslogStream new log entries in real time
grep "Failed" /var/log/auth.logFilter for failed login attempts
journalctl -u ssh.serviceView systemd journal for SSH service
sed, awk, cutParse and extract fields from log entries

Additional monitoring tools: syslog, rsyslog, ss, lsof, ELK stack (Elasticsearch, Logstash, Kibana).



References / Images