Essential Linux Commands and Scripts for System Administration
A comprehensive collection of Linux commands and shell scripts covering mounting Windows shares, monitoring HTTP concurrency, network sniffing, log analysis, file system checks, boot sequence, link types, partition handling, FTP modes, and various automation tasks for effective system administration.
1. How to mount a Windows shared directory in Linux?
Use
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?
Run
netstat -n | awk '/^tcp/ {++b[$NF]} END {for(a in b) print a,b[a]}'. Use ulimit -n to check the maximum open file descriptors (default 1024) and adjust via /etc/security/limits.conf if needed.
3. How to sniff traffic on port 80 with tcpdump and list top sources?
Execute
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 in /var/log?
Use ls /var/log/ -1R | grep "-" | wc -l.
5. How to list connection counts per IP on a Linux system?
Run
netstat -n | awk '/^tcp/ {print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -rn.
6. How to generate a 32‑character random password in a shell?
Use
cat /dev/urandom | head -1 | md5sum | head -c 32 >> /pass.
7. How to find the top 5 IPs in Apache access.log?
Run
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 -C filename (or other options: -c for characters, -b for octal, -o for double‑byte octal, -d for decimal, -x for hexadecimal).
9. What do VSZ and RSS represent in ps aux ?
VSZ is the virtual memory size; RSS is the resident set size (physical memory used).
10. How to check and repair a Linux filesystem (e.g., /dev/hda5)?
Use fsck to detect and fix inconsistencies after power loss or disk issues.
11. What is the Linux boot sequence?
BIOS → MBR → boot loader → kernel → init (runlevel 3 or 5) → rc scripts → local services → login.
12. Difference between symbolic (soft) and hard links?
A symbolic link works like a Windows shortcut; a hard link creates another directory entry pointing to the same inode, cannot cross filesystems, and both names reflect changes.
13. How to save the current partition table?
Use dd if=/dev/sda of=./mbr.txt bs=1 count=512.
14. Common vi/vim text‑editing shortcuts in command mode.
Copy line: yy, paste: P Delete line: dd Delete all: dG (note uppercase G)
Go to line 90: :90 Search for a string: /path 15. How to install GRUB manually?
Run grub-install /dev/sda.
16. How to modify kernel parameters?
Edit /etc/sysctl.conf and apply with sysctl -p.
17. How to generate a random number between 1 and 39?
Use expr $[RANDOM%39] + 1.
18. How to limit Apache new connections per second?
Typically done with firewall rules, e.g.,
iptables -A INPUT -d 172.16.100.1 -p tcp --dport 80 -m limit --limit 1/second -j ACCEPT.
19. What are FTP active and passive modes?
Active (PORT) mode: client tells server which port to connect back to for data transfer. Passive (PASV) mode: server tells client which port to connect to for data transfer.
20. Show lines in /etc/inittab starting with # followed by whitespace and non‑whitespace characters.
Use grep "^#\{1,\}[^ ]" /etc/inittab.
21. Show lines in /etc/inittab containing a single digit between two colons.
Use grep "\:[0-9]\{1\}:" /etc/inittab.
22. How to add a script as a system service?
Include a shebang, chkconfig header, and description, then run chkconfig httpd --add to register the service.
23. Script to add 20 users (user01‑20) with random 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
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
done
wait25. Script to check syntax of another script and offer to edit.
[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
fi26. Script 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)" answer
if [ "$answer" == "y" ]; then
mkdir -p $dir && cd $dir
wget $url >/dev/null 2>&1
else
return 51
fi
}
[ $? -ne 0 ] && return 52
}
download $url $dir
echo $?27. Script to wipe a disk, create partitions, and format them with ext3.
#!/bin/bash
# Function to clean and partition /dev/sdb
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 || return 67 ;;
n) exit 88 ;;
*) echo "Invalid choice"; return 27 ;;
esac
echo -e "n
p
1
+100M
n
p
2
+1024M
w" | fdisk $dev || return 68
partprobe
for part in $(fdisk -l $dev | tail -2 | awk '{print $1}'); do
mke2fs -j $part >/dev/null || { ErrorPart=$part; 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! dd/fdisk/mke2fs failed."28. How to make the history command show timestamps?
Add the following to /etc/profile or your shell init file:
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.
