This domain ensures Linux system confidentiality, integrity, and availability. Topics include file permissions, PAM, firewall setup, SELinux/AppArmor, SSH hardening, and log auditing.
Linux systems rely heavily on user and group management for securing access to resources:
getfacl
and
setfacl
.
/etc/shadow
for secure password storage, replacing
plain-text storage in /etc/passwd
.
Pluggable Authentication Modules (PAM) provide a flexible and modular framework for managing authentication on Linux systems. PAM allows administrators to define how users are authenticated, authorized, and managed during login and other system activities.
/etc/pam.d/
directory, making it easy to manage
authentication policies for various services.
pam_unix.so
: Provides standard Unix
authentication using the /etc/passwd
and
/etc/shadow
files.
pam_tally2.so
: Tracks failed login
attempts and can lock accounts after a specified number of failures.
This is useful for mitigating brute-force attacks.
pam_pwquality.so
: Enforces password
complexity requirements, such as minimum length, character classes,
and dictionary checks, to ensure strong passwords.
pam_faillock.so
: Replaces
pam_tally2.so
in modern systems for account lockout
policies. It integrates with systemd
for better logging
and management.
pam_limits.so
: Enforces resource
limits (e.g., maximum number of processes or open files) for users,
as defined in /etc/security/limits.conf
.
PAM configurations are stored in the
/etc/pam.d/
directory, with each file corresponding to a
specific service (e.g., sshd
, login
,
sudo
). Each configuration file contains a stack of rules,
where each rule specifies a module and its behavior.
To enforce password complexity using pam_pwquality.so
,
edit the /etc/pam.d/common-password
file and add the
following line:
password requisite pam_pwquality.so retry=3 minlen=12 dcredit=-1 ucredit=-1 ocredit=-1 lcredit=-1
This configuration enforces the following rules:
minlen=12
).
dcredit=-1
).ucredit=-1
).
ocredit=-1
).
lcredit=-1
).
To lock user accounts after three failed login attempts using
pam_faillock.so
, edit the
/etc/pam.d/common-auth
file and add the following lines:
auth required pam_faillock.so preauth silent deny=3 unlock_time=600 auth [default=die] pam_faillock.so authfail deny=3 unlock_time=600 account required pam_faillock.so
This configuration does the following:
deny=3
).
unlock_time=600
).
pam_faillock.so
module for modern systems
instead of pam_tally2.so
, as it provides better
integration with systemd
.
Securing Linux systems requires a combination of network-level protection and system-level access controls. Host-based firewalls like UFW and Firewalld provide network traffic filtering, while SELinux and AppArmor enforce Mandatory Access Control (MAC) policies to restrict system access.
Firewalls are essential for controlling inbound and outbound network traffic. They allow administrators to define rules that permit or deny traffic based on IP addresses, ports, and protocols.
UFW is a user-friendly firewall management tool for Debian-based
systems. It simplifies the process of configuring
iptables
rules.
sudo ufw enable
sudo ufw allow 22
sudo ufw deny 80
sudo ufw status
Firewalld is a dynamic firewall management tool for Red Hat-based systems. It supports zones, which allow different rules for different network interfaces.
sudo systemctl start firewalld sudo systemctl enable firewalld
sudo firewall-cmd --add-service=https --permanent
sudo firewall-cmd --reload
sudo firewall-cmd --list-all
MAC systems like SELinux and AppArmor enforce strict access controls by confining processes and users to only the resources explicitly allowed by security policies.
SELinux is a powerful MAC system that enforces security policies at the kernel level. It is commonly used in Red Hat-based distributions.
getenforce
sudo setenforce 0
/etc/selinux/config
:
SELINUX=permissive
semanage
:
sudo semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?" sudo restorecon -Rv /web
AppArmor is an alternative to SELinux that uses profiles to restrict the capabilities of individual applications. It is commonly used in Ubuntu-based systems.
sudo aa-status
sudo aa-complain /etc/apparmor.d/usr.sbin.nginx
sudo aa-enforce /etc/apparmor.d/usr.sbin.nginx
Hardening Linux systems is essential to reduce attack surfaces and protect against unauthorized access. This involves implementing advanced security mechanisms such as SELinux, AppArmor, and file integrity monitoring tools.
getenforce
to check the current SELinux mode
(e.g., Enforcing, Permissive, or Disabled).
setenforce
to toggle between Enforcing and
Permissive modes.
semanage
to define
rules for file contexts, ports, and booleans.
Example: To allow an HTTP server to bind to a non-standard port (e.g., 8080), use:
semanage port -a -t http_port_t -p tcp 8080
Learn more about SELinux at the official SELinux project.
aa-status
to check the status of AppArmor and
view loaded profiles.
aa-complain
to set a profile to complain mode,
where violations are logged but not enforced.
aa-enforce
to enforce a profile and restrict
application behavior.
Example: To enforce a profile for the Nginx web server, run:
aa-enforce /etc/apparmor.d/usr.sbin.nginx
Learn more about AppArmor at the Ubuntu AppArmor documentation.
sudo apt install aide
or
sudo yum install aide
).
sudo aide --init
sudo aide --check
Learn more about AIDE at the official AIDE project.
apt
, yum
, or dnf
.
iptables
or
firewalld
).
journalctl
or logwatch
.
Effective log management and auditing are critical for detecting suspicious activities, ensuring compliance with security policies, and maintaining system integrity. Linux provides robust tools and mechanisms for monitoring, analyzing, and auditing system logs.
/var/log/auth.log
: Contains
authentication-related events, such as login attempts, SSH access,
and sudo usage. This is crucial for identifying unauthorized access
attempts.
/var/log/syslog
: Stores general system
messages and logs from various services. It is useful for
troubleshooting system-wide issues.
/var/log/kern.log
: Logs kernel-related
messages, including hardware errors and driver issues.
/var/log/audit/audit.log
: Contains
logs generated by the auditd
service, which tracks
access to sensitive files and monitors policy violations.
auditd
for Advanced Auditing:
The auditd
(Audit Daemon) service is a powerful tool for
tracking system events and monitoring access to critical files. It is
commonly used to meet compliance requirements and detect unauthorized
activities.
auditd
:
sudo apt install auditd
auditd
service:
sudo systemctl start auditd sudo systemctl enable auditd
/etc/passwd
):
sudo auditctl -w /etc/passwd -p wa -k passwd_changes
This rule tracks write (w
) and attribute change
(a
) operations on /etc/passwd
and tags
the events with the key passwd_changes
.
ausearch
:
sudo ausearch -k passwd_changes
aureport
:
sudo aureport -f
journalctl
for Log Analysis:
The journalctl
command is used to query and view logs
managed by systemd
. It provides powerful filtering
options for analyzing logs.
journalctl
journalctl -u sshd
journalctl -b
journalctl -f
/var/log/auth.log
and
/var/log/audit/audit.log
, to detect unauthorized access
attempts.
logrotate
to manage log
file sizes and ensure older logs are archived.
Mastering these commands is essential for efficiently managing Linux security settings:
Linux security is a multi-layered discipline involving access control, authentication management, network protection, system hardening, and auditing. Mastery of these concepts prepares you for real-world Linux administration and is critical for passing the CompTIA Linux+ XK0-005 exam.
Continue expanding your Linux knowledge: Explore Domain 3: Scripting and Automation next!