Based on the Linux Master 1 exam syllabusReport an error
-A (append) versus -I (insert at front) changes the result.-P INPUT DROP, add the SSH (22) allow rule first. Otherwise you cut yourself off.service iptables save; for firewalld use --permanent + --reload.chcon (temporary) / restorecon (restore to policy) / semanage fcontext (permanent registration).facility.priority and records the given level and above. .none excludes, .= means only that level.interval + rotate N + compress. TCP Wrapper is allow first, and if neither file matches, access is allowed.| Chain | Direction | Example |
|---|---|---|
INPUT | Incoming (to the server) | External → my port 80 |
OUTPUT | Outgoing (from the server) | Me → external DNS |
FORWARD | Passing through (routing) | Via the gateway |
| Option | Meaning | Target | Meaning |
|---|---|---|---|
-A | Append to end of chain | ACCEPT | Allow |
-I | Insert at the front | DROP | Discard without reply |
-D | Delete | REJECT | Send a rejection reply |
-P | Default policy | LOG | Log and pass through |
-j | Specify the target | ||
-p | Protocol (tcp/udp) | --dport | Destination port |
-s | Source IP | --sport | Source port |
# List rules
iptables -L -n -v --line-numbers # Detailed listing with line numbers
# Allow incoming TCP on 80 (HTTP) and 22 (SSH)
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# Allow only a specific IP range
iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 3306 -j ACCEPT
# Block all other INPUT (default policy)
iptables -P INPUT DROP
# Delete a rule / reset
iptables -D INPUT 3 # Delete rule number 3
iptables -F # Flush all rules
# Save (persist after reboot) - required since rules live only in memory
service iptables save # or iptables-save > /etc/sysconfig/iptables⚠️ Fatal trap: If you run
iptables -P INPUT DROPfirst during a remote SSH session, you cut yourself off. Always add the port 22 ACCEPT rule first, then change the policy.
Order is everything: Rules are matched top to bottom and stop at the first match. If "DROP everything" is above, an ACCEPT added later (-A) never gets reached → insert it at the front with -I for it to work.
# Status and basic info
firewall-cmd --state # Whether it is running
firewall-cmd --get-default-zone # Default zone
firewall-cmd --list-all # All rules of the current zone
# Allow a service/port (permanent + immediate)
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload # Apply permanent to the current runtime
# Allow only a specific IP (rich rule)
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" service name="ssh" accept'
# Remove
firewall-cmd --permanent --remove-service=http
firewall-cmd --reload| Option | Meaning |
|---|---|
--add-service / --add-port | Allow a service name / port |
--permanent | Save permanently (not applied immediately) |
--reload | Apply permanent to the current session |
--list-all | Check all current rules |
--get-default-zone | Show the default zone |
⚠️ Key trap: Using only
--permanentdoes not apply right now. You must--reloadto apply it to the current session. Conversely, running only--add-servicewithout--reloaddisappears at reboot. "Permanent + immediate" =--permanentthen--reload.
Saving in iptables vs firewalld: iptables uses service iptables save, firewalld uses --permanent. Both solve the same problem, "rules in memory are volatile," in different ways.
getenforce # Current mode (Enforcing/Permissive/Disabled)
setenforce 0 # Temporarily switch to Permissive
setenforce 1 # Temporarily switch to Enforcing
# For a permanent change (including disabled), edit SELINUX= in /etc/selinux/config and reboot| Mode | Behavior |
|---|---|
Enforcing | Policy violations blocked + logged |
Permissive | Violations allowed but only logged |
Disabled | SELinux fully off |
user:role:type:level, and the type is the key.ls -Z /var/www/html # Check file context (-Z)
ps -eZ # Process contexts
# Change a context
chcon -t httpd_sys_content_t /web/index.html # Change immediately (temporary)
semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?" # Register permanently in policy
restorecon -Rv /web # Restore the default context per policy
# Booleans (feature on/off)
getsebool -a | grep httpd
setsebool -P httpd_can_network_connect on # -P = permanent| Command | Role |
|---|---|
ls -Z | Show context |
chcon -t type | Temporarily change context |
restorecon | Restore default context |
semanage fcontext | Register context in permanent policy |
setsebool -P | Permanently change a boolean |
Diagnostic signal: "Permissions (rwx) are right but access fails" → suspect SELinux. If setenforce 0 fixes it, it is an SELinux problem (in production, do not turn it off; fix the context with restorecon/chcon).
chcon vs restorecon: chcon sets it directly (may vanish on relabeling), restorecon restores per policy, and semanage fcontext + restorecon is the permanent standard. You cannot reach Disabled with setenforce (permanent is the config file).
/etc/rsyslog.conf (or /etc/rsyslog.d/*.conf) distributes logs.facility.priority destination.# facility.priority destination file
*.info;mail.none;authpriv.none;cron.none /var/log/messages
authpriv.* /var/log/secure
mail.* /var/log/maillog
cron.* /var/log/cron
*.emerg :omusrmsg:*
kern.* /var/log/kern.log
local7.* /var/log/boot.log| facility | Meaning | priority (low→high) |
|---|---|---|
auth/authpriv | Authentication, security | debug |
cron | Cron | info |
kern | Kernel | notice |
mail | warning/warn | |
daemon | Daemons | err/error |
local0~7 | User-defined | crit, alert, emerg |
| Notation | Meaning |
|---|---|
mail.info | Everything from mail at info and above |
mail.=info | Exactly the info level only |
mail.none | Exclude mail |
*.info | info and above of all facilities |
# Apply after editing
systemctl restart rsyslogThe setting that gathers security authentication logs into /var/log/secure is authpriv.* /var/log/secure. .none excludes, and .= means exactly that level only.
/etc/logrotate.conf and /etc/logrotate.d/* keep logs from growing without limit.# /etc/logrotate.d/myapp
/var/log/myapp/*.log {
weekly # Rotation interval: daily/weekly/monthly
rotate 4 # Number to keep (4 = about 4 weeks), older ones deleted
compress # gzip-compress rotated logs
delaycompress # Delay compression by one generation (latest log stays readable)
missingok # No error if the file is missing
notifempty # Do not rotate if empty
create 0640 root root # Permissions and owner of the new log file
size 100M # (Optional) rotate by size
postrotate # Command to run after rotation
systemctl reload rsyslog > /dev/null 2>&1 || true
endscript
}| Directive | Meaning |
|---|---|
daily/weekly/monthly | Rotation interval |
rotate N | Number of generations to keep |
compress | gzip compression |
size | Rotate by size |
create | Permissions and owner of the new file |
postrotate...endscript | Command after rotation |
logrotate -d /etc/logrotate.d/myapp # Debug (does not actually run, predicts behavior)
logrotate -f /etc/logrotate.d/myapp # Force an immediate rotationExam interpretation: weekly + rotate 4 + compress = rotate every week, keep the latest 4 (about 4 weeks) compressed, delete the excess. Size-based is size, interval-based is daily/weekly.
# /etc/hosts.allow (checked first, allowed on match)
sshd: 192.168.1.0/24
vsftpd: 192.168.1.10
# /etc/hosts.deny (checked next, denied on match)
ALL: ALLservice : host, and the keywords ALL/LOCAL/EXCEPT are used.| Keyword | Meaning |
|---|---|
ALL | All services/hosts |
LOCAL | Local hosts with no dot (.) |
EXCEPT | Specify an exception |
Order changes the result: allow takes priority over deny. The security standard is to put ALL: ALL in hosts.deny and whitelist only the targets to allow in hosts.allow. If both are empty, everything is allowed.
| Task | iptables | firewalld | Caution |
|---|---|---|---|
| Allow a port | -A INPUT -p tcp --dport N -j ACCEPT | --permanent --add-port=N/tcp | Direction, protocol |
| Block | -j DROP/REJECT | --remove-service | DROP = no response |
| Save | service iptables save | --permanent --reload | Prevent volatility |
| Policy | -P INPUT DROP | Zone default | Allow SSH first |
# Firewall status at a glance
iptables -L -n -v --line-numbers
firewall-cmd --list-all
# SELinux status and context
getenforce
ls -Z /var/www/html 2>/dev/null
# Preview log settings (no actual rotation)
logrotate -d /etc/logrotate.conf 2>&1 | head -20Lab tasks ultimately ask for exact options and directives. One direction, one type, one --reload decides pass or fail.
user:role:type:level label attached to files and processes. The verdict depends on the type.setsebool -P.Click a choice to reveal the answer and explanation.
Question 1
Which iptables command allows incoming TCP connections to port 22 (SSH) from outside?
Question 2
While managing a server over remote SSH, you want to change the firewall default policy to DROP. What is the correct order of operations?
Question 3
How do you allow the http service in firewalld so that it persists after reboot and takes effect immediately?
Question 4
Which SELinux mode only logs policy violations without blocking them?
Question 5
Which command restores a file's SELinux context to the default per policy?
Question 6
In rsyslog.conf, which is the correct meaning of mail.none?
Question 7
What is the correct behavior of the logrotate settings weekly, rotate 4, compress?