Essential Linux Command-Line Tricks Every Sysadmin Should Know
This guide compiles 28 practical Linux commands and shell scripts covering file system mounting, network monitoring, process inspection, password generation, log analysis, link handling, boot sequence, FTP modes, and service management, providing concise examples for everyday system administration tasks.
1. Mount a Windows share in Linux Use
mount.cifs //IP_ADDRESS/server /mnt/server -o user=administrator,password=123456(replace IP and credentials with the Windows host values).
2. View HTTP concurrent requests and TCP connection states
netstat -n | awk '/^tcp/ {++b[$NF]} END {for(a in b) print a,b[a]}'Check the maximum number of open file descriptors with ulimit -n (default 1024). To increase, edit /etc/security/limits.conf and add:
* soft nofile 10240 * hard nofile 10240Reboot for changes to take effect.
3. Use tcpdump to capture the top IPs accessing port 80
tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4"."}' | sort | uniq -c | sort -nr | head -54. Count files in /var/log ls /var/log/ -1R | grep "-" | wc -l 5. Count connections per IP on a Linux system
netstat -n | awk '/^tcp/ {print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -rn6. Generate a 32‑character random password in a shell
cat /dev/urandom | head -1 | md5sum | head -c 32 >> /pass7. Find the top 5 IPs in Apache access.log
cat access.log | awk '{print $1}' | sort | uniq -c | sort -n -r | head -58. View the contents of a binary file Use hexdump with appropriate flags, e.g. hexdump -C file for canonical hex+ASCII output. Other useful options: -c (char), -b (octal), -o (two‑byte octal), -d (two‑byte decimal), -x (two‑byte hex).
9. Meaning of VSZ and RSS in ps aux VSZ = virtual memory size of the process; RSS = resident set size (actual physical memory used).
10. Check and repair a filesystem (e.g., /dev/hda5) Use fsck /dev/hda5 to detect and fix inconsistencies.
11. Linux boot sequence BIOS → MBR → Boot loader → Kernel → init (runlevel from inittab, typically 3 for multi‑user CLI or 5 for GUI) → rc.sysinit scripts → /etc/rc.d/rc.local → login prompt.
12. Difference between symbolic (soft) and hard links A symbolic link ( ln -s source link) works like a Windows shortcut and can span filesystems; deleting the target breaks the link. A hard link ( ln source hardlink) creates another directory entry for the same inode, cannot cross filesystems, and both names remain valid if one is removed.
13. Save the current partition table dd if=/dev/sda of=./mbr.txt bs=1 count=512 14. Common vi/vim editing shortcuts Copy line: yy, paste: p; delete line: dd; delete all: dG; go to line 90: :90; search string: /path.
15. Manually install GRUB grub-install /dev/sda 16. Modify kernel parameters Edit /etc/sysctl.conf then apply with sysctl -p.
17. Generate a random number between 1 and 39 expr $[RANDOM%39] + 1 18. Limit Apache new connections per second (example using iptables)
iptables -A INPUT -d 172.16.100.1 -p tcp --dport 80 -m limit --limit 1/second -j ACCEPT19. FTP active vs passive mode Active (PORT): client tells server which port to connect back to for data. Passive (PASV): server tells client which port to connect to; data connection is initiated by the client.
20. Show lines in /etc/inittab that start with # followed by whitespace and then non‑whitespace grep "^#\{1,\}[[:space:]]\+[^[:space:]]" /etc/inittab 21. Show lines in /etc/inittab containing a single digit between two colons grep "\:[0-9]\{1\}:" /etc/inittab 22. Add a script to system services (use service ) Include at the top of the script:
#!/bin/bash
# chkconfig: 345 85 15
# description: httpdThen run chkconfig httpd --add and control with service httpd start|restart.
23. Script to add 20 users (user01‑user20) with random 5‑character passwords
#!/bin/bash
for i in `seq -f "%02g" 1 20`; do
useradd user$i
echo "user$i-`echo $RANDOM|md5sum|cut -c1-5`" | passwd --stdin user$i >/dev/null 2>&1
done24. Script to ping all hosts in 192.168.1.0/24 and report status
#!/bin/bash
for ip in `seq 1 255`; do
ping -c 1 192.168.1.$ip >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "192.168.1.$ip UP"
else
echo "192.168.1.$ip DOWN"
fi
done25. Script to check another script for syntax errors and prompt user
#!/bin/bash
read -p "please input check script-> " file
if [ -f $file ]; then
sh -n $file >/dev/null 2>&1
if [ $? -ne 0 ]; then
read -p "You input $file syntax error, [Type q to exit or Type vim to edit]" answer
case $answer in
q|Q) exit 0 ;;
vim) vim $file ;;
*) exit 0 ;;
esac
fi
else
echo "$file not exist"
exit 1
fi26. Function to download a file to a directory with error handling
#!/bin/bash
url=$1
dir=$2
download() {
cd $dir >/dev/null 2>&1 || {
read -p "$dir No such file or directory, create? (y/n)" ans
if [ "$ans" = "y" ]; then
mkdir -p $dir && cd $dir
else
return 51
fi
}
wget $url >/dev/null 2>&1 || return 52
}
download $url $dir
echo $?27. Script to wipe a disk, create partitions, and format them (error‑coded)
#!/bin/bash
Disk_Mod() {
local dev=$1
read -p "Warning! This will erase $dev. Continue? (y/n)" choice
case $choice in
y) dd if=/dev/zero of=$dev bs=512 count=1 &>/dev/null || return 67 ;;
n) exit 88 ;;
*) echo "Invalid choice"; return 66 ;;
esac
echo -e "n
p
1
+100M
n
p
2
+1024M
w
" | fdisk $dev &>/dev/null || return 68
partprobe
for part in $(fdisk -l $dev | tail -2 | awk '{print $1}'); do
mkfs.ext3 $part &>/dev/null || { ErrorPart=$part; return 69; }
done
return 0
}
Disk_Mod /dev/sdb
res=$?
case $res in
0) echo "Success" ;;
66) echo "Invalid input" ;;
67) echo "dd/fdisk/mkfs error" ;;
68) echo "Partition creation error" ;;
69) echo "Formatting error on $ErrorPart" ;;
*) echo "Unknown error" ;;
esac28. Show timestamps in command history Add to /etc/profile or current shell:
HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S"
export HISTTIMEFORMATSigned-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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
