Based on the Linux Master 1 exam syllabusReport an error
hosts.allow first → if it matches, allow and stop; otherwise hosts.deny → if neither matches, allow by default.enforcing/permissive/disabled, the context is user:role:type:level, and the type is the key. You cannot go to disabled with setenforce./var/log. wtmp, btmp, and lastlog are binary, so you read them with last, lastb, and lastlog. rsyslog.conf records that level and above using the facility.priority pair.fail2ban watches logs and automatically blocks failing IPs, but its settings go in jail.local.libwrap library (tcpd).| File | Role |
|---|---|
/etc/hosts.allow | List of hosts to allow access |
/etc/hosts.deny | List of hosts to deny access |
Check order (memorize absolutely): ① Check /etc/hosts.allow first → if it matches, allow and stop. ② If not found, check /etc/hosts.deny → if it matches, deny. ③ If it is in neither, allow by default. In other words, allow takes priority over deny, and if neither applies, the connection passes.
service : host.# /etc/hosts.deny — deny everything by default
ALL : ALL
# /etc/hosts.allow — allow only specific ones
sshd : 192.168.1.0/24
sshd : 192.168.1.100
vsftpd : .example.com
ALL : 127.0.0.1| Keyword | Meaning |
|---|---|
ALL | All services or all hosts |
LOCAL | Local host names without a dot (.) |
.example.com | All hosts in the example.com domain |
192.168.1. | That network range |
EXCEPT | Specify an exception (e.g., ALL EXCEPT 192.168.1.5) |
⚠️ Trap: if you block everything by putting
ALL : ALLin hosts.deny and forget to allow yourself in hosts.allow, you get blocked even though you came in over SSH (the same trap as with firewalls). Also, TCP Wrapper only applies to services that uselibwrap(sshd, vsftpd, etc.) — it is not a universal tool that blocks every service.
| Mode | Behavior |
|---|---|
enforcing | Enforces the policy. On a violation, blocks + records a log |
permissive | Does not enforce the policy; only logs violations (for debugging) |
disabled | SELinux is completely disabled |
# Check the current mode
getenforce # prints Enforcing / Permissive / Disabled
sestatus # detailed status
# Temporary mode change (reverts on reboot)
setenforce 1 # Enforcing
setenforce 0 # Permissive
# For a permanent change, edit the configuration file
vi /etc/selinux/config
# SELINUX=enforcing ← set this value to enforcing/permissive/disabledTemporary vs permanent: setenforce only switches between enforcing↔permissive temporarily, and after a reboot it returns to the value in /etc/selinux/config. To go to disabled or make a permanent change, you must edit the /etc/selinux/config file and reboot. The trap is that you cannot go to disabled with setenforce.
ls -Z (files) and ps -Z (processes).# Show a file's SELinux context (-Z)
ls -Z /var/www/html
# Example output: system_u:object_r:httpd_sys_content_t:s0
# └user┘ └role┘ └──── type ────┘ └level┘user:role:type:level, and of these the type is the most important in actual access control.httpd_t type and can only serve files of the httpd_sys_content_t type.# Change a file's context (type)
chcon -t httpd_sys_content_t /srv/web/index.html
# Restore the context to the policy default
restorecon -Rv /var/www/html
# Check/change SELinux booleans (on/off switches)
getsebool -a
setsebool -P httpd_can_network_connect onchcon vs restorecon: chcon changes the context directly, but it is temporary and can disappear on restorecon or a filesystem relabel. For permanence, the standard way is to register a rule in the policy with semanage fcontext and then apply it with restorecon. restorecon returns the context to the one the policy defines as "what it should originally be".
If "the permissions (chmod) are definitely right but the service cannot access the file", suspect the SELinux context. If switching temporarily to setenforce 0 (permissive) makes the problem go away, the cause is SELinux — but in production, do not turn it off; fix the context correctly.
/var/log.| Log file | Contents |
|---|---|
/var/log/messages | General system messages (RHEL family) |
/var/log/syslog | General system messages (Debian family) |
/var/log/secure | Authentication/security related (SSH logins, etc., RHEL) |
/var/log/auth.log | Authentication related (Debian) |
/var/log/maillog | Mail server log |
/var/log/cron | cron job log |
/var/log/boot.log | Boot messages |
/var/log/dmesg | Kernel ring buffer (hardware at boot) |
/var/log/wtmp | Login records (binary, viewed with the last command) |
/var/log/btmp | Failed login records (viewed with lastb) |
/var/log/lastlog | Last login per user (viewed with lastlog) |
⚠️ Beware of binary logs:
wtmp,btmp, andlastlogare binary files, not text. Opening them withcatlooks garbled, and each must be read with its dedicated command:last,lastb,lastlog. It is an easy trap to fall into.
Where and how logs are recorded is decided in /etc/rsyslog.conf.
The core syntax takes the form facility.priority action.
facility (service category): the type of source that produced the log. Examples: auth (authentication), authpriv (security authentication), cron, mail, kern (kernel), daemon, user, local0 to local7 (user-defined)
priority (severity): the importance of the message. From low to high: debug < info < notice < warning < err < crit < alert < emerg
# /etc/rsyslog.conf example
# facility.priority target
authpriv.* /var/log/secure
mail.* -/var/log/maillog
cron.* /var/log/cron
*.info;mail.none;authpriv.none /var/log/messages
*.emerg :omusrmsg:*
kern.* /var/log/kern.logmail.err catches err, crit, alert, and emerg.mail.* means all levels, and mail.none means exclude that facility.Key interpretation: *.info;mail.none;authpriv.none /var/log/messages means "record info and above of all facilities in messages, except mail and authpriv". If you add the = sign (e.g., mail.=info), it catches exactly that level only. You need this facility.priority pair interpretation on the practical exam.
# Apply after editing rsyslog.conf
systemctl restart rsyslog
# Generate a log entry yourself to test
logger -p authpriv.notice "test message"logrotate periodically cuts (rotates) log files and keeps them, deleting or compressing old ones. It usually runs daily from cron./etc/logrotate.conf (global) and /etc/logrotate.d/ (per service).# /etc/logrotate.d/myapp example
/var/log/myapp/*.log {
weekly
rotate 4
compress
delaycompress
missingok
notifempty
create 0640 root adm
postrotate
systemctl reload myapp > /dev/null 2>&1 || true
endscript
}| Directive | Meaning |
|---|---|
daily / weekly / monthly | Rotation period |
rotate N | Number of old logs to keep (the oldest is deleted beyond N) |
compress | Compress rotated logs with gzip |
delaycompress | Delay compression by one cycle (the most recent rotated log is not compressed) |
missingok | Do not raise an error if the log file is missing |
notifempty | Do not rotate if the log is empty |
create mode owner group | Create a new empty log file with the given permissions after rotation |
size N | Rotate when the specified size is exceeded |
postrotate ... endscript | Script to run after rotation (usually a service reload) |
What rotate N means: rotate 4 + weekly means "rotate every week and keep the most recent 4 (about 4 weeks' worth)". When rotated, myapp.log→myapp.log.1→myapp.log.2... get pushed back, and at the 5th, the oldest is deleted. With compress, they are compressed to .gz.
# Check config syntax + force run (debug)
logrotate -d /etc/logrotate.conf # -d: debug (does not actually run, simulation)
logrotate -f /etc/logrotate.conf # -f: force immediate runTry it yourself: open an existing file in /etc/logrotate.d/ (e.g., syslog) to see which directives are used, and simulate what would happen without touching real files using logrotate -d (debug mode). Also create a message with logger -p local0.err "hi" and trace it in /var/log/messages to see the facility.priority behavior with your own eyes.
| Category | NIDS (network based) | HIDS (host based) |
|---|---|---|
| What it monitors | Packets passing through the network | Files, logs, processes inside that server |
| Where it is installed | Choke points of traffic, such as switch mirror ports and gateways | On each server to be protected |
| What it catches well | Port scans, exploit packets, worm spread | System file tampering, backdoor installation, repeated login failures |
| What it cannot see | The contents of encrypted traffic | What happens outside its own host |
| Representative tools | snort, suricata | tripwire, AIDE, fail2ban (log based) |
fail2ban monitors log files in real time, and when a failure pattern exceeds a threshold within a set time window, it blocks that source IP with a firewall rule for a set duration.logpath), with which filter (filter), how many failures (maxretry), and for how long to block (bantime)".| File | Role |
|---|---|
/etc/fail2ban/jail.conf | The original provided by the distribution. Do not edit it directly (it is overwritten on package updates) |
/etc/fail2ban/jail.local | Administrator settings. It overrides jail.conf, and writing here is the rule |
/etc/fail2ban/jail.d/*.conf | Configuration fragments split per service |
/etc/fail2ban/filter.d/*.conf | Definitions of the regular expressions (failregex) that recognize failure logs |
/etc/fail2ban/action.d/*.conf | The actions that perform the actual blocking (iptables, firewalld, nftables, etc.) |
# /etc/fail2ban/jail.local
[DEFAULT]
bantime = 3600 # ban duration (seconds)
findtime = 600 # time window for counting failures (seconds)
maxretry = 5 # block when this count is exceeded
ignoreip = 127.0.0.1/8 192.168.1.0/24 # ranges never to block
[sshd]
enabled = true
port = ssh
logpath = /var/log/secure # /var/log/auth.log on the Debian familysystemctl enable --now fail2ban
fail2ban-client status # list of active jails
fail2ban-client status sshd # number and list of banned IPs for that jail
fail2ban-client set sshd unbanip 1.2.3.4 # manually unban
fail2ban-client reload # re-read the configurationIf you write settings directly in jail.conf, they get overwritten wholesale on a package update and vanish — always write them in jail.local (or jail.d/). Also, if you do not put your management range in ignoreip, a few password typos can get the administrator blocked, and if logpath does not match the distribution (/var/log/secure on the RHEL family vs /var/log/auth.log on the Debian family), the jail catches nothing even though it is running.
snort is a representative open-source NIDS that compares packets passing through the network against rules to find attack signatures.| Mode | What it does | Example |
|---|---|---|
| Sniffer | Captures packets and only prints them to the screen | snort -v (-d payload, -e adds link-layer headers) |
| Packet logger | Saves captured packets to a file | snort -l /var/log/snort |
| NIDS | Compares against rules and raises alerts | snort -c /etc/snort/snort.conf |
alert tcp any any -> 192.168.1.0/24 80 (msg:"passwd access"; content:"/etc/passwd"; sid:1000001; rev:1;)
└─ header: action protocol sourceIP port direction destIP port ─┘ └──────────── options ────────────┘
alert (alert + log), log (log only), pass (ignore). In inline (IPS) mode, drop and reject are also used-> (one-way), <> (bidirectional)msg (alert message), content (the string to find in the payload), sid (the rule's unique number; user-defined ones are 1000000 and above), rev (the rule revision number)/bin/ls being swapped out or an account being quietly added to /etc/passwd.tripwire and AIDE (Advanced Intrusion Detection Environment) take a snapshot of the size, permissions, owner, and hash of the monitored files as a baseline database.| Step | tripwire | AIDE |
|---|---|---|
| Configuration/policy | /etc/tripwire/twcfg.txt, policy twpol.txt | /etc/aide.conf |
| Create baseline DB | tripwire --init | aide --init |
| Integrity check | tripwire --check | aide --check |
| Accept legitimate changes | tripwire --update | aide --update |
twadmin), and you read check reports with twprint./var/lib/aide/aide.db.gz, and the administrator promotes the aide.db.new.gz created by --init by renaming it manually.The baseline DB only has meaning if it is created when the system is definitely clean (right after installation). If you run --init on an already compromised system, even the backdoor is registered as "normal" and the tool is rendered useless. Also, if you keep the DB and configuration only in writable locations on that server, an intruder can tamper with them too, so the rule is to keep them on read-only media or external storage.
The three tools do not overlap in role. fail2ban instantly blocks repeated failures that show up in logs (response), snort detects attack signatures in network packets (NIDS), and tripwire/AIDE verify after the fact whether files have changed (HIDS). That is why in practice all three are deployed together.
Tomorrow we do a comprehensive review of the week.
libwrap.enforcing (block + log), permissive (log only), disabled (off). You cannot reach disabled with setenforce.user:role:type:level label attached to files and processes. The actual decision depends on the type.last, lastb, and lastlog.rotate N is the number of old logs to keep.jail.local.Click a choice to reveal the answer and explanation.
Question 1
Which is the correct order in which TCP Wrapper checks its access control files?
Question 2
Among the SELinux modes, which one does not block policy violations and only logs them?
Question 3
Which is the correct command to temporarily switch SELinux to permissive mode?
Question 4
Among the files under /var/log, which holds the failed login records and must be viewed with the lastb command?
Question 5
Which is the correct interpretation of the mail.err /var/log/maillog setting in rsyslog.conf?
Question 6
In a logrotate configuration, which directive specifies the number of old log files to keep?
Question 7
Which is the correct option for checking a file's security context in SELinux?