Essential Linux Command Cheat Sheet for System Administration
This article compiles a comprehensive set of Linux command‑line shortcuts covering system shutdown, hardware inspection, user management, networking, file handling, compression, performance monitoring, and many other routine administration tasks, providing a handy reference for developers and sysadmins alike.
Although most of my work involves Java development, I frequently use Linux, especially on macOS with a dark terminal, and I have compiled a collection of useful Linux commands for quick reference.
Basic Operations
Linux shutdown, reboot
# Shutdown
shutdown -h now
# Reboot
shutdown -r nowView system and CPU information
# View kernel info
uname -a
# View kernel version
cat /proc/version
# View current environment variables
env
# View CPU info
cat /proc/cpuinfo
# Count logical CPUs and model names
cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c
# Count physical CPUs and cores
cat /proc/cpuinfo | grep physical | uniq -c
# Check if CPU runs in 32‑bit or 64‑bit mode
getconf LONG_BIT
# Verify 64‑bit support
cat /proc/cpuinfo | grep flags | grep ' lm ' | wc -lCreate symbolic link
ln -s /usr/local/jdk1.8/ jdkrpm related
# Check if a package was installed via rpm
rpm -qa | grep <package_name>ssh key
# Generate ssh key
ssh-keygen -t rsa -C [email protected]
# Copy the content of id_rsa.pub to the remote server's ~/.ssh/authorized_keys (create the file if it does not exist; .ssh permission 700, authorized_keys permission 600)Command alias
# Add alias in each user's .bash_profile
alias ll='ls -alF'Synchronize server time
sudo ntpdate -u ntp.api.bzRun command in background
# Background with nohup.out
nohup xxx &
# Background without any log
nohup xxx > /dev/null &
# Background with error output redirected to log
nohup xxx > out.log 2>&1 &Force logout active user
# Force logout by TTY
pkill -kill -t <TTY>Find command path
which <command>View max file descriptors for a process
ulimit -nConfigure DNS
vim /etc/resolv.confnslookup, view domain routing
nslookup google.comlast, recent login info
# Show last 5 login accounts
last -n 5Set static IP
ifconfig em1 192.168.5.177 netmask 255.255.255.0View environment variables of a process
# Show environment of process with PID XXXXX
ps eww -p XXXXXView process tree
ps auwxfView process start path
cd /proc/<pid>
ls -all # cwd shows the start directoryAdd user and configure sudo
# Add user
useradd <username>
passwd <username>
# Grant sudo
vim /etc/sudoers # Add a line: <username> ALL=(ALL) ALLKill all processes containing a name
ps aux | grep xxx | grep -v grep | awk '{print $2}' | xargs kill -9Disk, File, Directory Operations
vim operations
# Global replace
:%s/x/y/g
# Move cursor to line start
0
# Move cursor to line end
$
# Jump to file end
Shift+g
# Jump to file start
gg
# Show line numbers
:set nu
# Hide line numbers
:set nonu
# Search forward
/xxx # press n for next
# Search backward
?xxxEdit read‑only file without switching user
:w !sudo tee %View disk and directory information
# Show mounted filesystems
mount
# Show partition usage
df
# Show directory size recursively
du -H -h
# Show size of each item in current directory (non‑recursive)
du -sh *wc command
# Count lines
wc -l filename
# Count words
wc -w filename
# Length of longest line
wc -L filename
# Count bytes
wc -cCompression and Decompression
Compress
tar czvf xxx.tar # compress directory
zip -r xxx.zip # compress directoryDecompress
tar zxvf xxx.tar
tar zxvf xxx.tar -C /xxx/yyy/ # extract to specific folder
unzip xxx.zipChange file ownership
chown eagleye.eagleye xxx.logCopy, scp, mkdir
# Copy file
cp xxx.log
# Force overwrite
cp -f xxx.log
# Copy directory
cp -r src_dir dest_dir
# Remote copy
scp -P <ssh_port> [email protected]:/home/username/xxx /home/xxx
# Create nested directories
mkdir -p /xxx/yyy/zzz
# Batch create java and resources folders under test and main
mkdir -p src/{test,main}/{java,resources}Compare two files
diff -u 1.txt 2.txtLog output byte count (performance testing)
# Append a dot to log each iteration and monitor size
tail -f xxx.log | pv -btView and remove special characters
# Show special characters
cat -v xxx.sh
# Remove carriage return characters
sed -i 's/^M//g' env.shConvert file format
# Convert to Unix format
cat file.sh > file.sh_bak
# Create new file from stdin
cat > file1.sh
# Set encoding in vim
:set fileencodings=utf-8
:w
:set fileformat=unix
# Use dos2unix on mac
find . -name "*.sh" | xargs dos2unixtee and redirect while displaying
awk '{print $0}' xxx.log | tee test.logNetwork Related
Find process using a port
lsof -i:<port>Get local IP address
/sbin/ifconfig -a | grep inet | grep -v 127.0.0.1 | grep -v inet6 | awk '{print $2}' | tr -d "addr:"iptables
# Show status
service iptables status
# Block an IP
iptables -I INPUT -s <ip> -j DROP
# Unblock an IP
iptables -D INPUT -s <ip> -j DROP
# Open port 9090
/sbin/iptables -I INPUT -p tcp --dport 9090 -j ACCEPT
# Service control
/etc/init.d/iptables status
/etc/init.d/iptables start
/etc/init.d/iptables stop
/etc/init.d/iptables restartnc (netcat) for TCP testing
# Send data to endpoint
nc 192.168.0.11 8000 < data.txt
# Listen and save incoming data
nc -l 8000 > received_data
# Keep listening
nc -lk 8000tcpdump
# Capture TCP packets on port 12301
tcpdump -i em1 tcp port 12301 -s 1500 -w abc.pcaptraceroute
# Default UDP
traceroute -I www.163.com # use ICMP
traceroute -M 3 www.163.com # start from TTL 3
traceroute -p 8080 192.168.10.11ss (socket statistics)
# List all listening ports
ss -l
# Show processes with 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 )'
# Show processes connecting to X server
ss -x src /tmp/.X11-unix/*
# Show socket statistics
ss -snetstat
# Count connections per IP and total
netstat -n | awk '/^tcp/ {n=split($(NF-1),array,":");if(n<=2)++S[array[1]];else++S[array[4]];++s[$NF];++N} END {for(a in S){printf("%-20s %s
", a, S[a]);++I}printf("%-20s %s
","TOTAL_IP",I);for(a in s) printf("%-20s %s
",a, s[a]);printf("%-20s %s
","TOTAL_LINK",N);}'Linux Performance Monitoring
top
Press uppercase F or O, then a‑z to sort by a column; press R to reverse the order.
dmesg
dmesgiostat (disk I/O)
iostat -xz 1free (memory usage)
free -msar (network throughput)
# Network device throughput
sar -n DEV 1
# TCP connection statistics
sar -n TCP,ETCP 1vmstat (CPU, memory, I/O)
# Sample every 2 seconds, once
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.
