Essential Linux Command Cheat Sheet for System Administration
This comprehensive guide presents essential Linux commands for system shutdown, reboot, hardware inspection, file manipulation, user management, networking, performance monitoring, and troubleshooting, offering clear examples and code snippets to help administrators efficiently manage and diagnose Linux servers.
Basic Operations
Shutdown and reboot the system:
# Shutdown
shutdown -h now
# Reboot
shutdown -r nowView system and CPU information:
# Kernel info
uname -a
# Kernel version
cat /proc/version
# Environment variables
env
# CPU info
cat /proc/cpuinfo
# Logical CPU count and model
cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c
# Physical CPU count and cores
cat /proc/cpuinfo | grep physical | uniq -c
# Check if CPU runs in 64‑bit mode
getconf LONG_BIT
# Verify 64‑bit support
cat /proc/cpuinfo | grep flags | grep ' lm ' | wc -lCreate a symbolic link: ln -s /usr/local/jdk1.8/ jdk RPM related – check if a package is installed:
# List installed packages matching a name
rpm -qa | grep <em>package_name</em>SSH key generation and deployment:
# Generate SSH key
ssh-keygen -t rsa -C [email protected]
# Copy the public key to the target server's authorized_keysCommand aliasing (add to .bash_profile): alias ll='ls -alF' Synchronize server time: sudo ntpdate -u ntp.api.bz Run commands in the background with nohup:
# With output file
nohup xxx &
# Discard all output
nohup xxx > /dev/null &
# Redirect both stdout and stderr to a log
nohup xxx > out.log 2>&1 &Forcefully log out an active user (replace TTY): pkill -kill -t <em>TTY</em> Find the full path of a command: which <em>command</em> Check the maximum number of open file descriptors: ulimit -n Configure DNS resolver: vim /etc/resolv.conf Use nslookup to query DNS records: nslookup google.com Show recent login information:
# Last 5 logins
last -n 5Set a static IP address:
ifconfig em1 192.168.5.177 netmask 255.255.255.0Disk, File, and Directory Operations
Vim editing shortcuts:
# Global substitution
:%s/x/y/g
# Move cursor to line start / end
0
$
# Jump to file start / end
gg
G
# Toggle line numbers
:set nu
:set nonu
# Search forward and backward
/xxx
?xxxEdit a read‑only file and save without leaving the editor: :w !sudo tee % View disk and directory size information:
# Mounted filesystems
mount
# Partition usage
df
# Directory size (recursive)
du -H -h
# Top‑level directory sizes only
du -sh *Word count utilities:
# Line count
wc -l filename
# Word count
wc -w filename
# Length of longest line
wc -L filename
# Byte count
wc -c filenameCompress and decompress archives:
# Create archives
tar czvf xxx.tar <em>directory</em>
zip -r xxx.zip <em>directory</em>
# Extract archives
tar zxvf xxx.tar
# Extract to specific directory
tar zxvf xxx.tar -C /xxx/yyy/
unzip xxx.zipChange file ownership: chown eagleye.eagleye xxx.log Copy, remote copy, and create directories:
# Copy file
cp xxx.log
# Force overwrite
cp -f xxx.log
# Copy directory recursively
cp -r src_dir dest_dir
# Secure copy over SSH
scp -P <em>port</em> [email protected]:/home/user/xxx /home/xxx
# Create nested directories
mkdir -p /xxx/yyy/zzz
# Batch create Java project structure
mkdir -p src/{test,main}/{java,resources}Compare two files: diff -u 1.txt 2.txt Log performance testing by counting bytes written: tail -f xxx.log | pv -bt Remove special characters from scripts:
# Show special characters
cat -v xxx.sh
# Delete carriage‑return characters
sed -i 's/^M//g' env.shConvert file format to Unix line endings:
# Using dos2unix
find . -name "*.sh" | xargs dos2unixRedirect output to both screen and file:
awk '{print $0}' xxx.log | tee test.logSearch Commands
Grep examples:
# Invert match
grep -v xxx
# Exclude empty lines
grep -v '^$' file
# Show line numbers for matches
grep -n 'pattern' file
# Count occurrences
grep -c 'pattern' file
# Case‑insensitive search
grep -i 'pattern' fileAwk examples:
# Print lines where the 5th field contains "user"
awk -F ':' '{if ($5 ~ /user/) print $0}' /etc/passwd
# Count occurrences of a character (non‑Chinese)
awk -v RS='character' 'END {print --NR}' xxx.txtFind command examples:
# Find files with .mysql extension
find /home/eagleye -name "*.mysql" -print
# Files accessed in the last 3 days
find /usr -atime 3 -print
# Files modified in the last 5 days
find /usr -ctime 5 -print
# Files owned by user "jacky" starting with "j"
find /doc -user jacky -name "j*" -print
# Files ending with .bak and delete them
find /doc -name "*bak" -exec rm {} \;Network Related
Identify the process using a specific port: lsof -i:port Get the machine's IP address (excluding loopback and IPv6):
/sbin/ifconfig -a | grep inet | grep -v 127.0.0.1 | grep -v inet6 | awk '{print $2}' | tr -d "addr:"iptables firewall management:
# Show status
service iptables status
# Block an IP
iptables -I INPUT -s <em>IP</em> -j DROP
# Unblock an IP
iptables -D INPUT -s <em>IP</em> -j DROP
# Open port 9090
/sbin/iptables -I INPUT -p tcp --dport 9090 -j ACCEPT
# Start/stop/restart firewall
/etc/init.d/iptables start
/etc/init.d/iptables stop
/etc/init.d/iptables restartNetcat (nc) for TCP testing:
# Send data to a server
nc 192.168.0.11 8000 < data.txt
# Listen and save incoming data
nc -l 8000 > received_data
# Persistent listening
nc -lk 8000tcpdump to capture packets:
# Capture TCP packets on port 12301
tcpdump -i em1 tcp port 12301 -s 1500 -w abc.pcapTraceroute variations:
# ICMP traceroute
traceroute -I www.163.com
# Start from TTL 3
traceroute -M 3 www.163.com
# Specify destination port
traceroute -p 8080 192.168.10.11ss (socket statistics) examples:
# List listening sockets
ss -l
# Show process info for sockets
ss -pl
# Show all TCP sockets
ss -t -a
# Show all UDP sockets
ss -u -a
# Show established SMTP connections
ss -o state established '( dport = :smtp or sport = :smtp )'
# Show established HTTP connections
ss -o state established '( dport = :http or sport = :http )'netstat examples:
# Count connections per IP and total
netstat -n | awk '/^tcp/ {n=split($(NF-1),a,":"); if(n<=2) ++S[a[1]]; else ++S[a[4]]; ++s[$NF]; ++N} END {for(i in S) printf("%-20s %s
", i, S[i]); printf("%-20s %s
","TOTAL_IP",NR); for(i in s) printf("%-20s %s
",i,s[i]); printf("%-20s %s
","TOTAL_LINK",N);}'
# Show connection state distribution
netstat -n | awk '/^tcp/ {++state[$NF]} END {for(k in state) print k, "\t", state[k]}'
# Find most frequent TIME_WAIT connections
netstat -n | grep TIME_WAIT | awk '{print $5}' | sort | uniq -c | sort -rn | head -n20Linux Performance Monitoring
top – interactive process viewer (use F or O then a‑z to sort columns, R to reverse).
Column
Meaning
PID
Process ID
PPID
Parent process ID
RUSER
Real user name
UID
User ID of owner
USER
Username of owner
GROUP
Group name of owner
TTY
Terminal that started the process ("?" if none)
PR
Priority
NI
Nice value (negative = higher priority)
P
CPU used last (relevant on multi‑CPU systems)
%CPU
CPU usage percentage since last update
TIME
Total CPU time used (seconds)
%MEM
Physical memory usage percentage
VIRT
Virtual memory size (KB)
RES
Resident (non‑swapped) memory size (KB)
SHR
Shared memory size (KB)
S
Process state (D=uninterruptible sleep, R=running, S=sleep, T=stopped, Z=zombie)
COMMAND
Command name / line
dmesg – display kernel ring buffer messages: dmesg iostat – monitor disk I/O performance:
iostat -xz 1
# Columns: r/s, w/s, rkB/s, wkB/s, await (ms), avgqu‑sz, %utilfree – show memory usage:
free -m
# Output includes total, used, free, shared, buffers, cached, and swap.sar – system activity reporter for network throughput and TCP stats:
# Network device throughput
sar -n DEV 1
# TCP connection statistics
sar -n TCP,ETCP 1vmstat – report CPU, memory, swap, I/O, and system activity:
# Sample: vmstat 2 1 (collect once after 2‑second interval)
vmstat 2 1Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
