Security Hardening and Monitoring
Category: Advanced Linux Administration
Type: Linux Commands
Generated on: 2025-07-10 03:17:41
For: System Administration, Development & Technical Interviews
Security Hardening & Monitoring - Linux Commands Cheatsheet
Section titled “Security Hardening & Monitoring - Linux Commands Cheatsheet”This cheat sheet provides a comprehensive guide to Linux commands used for security hardening and monitoring. It’s designed for both system administrators and developers, offering practical examples and advanced techniques.
1. Command Overview
These commands are essential for securing your Linux systems, from initial hardening to continuous monitoring for potential threats and vulnerabilities. They allow you to control access, audit system activity, detect intrusions, and maintain a secure environment.
2. Basic Syntax
command [options] [arguments]
3. Practical Examples
-
iptables/nftables(Firewall Management)-
Command Overview:
iptables(legacy) andnftables(modern) are used to configure the Linux kernel’s built-in firewall. They define rules for allowing or blocking network traffic based on various criteria (source/destination IP, port, protocol, etc.).nftablesis generally preferred due to its improved syntax and performance. -
Basic Syntax (nftables):
Terminal window nft [options] command object -
Basic Syntax (iptables):
Terminal window iptables [-t table] command [chain] rule-specification [options] -
Practical Examples (nftables):
Terminal window # List all nftables rulesnft list rules# Create a new filter tablenft add table inet filter# Create an input chainnft add chain inet filter input { type filter hook input priority 0 \; policy drop \; }# Allow SSH trafficnft add rule inet filter input tcp dport 22 ct state new accept# Allow established and related connectionsnft add rule inet filter input ct state {established, related} accept# Drop all other incoming trafficnft add rule inet filter input drop# Save the configuration (Debian/Ubuntu)nft list rules > /etc/nftables.confsystemctl restart nftables.service# Load the configuration on bootsystemctl enable nftables.service -
Practical Examples (iptables):
Terminal window # List all iptables rulesiptables -L# Allow SSH traffic (port 22)iptables -A INPUT -p tcp --dport 22 -j ACCEPT# Allow HTTP traffic (port 80)iptables -A INPUT -p tcp --dport 80 -j ACCEPT# Allow HTTPS traffic (port 443)iptables -A INPUT -p tcp --dport 443 -j ACCEPT# Allow established and related connectionsiptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT# Drop all other incoming trafficiptables -P INPUT DROP# Save iptables rules (Debian/Ubuntu)iptables-save > /etc/iptables/rules.v4# Load iptables rules on boot (Debian/Ubuntu)# Create /etc/network/if-pre-up.d/iptablesload:# #!/bin/sh# iptables-restore < /etc/iptables/rules.v4# chmod +x /etc/network/if-pre-up.d/iptablesload# Save iptables rules (CentOS/RHEL)iptables-save > /etc/sysconfig/iptables# Load iptables rules on boot (CentOS/RHEL)systemctl enable iptables.servicesystemctl start iptables.serviceSample Output (
iptables -L):Chain INPUT (policy DROP)target prot opt source destinationACCEPT tcp -- anywhere anywhere tcp dpt:sshACCEPT tcp -- anywhere anywhere tcp dpt:httpACCEPT tcp -- anywhere anywhere tcp dpt:httpsACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHEDDROP all -- anywhere anywhere -
Common Options (iptables):
-A chain: Append a new rule to the specified chain.-I chain [rulenum]: Insert a new rule at the specified position in the chain.-D chain rulenum: Delete a rule from the chain.-D chain rule-specification: Delete a rule matching the specification.-L chain: List rules in the specified chain.-L -vfor verbose output.-L -nfor numeric output.-F chain: Flush (delete all rules) in the specified chain.-P chain target: Set the default policy for the chain.-p protocol: Specify the protocol (e.g., tcp, udp, icmp).--dport port: Specify the destination port.--sport port: Specify the source port.-s source: Specify the source IP address or network.-d destination: Specify the destination IP address or network.-j target: Specify the target action (e.g., ACCEPT, DROP, REJECT).-m module: Use a specific module (e.g., conntrack, state).
-
Common Options (nftables):
list rules: Lists all the rulesadd table: Creates a new tableadd chain: Creates a new chainadd rule: Adds a new rule
-
Advanced Usage (iptables):
Terminal window # Rate limit incoming SSH connections to prevent brute-force attacksiptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -m recent --set --name SSH --rsourceiptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -m recent --update --seconds 60 --hitcount 4 --name SSH --rsource -j DROP# Log dropped packetsiptables -A INPUT -j LOG --log-prefix "IPTABLES DROP: " --log-level 4 -
Advanced Usage (nftables):
Terminal window # Create a set to store IP addresses to blocknft add set inet filter blacklist { type ipv4_addr \; flags interval,timeout \; }# Add an IP address to the blacklist with a timeout of 1 hournft add element inet filter blacklist { 192.168.1.1 timeout 3600s }# Block traffic from IPs in the blacklistnft add rule inet filter input ip saddr @blacklist drop -
Tips & Tricks:
- Start with a restrictive policy (e.g., DROP all incoming traffic) and then selectively allow necessary traffic.
- Use
iptables-saveornft list rulesto save your configuration and ensure it’s loaded on boot. - Test your rules carefully before applying them to a production system.
- Use connection tracking (
-m conntrack) to efficiently handle related connections. - Use logging (
-j LOG) to monitor dropped packets and identify potential attacks.
-
Troubleshooting:
- If you lock yourself out of your system, use a console connection or out-of-band management to revert the changes.
- Check the system logs for error messages related to iptables/nftables.
- Use
iptables -L -v -nornft list rulesto verify that your rules are configured correctly.
-
Related Commands:
tcpdump,nmap,netstat,ss
-
-
auditd(Auditing)-
Command Overview:
auditdis the Linux audit daemon. It collects detailed information about system events, such as file access, command execution, and user logins. This information is invaluable for security auditing, compliance, and forensic analysis. -
Basic Syntax:
Terminal window auditctl [options]ausearch [options]aureport [options] -
Practical Examples:
Terminal window # Start the audit daemonsystemctl start auditd# Check the status of the audit daemonsystemctl status auditd# Configure audit rules (e.g., monitor all access to /etc/passwd)auditctl -w /etc/passwd -p war -k passwd_changes# List current audit rulesauditctl -l# Search audit logs for events related to passwd_changesausearch -k passwd_changes# Generate a report of all audit eventsaureport -au# Generate a report of all login eventsaureport -l# Generate a report of all file access eventsaureport -f# Stop the audit daemonsystemctl stop auditdSample Output (
auditctl -l):LIST_RULES: exit,always watch=/etc/passwd perm=war key=passwd_changes -
Common Options:
auditctl -w path -p permissions -k key: Add a watch rule for a specific file or directory.path: The file or directory to watch.permissions: The permissions to monitor (r=read, w=write, x=execute, a=attribute change).key: A unique identifier for the rule.
auditctl -l: List current audit rules.auditctl -d rule_number: Delete an audit rule.ausearch -k key: Search audit logs for events with a specific key.ausearch -m message_type: Search for specific message types (e.g.,USER_LOGIN,SYSCALL).ausearch -ts start_time -te end_time: Search within a specific time range.aureport -au: Generate a user activity report.aureport -l: Generate a login report.aureport -f: Generate a file access report.
-
Advanced Usage:
Terminal window # Monitor all system calls related to user management (e.g., useradd, userdel)auditctl -a always,exit -F arch=b64 -S adduser,deluser,usermod -k user_management# Configure auditd to rotate logs daily and keep 30 days of logs# Edit /etc/audit/auditd.conf:# freq = daily# num_logs = 30# max_log_file = 10# systemctl restart auditd -
Tips & Tricks:
- Start with a minimal set of audit rules to avoid overwhelming the system with log data.
- Use descriptive keys to easily identify and search for specific events.
- Regularly review audit logs to identify suspicious activity.
- Consider using a log management system to centralize and analyze audit logs.
- Configure
auditd.confappropriately for log rotation and retention.
-
Troubleshooting:
- If auditd is not running, check the system logs for error messages.
- Verify that the audit rules are configured correctly using
auditctl -l. - Ensure that the audit logs are being written to the correct location (default:
/var/log/audit/audit.log). - If you’re not seeing the expected events in the audit logs, double-check the permissions and parameters of your audit rules.
-
Related Commands:
strace,lsof,systemd-journald
-
-
ps/top/htop(Process Monitoring)-
Command Overview: These commands provide information about running processes on the system.
psis a static snapshot, whiletopandhtopprovide real-time monitoring. They are essential for identifying resource-intensive processes, detecting rogue processes, and troubleshooting performance issues. -
Basic Syntax:
Terminal window ps [options]top [options]htop [options] -
Practical Examples:
Terminal window # List all running processesps aux# List processes owned by a specific userps -u username# Find the process ID (PID) of a process by nameps -ef | grep process_name# Display real-time process monitoring with toptop# Display real-time process monitoring with htop (requires installation)htopSample Output (
ps aux | head -n 5):USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDroot 1 0.0 0.0 168284 6540 ? Ss Oct26 0:01 /sbin/initroot 2 0.0 0.0 0 0 ? S Oct26 0:00 [kthreadd]root 3 0.0 0.0 0 0 ? I< Oct26 0:00 [rcu_gp]root 4 0.0 0.0 0 0 ? I< Oct26 0:00 [rcu_par_gp] -
Common Options:
ps aux: List all processes with detailed information (user, PID, CPU usage, memory usage, etc.).ps -ef: List all processes with full command line information.ps -u username: List processes owned by a specific user.top: Display real-time process monitoring.htop: An interactive process viewer with more features thantop(requires installation:sudo apt install htoporsudo yum install htop).
-
Advanced Usage:
Terminal window # Find the process ID (PID) of a process and kill itPID=$(ps -ef | grep process_name | grep -v grep | awk '{print $2}')kill -9 $PID# Monitor processes consuming the most CPUtop -o %CPU# Monitor processes consuming the most memorytop -o %MEM# Monitor processes started by a specific usertop -u username -
Tips & Tricks:
- Use
grepto filter the output ofpsto find specific processes. - Use
killto terminate a process. Usekill -9as a last resort (forces immediate termination). htopprovides a more user-friendly interface and more features thantop.- Pay attention to the CPU and memory usage of processes to identify potential bottlenecks or resource leaks.
- Monitor processes started by unknown users or processes that are consuming excessive resources.
- Use
-
Troubleshooting:
- If a process is unresponsive, try sending a
SIGTERMsignal (kill -15 PID) before resorting toSIGKILL(kill -9 PID). - Check the system logs for error messages related to the process.
- Use
straceto trace the system calls made by a process.
- If a process is unresponsive, try sending a
-
Related Commands:
kill,pmap,lsof,vmstat,iostat
-
-
netstat/ss(Network Monitoring)-
Command Overview:
netstat(legacy) andss(modern) are used to display network connections, listening ports, routing tables, and network interface statistics.ssis generally faster and provides more detailed information. -
Basic Syntax:
Terminal window netstat [options]ss [options] -
Practical Examples:
Terminal window # List all listening ports (netstat)netstat -tulnp# List all listening ports (ss)ss -tulnp# List all established TCP connections (netstat)netstat -t# List all established TCP connections (ss)ss -t# Show network interface statistics (netstat)netstat -i# Show network interface statistics (ss)ss -i# Show routing table (netstat)netstat -r# Show routing table (ss)ip route showSample Output (
netstat -tulnp | head -n 5):Active Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program nametcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 950/sshdtcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1321/mastertcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1548/apache2tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 1548/apache2 -
Common Options (netstat):
-t: Display TCP connections.-u: Display UDP connections.-l: Display listening sockets.-n: Display numerical addresses (don’t resolve hostnames).-p: Display the process ID (PID) and program name associated with the socket.-r: Display the routing table.-i: Display network interface statistics.
-
Common Options (ss):
-t: Display TCP connections.-u: Display UDP connections.-l: Display listening sockets.-n: Display numerical addresses (don’t resolve hostnames).-p: Display the process ID (PID) associated with the socket.-i: Display network interface statistics.-s: Display summary statistics.
-
Advanced Usage (netstat):
Terminal window # Find the process listening on a specific portnetstat -tulnp | grep :80# Monitor network traffic in real-time using watchwatch -n 1 netstat -tulnp -
Advanced Usage (ss):
Terminal window # Find the process listening on a specific portss -tulnp | grep :80# Monitor network traffic in real-time using watchwatch -n 1 ss -tulnp# Show TCP connections in state ESTABLISHEDss -t state established -
Tips & Tricks:
- Use
netstat -tulnporss -tulnpto identify which processes are listening on specific ports. - Use
netstat -rorip route showto examine the routing table. - Use
netstat -iorss -ito monitor network interface statistics (e.g., packets sent/received, errors). - Monitor for unusual network activity, such as connections to unknown IP addresses or ports.
- Use
-
Troubleshooting:
- If a service is not listening on the expected port, check the service configuration and logs.
- If you’re unable to connect to a remote host, check the routing table and firewall rules.
- Use
tcpdumpto capture and analyze network traffic.
-
Related Commands:
tcpdump,nmap,iptables,nftables,ip
-
-
lsof(List Open Files)-
Command Overview:
lsoflists all open files and the processes that are using them. In Linux, “everything is a file,” so this includes network sockets, pipes, devices, and regular files. It’s useful for identifying which process is using a particular file or socket, troubleshooting resource conflicts, and detecting suspicious activity. -
Basic Syntax:
Terminal window lsof [options] [file...] -
Practical Examples:
Terminal window # List all open fileslsof# List open files for a specific process ID (PID)lsof -p PID# List open files for a specific userlsof -u username# List open files for a specific filelsof /path/to/file# List open files for a specific network portlsof -i :port_number# List open files for a specific internet addresslsof -i@host:portSample Output (
lsof | head -n 5):COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEsystemd 1 root cwd DIR 253,0 4096 2 /systemd 1 root rtd DIR 253,0 4096 2 /systemd 1 root txt REG 253,0 1682840 8129 /usr/lib/systemd/systemdsystemd 1 root mem REG 253,0 1671776 2442 /usr/lib/locale/locale-archive -
Common Options:
-p PID: List open files for a specific process ID.-u username: List open files for a specific user./path/to/file: List processes using the specified file.-i [protocol][@hostname|address][:port]: List open files related to network connections.-i TCP:80- TCP connections on port 80-i UDP- All UDP connections
-c command: List open files used by processes whose command names begin with the string command.+D directory: Recursively list all files under a directory. Useful for finding processes holding open files in a directory.
-
Advanced Usage:
Terminal window # Find the process listening on port 80lsof -i :80# Find all files opened by the Apache web serverlsof -c apache2# Find all files opened in /tmp directorylsof +D /tmp# Kill all processes using a specific file (use with caution!)lsof /path/to/file | awk 'NR!=1 {print $2}' | xargs kill -9 -
Tips & Tricks:
- Use
lsof -ito identify which process is listening on a specific port. - Use
lsof /path/to/fileto determine which process is preventing you from deleting or modifying a file. - Use
lsof +D /directoryto find processes holding open files in a directory.
- Use
-
Troubleshooting:
- If you’re unable to delete a file, use
lsofto find the process that is using it and terminate the process. - If a service is not working correctly, use
lsofto check if it has the necessary files open.
- If you’re unable to delete a file, use
-
Related Commands:
ps,netstat,ss,fuser
-
-
find(File Searching)-
Command Overview: The
findcommand is a powerful tool for locating files and directories based on various criteria, including name, size, modification time, permissions, and ownership. It’s essential for security audits, identifying suspicious files, and performing file management tasks. -
Basic Syntax:
Terminal window find [path...] [expression] -
Practical Examples:
Terminal window # Find all files in the current directoryfind .# Find all files with a specific namefind . -name "filename.txt"# Find all files with a specific extensionfind . -name "*.log"# Find all files larger than 10MBfind . -size +10M# Find all files modified in the last 24 hoursfind . -mtime -1# Find all files owned by a specific userfind . -user username# Find all files with specific permissions (e.g., world-writable)find . -perm 777Sample Output (
find . -name "*.log"):./access.log./error.log./debug.log -
Common Options:
-name pattern: Find files with a specific name matching the pattern.-type type: Find files of a specific type (f=file, d=directory, l=symbolic link).-size [+|-]size: Find files larger (+) or smaller (-) than the specified size. Use suffixes likek(kilobytes),M(megabytes),G(gigabytes).-mtime [+|-]n: Find files modified more than (+) or less than (-) n days ago.-atime [+|-]n: Find files accessed more than (+) or less than (-) n days ago.-ctime [+|-]n: Find files whose status was changed more than (+) or less than (-) n days ago.-user username: Find files owned by a specific user.-group groupname: Find files owned by a specific group.-perm mode: Find files with specific permissions (e.g., 777, 644).-perm -modefinds files with at least these permissions.-perm /modefinds files with any of these permissions set.-exec command {} \;: Execute a command on each file found.{}is replaced with the filename.\;terminates the command.-print: Print the filename (default action).-delete: Delete the files found (USE WITH EXTREME CAUTION!).
-
Advanced Usage:
Terminal window # Find all world-writable files and directories and print their permissionsfind . -perm 777 -print -exec ls -l {} \;# Find all files modified in the last 7 days and back them upfind . -mtime -7 -exec cp {} /backup/ \;# Find all empty files and delete them (USE WITH EXTREME CAUTION!)find . -type f -empty -delete# Find all files larger than 100MB and compress themfind . -size +100M -exec gzip {} \; -
Tips & Tricks:
- Use
-type fto search only for files, and-type dto search only for directories. - Use
-size,-mtime,-atime, and-ctimeto narrow down your search based on file attributes. - Use
-execto perform actions on the files found. - Be extremely careful when using
-deleteto avoid accidentally deleting important files. Test your command without-deletefirst. - Use quotes around patterns containing wildcards (e.g.,
find . -name "*.log") to prevent shell expansion.
- Use
-
Troubleshooting:
- If
findis not finding the files you expect, double-check the search criteria and the path. - If you get a “Permission denied” error, run
findas root or withsudo. - If you’re using
-exec, make sure the command is valid and that you have the necessary permissions to execute it.
- If
-
Related Commands:
locate,grep,ls
-
-
chmod/chown(Permissions and Ownership)-
Command Overview:
chmod(change mode) is used to modify the permissions of files and directories.chown(change owner) is used to change the ownership (user and group) of files and directories. These commands are fundamental for controlling access to your system’s resources. -
Basic Syntax:
Terminal window chmod [options] mode file...chown [options] user[:group] file... -
Practical Examples:
Terminal window # Change the permissions of a file to read/write for the owner, read-only for the group, and read-only for others (644)chmod 644 filename.txt# Change the permissions of a directory to read/write/execute for the owner, read/execute for the group, and read/execute for others (755)chmod 755 directory_name# Add execute permissions for the owner to a filechmod +x filename.sh# Remove write permissions for the group from a filechmod g-w filename.txt# Change the owner of a file to a specific userchown username filename.txt# Change the owner and group of a filechown username:groupname filename.txt# Change the owner and group of a directory and all its contents recursivelychown -R username:groupname directory_nameSample Output (no output, but the permissions change):
Terminal window # Before:ls -l filename.txt-rw-r--r-- 1 user user 1024 Oct 27 10:00 filename.txtchmod 755 filename.txt# After:ls -l filename.txt-rwxr-xr-x 1 user user 1024 Oct 27 10:00 filename.txt -
Common Options:
-
chmod mode file: Change the permissions of the file using octal notation (e.g., 644, 755). -
chmod +permissions file: Add permissions (e.g., +x for execute). -
chmod -permissions file: Remove permissions (e.g., -w for write). -
chmod u+permissions file: Change permissions for the user (owner). -
chmod g+permissions file: Change permissions for the group. -
chmod o+permissions file: Change permissions for others. -
chmod a+permissions file: Change permissions for all (user, group, others). -
chmod -R directory: Recursively change the permissions of a directory and all its contents. -
chown user file: Change the owner of the file. -
chown group file: Change the group of the file. -
chown user:group file: Change both the owner and group of the file. -
chown -R directory: Recursively change the owner and group of a directory and all its contents.
-
-
Advanced Usage:
Terminal window # Use symbolic mode to give the owner read/write/execute permissions, the group read/execute permissions, and others no permissions.chmod u=rwx,g=rx,o= filename.txt# Give the group the same permissions as the ownerchmod --reference=owner_file group_file# Find all files with mode 644 and change them to 600find . -perm 644 -exec chmod 600 {} \;# Change the owner and group of all files in a directory to a specific user and groupchown -R username:groupname /path/to/directory -
Tips & Tricks:
- Use octal notation or symbolic mode to specify permissions.
- Use
-Rto recursively change permissions or ownership of directories. - Be careful when using
chmod 777as it makes files world-writable. - Ensure that the user and group you are changing ownership to exist on the system.
- Use
ls -lto verify the permissions and ownership of files after making changes.
-
Troubleshooting:
- If you get a “Permission denied” error, run
chmodorchownas root or withsudo. - If you accidentally change the permissions of a critical system file, restore the original permissions from a backup or consult the documentation for the file.
- If you are unable to change the owner or group of a file, ensure that you have the necessary permissions (typically, you must be root).
- If you get a “Permission denied” error, run
-
Related Commands:
ls,chgrp,umask
-
-
su/sudo(Privilege Escalation)-
Command Overview:
su(switch user) allows you to become another user, typically the root user.sudo(superuser do) allows you to execute a single command with root privileges. These commands are crucial for performing administrative tasks that require elevated permissions. -
Basic Syntax:
Terminal window su [options] [user]sudo [options] command -
Practical Examples:
Terminal window # Become the root user (su)su# Become a specific user (su)su username# Execute a command as root (sudo)sudo apt update# Execute a command as a specific user (sudo)sudo -u username commandSample Output (su):
Terminal window # Normal user:whoamiusersuPassword:whoamiroot -
Common Options:
su: Switch to the root user.su -: Switch to the root user with a login shell (loads the root user’s environment).su username: Switch to a specific user.sudo command: Execute a command as root.sudo -u username command: Execute a command as a specific user.sudo -i: Start a new shell as root (similar tosu -).sudo -l: List the commands that the current user is allowed to run withsudo.
-
Advanced Usage:
Terminal window # Grant a user specific sudo privileges by editing /etc/sudoers (using visudo)# Example:# username ALL=(ALL:ALL) ALL # User can run any command as any user# username ALL=(root) /usr/bin/apt update, /usr/bin/apt upgrade # User can only run apt update and upgrade as root# Execute a sequence of commands as root using sudo -isudo -iapt updateapt upgradeexit -
Tips & Tricks:
- Use
sudoinstead ofsuwhenever possible to minimize the risk of accidental damage. - Configure
sudoerscarefully to grant users only the necessary privileges. - Use `
- Use
-