Operations 15 min read

Essential Linux Commands and Scripts for System Administration

A comprehensive cheat‑sheet of Linux system administration commands and scripts covering mounting Windows shares, monitoring HTTP concurrency, packet sniffing, log analysis, file system inspection, process metrics, boot sequence, link types, partition handling, user management, and networking utilities.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Essential Linux Commands and Scripts for System Administration

1. Mount a Windows share on Linux Use

mount.cifs //IP_ADDRESS/server /mnt/server -o user=administrator,password=123456

after creating the mount point.

2. View HTTP concurrent requests and TCP connection states Run

netstat -n | awk '/^tcp/ {++b[$NF]} END {for(a in b) print a,b[a]}'

. Use ulimit -n to check the max open file descriptors and adjust /etc/security/limits.conf if needed.

3. Capture top IPs accessing port 80 with tcpdump

tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4"."}' | sort | uniq -c | sort -nr | head -5

.

4. Count files in /var/log ls /var/log/ -1R | grep "-" | wc -l.

5. Show per‑IP connection counts

netstat -n | awk '/^tcp/ {print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -rn

.

6. Generate a 32‑character random password

cat /dev/urandom | head -1 | md5sum | head -c 32 >> /pass

.

7. Find top 5 IPs in Apache access.log

cat access.log | awk '{print $1}' | sort | uniq -c | sort -n -r | head -5

.

8. View binary file contents Use hexdump -C filename with options -c, -b, -o, -d, -x for different formats.

9. Meaning of VSZ and RSS in ps aux VSZ = virtual memory size, RSS = resident set size (physical memory used).

10. Check and repair a filesystem Run fsck /dev/hda5 to detect and fix inconsistencies.

11. Linux boot sequence BIOS → MBR → Bootloader → Kernel → init (runlevel) → rc scripts → local services → login.

12. Symbolic vs. hard links Symbolic links act like Windows shortcuts ( ln -s source link); hard links duplicate the inode ( ln source hardlink) and cannot cross filesystems.

13. Save current partition table dd if=/dev/sda of=./mbr.txt bs=1 count=512.

14. Basic vi/vim line operations Copy line: yy, paste: P; delete line: dd; delete all: dG; go to line 90: :90; search text: /pattern.

15. Install GRUB manually 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 Use firewall rule, e.g.,

iptables -A INPUT -d 172.16.100.1 -p tcp --dport 80 -m limit --limit 1/second -j ACCEPT

.

19. FTP active vs. passive mode Active (PORT) – client opens data port; Passive (PASV) – server opens data port.

20. Show commented lines in /etc/inittab grep "^#\{1,\}[^ ]" /etc/inittab.

21. Show lines containing a single digit surrounded by colons in /etc/inittab grep "\:[0-9]\{1\}:" /etc/inittab.

22. Add a script as a system service Include shebang, chkconfig header, then run chkconfig httpd --add and control with service httpd start|restart.

#!/bin/bash
#description: useradd
for i in `seq -f"%02g" 1 20`; do
  useradd user$i
  echo "user$i-`echo $RANDOM|md5sum|cut -c 1-5`"|passwd --stdin user$i >/dev/null 2>&1
done

23. Scan a /24 network for online hosts

#!/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
done
wait

24. Check a script for syntax errors and edit if needed

#!/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
fi

25. 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)" answer
    if [ "$answer" == "y" ]; then
      mkdir -p $dir && cd $dir
    else
      return 51
    fi
  }
  wget $url >/dev/null 2>&1 || return 52
}
download $url $dir
echo $?

26. Script to wipe a disk, create partitions, and format them

# Function to clean and partition /dev/sdb
Disk_Mod() {
  Sd=$1
  while :; do
    read -p "Warning!!! This operation will clean $Sd data. Next=y, Quit=n [y|n]:" Choice
    case $Choice in
      y) dd if=/dev/zero of=$Sd bs=512 count=1 &> /dev/null && break || return 67 ;;
      n) exit 88 ;;
      *) echo "Invalid choice, please choose again." ;;
    esac
  done
  echo -e "n
p
1

+100M
n
p
2

+1024M
w
" | fdisk $Sd &> /dev/null || return 68
  partprobe
  Part=$(fdisk -l $Sd | tail -2 | awk '{print $1}')
  for M in $Part; do
    mke2fs -j $M &> /dev/null || { ErrorPart=$M; return 69; }
  done
  return 0
}
Disk_Mod /dev/sdb
Res=$?
[ $Res -eq 0 ] && exit 0
[ $Res -eq 66 ] && echo "Error! Invalid input."
[ $Res -eq 67 ] && echo "Error! Command -> dd fdisk mke2fs"

27. Show command timestamps in history Add to profile: HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S"; export HISTTIMEFORMAT.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxScriptingcommandsFilesystem
MaGe Linux Operations
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.