Common commands of Linux operation and maintenance engineers (Collection)

1. Delete 0 byte file

find -type f -size 0 -exec rm -rf {} ;

2. View process

Sort by memory from large to small

PS -e -o "%C : %p : %z : %a"|sort -k5 -nr

3. Ranked by CPU utilization from large to small

ps -e -o "%C : %p : %z : %a"|sort -nr

4. Print the URL in the cache

grep -r -a jpg /data/cache/* | strings | grep "http:" | awk -F'http:' '{print "http:"$2;}'

5. View the number of concurrent http requests and their TCP connection status:

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

6. sed in this article, the line of Root matches the line of Root and replaces no with yes

sed -i '/Root/s/no/yes/' /etc/ssh/sshd_config 

7. How to kill MySQL process

ps aux |grep mysql |grep -v grep  |awk '{print $2}' |xargs kill -9 
(Learn from it awk Use of)

killall -TERM mysqld

kill -9 `cat /usr/local/apache2/logs/`   #Try killing process PID

8. Show services enabled at run level 3:

ls /etc/rc3.d/S* |cut -c 15-   (Learn from it cut Purpose of intercepting data)

9. How to display multiple information when writing SHELL and use EOF

cat << EOF
|       === Welcome to Tunoff services ===                  |

10. Clever use of for (such as building soft links to MySQL)

cd /usr/local/mysql/bin
for i in *
do ln /usr/local/mysql/bin/$i /usr/bin/$i

11. Get IP address

ifconfig eth0 |grep "inet addr:" |awk '{print $2}'| cut -c 6-  


ifconfig | grep 'inet addr:'| grep -v '' | cut -d: -f2 | awk '{ print $1}'

12. Memory size:

free -m |grep "Mem" | awk '{print $2}'

netstat -an -t | grep ":80" | grep ESTABLISHED | awk '{printf "%s %sn",$5,$6}' | sort

14. To view the number of concurrent requests from Apache and its TCP connection status:

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

15. Because IT station Colleagues need to count the size of all jpg files under the server and write a SHELL for him to count. It was originally implemented with xargs, but he handled part at a time. There are multiple summations... The following commands can be solved.

find / -name *.jpg -exec wc -c {} ;|awk '{print $1}'|awk '{a+=$1}END{print a}'

The more CPUs (multiple CPUs, cat /proc/cpuinfo |grep -c processor), the lower the system load and the more requests can be processed per second.

16. CPU load

cat /proc/loadavg

Check whether the first three output values exceed 4 times of the system logical CPU.

17. CPU load

mpstat 1 1

Check if% idle is too low (e.g. less than 5%).

18. Memory space


Check whether the free value is too low. You can also use # cat /proc/meminfo

19. SWAP space


Check whether the swap used value is too high. If the swap used value is too high, further check whether the swap action is frequent:

vmstat 1 5

Observe whether the si and so values are large

20. Disk space

df -h

Check whether the partition utilization rate (Use%) is too high (e.g. more than 90%). If you find that the space of a partition is nearly exhausted, you can enter the mount point of the partition and Use the following command to find the file or directory that occupies the most space:

du -cks * | sort -rn | head -n 10

21. Disk I/O load

iostat -x 1 2

Check if I/O utilization (% util) exceeds 100%

22. Network load

sar -n DEV

Check whether the network traffic (rxbyt/s, txbyt/s) is too high

23. Network error

netstat -i

Check whether there is a drop fifo colls carrier. You can also use the command: # cat /proc/net/dev

24. Number of network connections

netstat -an | grep -E "^(tcp)" | cut -c 68- | sort | uniq -c | sort -n

25. Total number of processes

ps aux | wc -l

Check whether the number of processes is normal (for example, more than 250)

26. Number of runnable processes

vmwtat 1 5

Column shows the number of processes that can be run. Check whether it exceeds 4 times the logical CPU of the system

27. Process

top -id 1

Observe whether there are abnormal processes.

28. Network status, check whether DNS and gateway can be connected normally

ping traceroute nslookup dig 

29. Users

who | wc -l

Check whether there are too many login users (for example, more than 50). You can also use the command: # uptime.

30. System log

# cat /var/log/rflogview/*errors
 Check whether there are abnormal error records. You can also search for some abnormal keywords, such as:

grep -i error /var/log/messages
grep -i fail /var/log/messages

31. Core log


Check whether there are abnormal error records.

32. System time


Check whether the system time is correct.

33. Number of open files

lsof | wc -l

Check whether the total number of open files is too high.

34. Log

# logwatch –print
 to configure /etc/log.d/logwatch.conf,take Mailto Set as your own email Address, start mail service(sendmail perhaps postfix),In this way, you can receive log reports every day.
default logwatch Only yesterday's log can be reported # logwatch – print – range all obtain all log analysis results.
Can use # logwatch – print – detail high to get more specific log analysis results (not just error logs).
Copy code

35. Kill the processes related to port 80

lsof -i :80|grep -v "ID"|awk '{print "kill -9",$2}'|sh

36. Clear the dead process

ps -eal | awk '{ if ($2 == "Z") {print $4}}' | kill -9

37. tcpdump packet capture is used to prevent port 80 from being attacked by people. It can analyze data

tcpdump -c 10000 -i eth0 -n dst port 80 > /root/pkts

38. Then check the number of IP duplicates and sort them from small to large. Note that there are two spaces in the middle of "- t +0"

# less pkts | awk {'printf $3"n"'} | cut -d. -f 1-4 | sort | uniq -c | awk {'printf $1" "$2"n"'} | sort -n -t +0

39. Check how many PHP CGI processes are active

netstat -anp | grep php-cgi | grep ^tcp | wc -l

40. View the self started services of the system

chkconfig --list | awk '{if ($5=="3:on") print $1}'

41. kudzu check the network card model

kudzu --probe --class=network

Necessary common commands for Senior Linux operation and Maintenance Engineer (common interview questions)

Common regular expressions

1. Regular expression matching Chinese characters: [u4e00-u9fa5]

Comment: matching Chinese is really a headache. It's easy to do with this expression

2. Matching double byte characters (including Chinese characters): [^ X00 XFF]

Comment: can be used to calculate the length of a string (a double byte character length meter 2, ASCII character meter 1)

3. Regular expression matching blank lines: ns*r

Comment: can be used to delete blank lines

4. Regular expression matching HTML tag: < (s *?) [^>]*>.*?</ 1>|<.*? />

Comment: the version circulated on the Internet is too bad. The above one can only match the part. There is still nothing to do with complex nested tags

5. Regular expression matching leading and trailing white space characters: ` ^ s | s < div

Comment: it can be used to delete white space characters at the beginning and end of a line (including spaces, tabs, page breaks, etc.), which is a very useful expression

6. Regular expression matching Email address:


Comment: form validation is very practical

7. Regular expression matching URL: [a-zA-z]+://[^s]*

Commentary: the functions of the version circulated on the Internet are very limited, and the above can basically meet the needs

8. Whether the matching account number is legal (starting with a letter, 5-16 bytes are allowed, and alphanumeric underscores are allowed): ` ^ a-za-z {4,15} < div

Comment: form validation is very practical

9. Matching domestic telephone number: d{3}-d{8}|d{4}-d{7}

Comment: the matching form is 0511-4405222 or 021-87888822

10. Match Tencent QQ number: [1-9] [0-9] {4,}

Comment: Tencent QQ starts from 10000

11. Matching China Postal Code: [1-9]d{5}(?!d)

Commentary: the postal code of China is 6 digits

12. Matching ×××: d{15}|d{18}

Commentary: China's ××× 15 or 18 bits

13. Matching ip address: D + d+. d+. d+

Comment: useful when extracting IP addresses

14. Match specific numbers:

^[1-9]d*$   //Match positive integer
^-[1-9]d*$  //Match negative integer
^-?[1-9]d*$  //Match integer
^[1-9]d*|0$ //Match non negative integer (positive integer + 0)
^-[1-9]d*|0$  //Match non positive integers (negative integers + 0)
^[1-9]d*.d*|0.d*[1-9]d*$  //Match positive floating point number
^-([1-9]d*.d*|0.d*[1-9]d*)$ //Match negative floating point number
^-?([1-9]d*.d*|0.d*[1-9]d*|0?.0+|0)$ //Match floating point number
^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$  //Match non negative floating point number (positive floating point number + 0)
^(-([1-9]d*.d*|0.d*[1-9]d*))|0?.0+|0$ //Match non positive floating point number (negative floating point number + 0)

Commentary: it is useful when dealing with a large amount of data. Pay attention to correction in specific application

15. Match a specific string:

^[A-Za-z]+$ //Matches a string of 26 English letters
^[A-Z]+$ //Matches a string of 26 uppercase letters
^[a-z]+$ //Matches a string of 26 lowercase letters
^[A-Za-z0-9]+$ //Matches a string of numbers and 26 English letters
^w+$ //Matches a string consisting of numbers, 26 English letters, or underscores

Commentary: some of the most basic and commonly used expressions

Tags: Operation & Maintenance

Posted by BZorch on Sat, 07 May 2022 23:06:49 +0300