Operations 16 min read

Essential Linux Commands and Scripts for System Administration

A comprehensive collection of Linux commands, shell one‑liners, and multi‑line scripts covering file sharing, network monitoring, process inspection, disk management, boot sequence, link types, user creation, FTP modes, and service automation to help sysadmins work efficiently.

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

1. How to mount a Windows shared directory on Linux?

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

(create the mount point manually; the user and password correspond to the Windows account).

2. How to view HTTP concurrent request count and TCP connection states?

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

Use ulimit -n to see the maximum number of open file descriptors (default 1024). To increase it, edit /etc/security/limits.conf and add:

* soft nofile 10240
* hard nofile 10240

then reboot.

3. How to sniff traffic on port 80 and list the top IPs?

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

4. How to count files under /var/log ? ls /var/log/ -1R | grep "-" | wc -l 5. How to list connection counts per IP on a Linux system?

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

6. Generate a 32‑character random password in the shell.

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

7. Find the top 5 IPs with most accesses in Apache access.log .

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

8. How to view the contents of a binary file? Use hexdump with appropriate flags, e.g., hexdump -C file for canonical hex+ASCII, -c for characters, -b for octal bytes, -o for octal words, -d for decimal, -x for hex.

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

10. How to check and repair /dev/hda5 ? Use fsck to examine and fix filesystem inconsistencies after power loss or disk errors.

11. Linux boot sequence overview. BIOS → MBR → Boot loader → Kernel → init (runlevel 3 or 5) → rc.sysinit → kernel modules → run‑level scripts → /etc/rc.d/rc.local/bin/login.

12. Difference between symbolic (soft) and hard links. A symbolic link works like a Windows shortcut; a hard link is an additional directory entry pointing to the same inode (cannot cross filesystems). Deleting the original file does not affect a hard link, but breaks a symbolic link.

13. Save the current partition table. dd if=/dev/sda of=./mbr.txt bs=1 count=512 14. Basic vi/vim line operations. Copy line: yy then 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 to 1 new connection per second, peak 3. Use iptables:

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 and server connects back; Passive (PASV) – server opens data port and client connects to it. Control channel is the same; data channel differs.

20. Show lines in /etc/inittab that start with # followed by whitespace and then a non‑whitespace character. grep "^#\{1,\}[^ ]" /etc/inittab 21. Show lines in /etc/inittab containing a single digit surrounded by colons. grep "\:[0-9]\{1\}:" /etc/inittab 22. Add a script to system services (use service to control). Add at the top of the script:

#!/bin/bash
# chkconfig: 345 85 15
# description: httpd

Then run chkconfig httpd --add and control with service httpd start|restart.

23. Script to add 20 users (user01‑20) with random 5‑character passwords.

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

24. Script to ping all hosts in 192.168.1.0/24 and report up/down.

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

25. Script to check another script for syntax errors and prompt the user.

[root@localhost tmp]# cat checksh.sh
#!/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

26. Script function to download a file to a directory, creating the directory if needed and returning specific error codes.

#!/bin/bash
url=$1
dir=$2
download() {
  cd $dir >>/dev/null 2>&1
  if [ $? -ne 0 ]; then
    read -p "$dir No such file or directory, create? (y/n)" answer
    if [ "$answer" == "y" ]; then
      mkdir -p $dir && cd $dir
      wget $url >/dev/null 2>&1
    else
      return 51
    fi
  fi
  if [ $? -ne 0 ]; then
    return 52
  fi
}
download $url $dir
echo $?

27. Script to wipe a disk, create two partitions (100M and 1G), format them as ext3, and return error codes. (Core commands shown; full script omitted for brevity.) Use dd if=/dev/zero of=$Sd bs=512 count=1, fdisk with echo‑pipe, partprobe, then mke2fs -j on each partition. Return 67, 68, 69 on failure, 0 on success.

28. Show timestamps in Bash 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.

ShellNetworkingSysadmin
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.