Linux Cheat Sheet / Advanced Command Reference.
Advanced command reference for Linux. We assume you already know basic commands such as ls, pwd, mkdir, gzip, etc. Here are some useful additions which you may not already know.
DNS (Domain Name Service)
Copy to Clipboard
# Perform forward or reverse lookup on string
host string
# Lookup string’s host info from nameserver
dig @nameserver string
File Manipulation
Copy to Clipboard
# Move all files containing string to /path
find . -type f -exec grep -l string {} \; -exec mv {} /path \;
# Change ownership and group of all files recursively
chown -cR --from=usera:groupa userb:groupb *
System Hardware Information
Copy to Clipboard
# Find total amount of RAM in the system
grep -i memtotal /proc/meminfo
# Display DMI/SMBIOS information
dmidecode -q
# Display CPU information
cat /proc/cpuinfo
# See if a processor supports hardware virtualization
egrep '(vmx|svm)' /proc/cpuinfo
# Display PCI information
lspci -tv
# Display USB information
lsusb -tv
# Display disk information for sda
hdparm -i /dev/sda
Web/HTML Editing Commands
Copy to Clipboard
# Recursively download entire website
wget -r -np -nH -mkp -e robots=off http://www.domain.com/
# Replace all X's with Y's in all files this directory and below.
find . -type f -print -exec sed -i -e 's|X|Y|g' {} \;
# View special characters in files. Isolate \n\r issues.
od -c
# Fix some DOS file formatting issues. ^V means Control-V
%/\n\n/^V^M/g
# Replace all X's with Y's in all files ending with *.txt
perl -pi -w -e 's/X/Y/g;' *.txt
# Print filename and all lines containing 'string'
find -name '*' | xargs grep 'string'
# Print all file names containing Test String in all subdirectories
grep -Hir "Test String" .
SSH (Secure Shell)
Copy to Clipboard
# Recursively copy while preserving permissions
scp -p -r [USER]@[HOST]:file /dir
# Create public & private keys on LOCAL-HOST
ssh-keygen
# Install public key for trusted login on REMOTE-HOST
ssh-copy-id -i ~/.ssh/id_rsa.pub [REMOTE-HOST]
# Jump though host1 to access host2
ssh -J root@host1.domain.com root@host2.domain.com
Troubleshooting
Copy to Clipboard
# Full process list with Child Process Tree
ps -auxf
# List all active network connections and state
netstat -tup
# Show information related to http (port 80) connections
lsof -i :80
# List open files by a particular userid
lsof -u userid
# List active network connections consuming bandwidth
iftop
# Top for Apache web server
apachetop -f logfile
# Find out which patches have been applied to a package
rpm -q --changelog PACKAGE-NAME
# Monitor IP Traffic
iptraf
# Like top, but with an h
htop
# My Traceroute - An improved traceroute
mtr
# Tail multiple files at the same time
multitail
# A http/web stress test utility
siege
Samba / Windows File Sharing
Copy to Clipboard
# Scan local network for windows shares
smbtree
# List all open files and show IP addresses with process IDs
smbstatus
Isolate High Load on Linux (CentOS) Servers
Copy to Clipboard
clamscan -ir --scan-mail=no --exclude-dir="mail" .
netstat -an | grep :80 | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr
netstat -anp | grep 'tcp\|udp' | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr
netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr
netstat -ntu | grep -v TIME_WAIT | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr
netstat -n --tcp --udp --numeric-hosts | grep -v 127.0.0.1 | awk '{if (/(tcp|udp)/) { print $5 }}' | sed 's/:.*//' | sort | uniq -c | sort -n | awk '{if ($1 > 20) {print "Count: "$1"\t"$2; }}'
# Isolate Spam Scripts in WHM/CPanel Environment
grep cwd /var/log/exim_mainlog | grep -v /var/spool | awk -F"cwd=" '{print $2}' | awk '{print $1}' | sort | uniq -c | sort -n