Summary of 40 commands necessary for Linux operation and maintenance

Time:2021-9-25

1. Delete 0 byte file
Linux命令

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

2. View process

Sort by memory size

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

3. Sorted 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. To 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 -i '/Root/s/no/yes/' /etc/ssh/sshd_configSed in this article, the line of root matches the line of root and replaces no with yes.

7. How to kill MySQL process

PS aux | grep MySQL | grep - V grep | awk '{print $2}' | xargs kill - 9 (learn the purpose of awk) kill - term mysqldkill - 9 ` cat / usr / local / apache2 / logs / httpd. PID ` try to kill the process PID

8. Show services enabled at run level 3:

Ls / etc / RC3. D / s * |cut - C 15 - (learn the purpose of cut and intercept data)

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

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

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

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

11. Get IP address

Ifconfig eth0 | grep "INET addr:" | awk '{print $2}' | cut - C 6 - or ifconfig | grep 'INET addr:' | grep - V '127.0.0.1' | cut - D: - F2 | awk '{print $1}'

12、Memory size:

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

13

netstat -an -t | grep ":80" | grep ESTABLISHED | awk '{printf "%s %s\n",$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 my colleague wanted to count the size of all JPG files under the server, he wrote 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}'

Number of CPUs (multiple CPUs,cat /proc/cpuinfo |grep -c processor)The more, 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

free

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

19. Swap space

free

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 (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 network error (drop FIFO Colls carrier), or 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 runnable processes. Check whether they exceed 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

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/messagesgrep -i fail /var/log/messages

31. Core log

dmesg

Check whether there are abnormal error records.

32. System time

date

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

Configure / etc / log.d/logwatch.conf, set mailto as your email address, and start the mail service (sendmail or postfix), so that you can receive log reports every day.

The default logwatch only reports yesterday’s logs. You can use # logwatch – print – range all to obtain all log analysis results.

You can use # logwatch – print – detail high to get more specific log analysis results (not just error logs).

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

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:\n\s*\r

Comment: can be used to delete blank lines

4. Regular expressions matching HTML Tags:]*>.*?|

Comment: the version circulated on the Internet is too bad. The above one can only match the part. It is still powerless for complex nested tags

5. Regular expression matching leading and trailing white space characters:^\s*|\s*$

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:

\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

Comment: form validation is useful

7. Regular expression matching URL of web address:[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][a-zA-Z0-9_]{4,15}$

Comment: form validation is useful

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 No.:[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 integer (negative integer + 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 numbers ^ [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 numbers (negative floating point numbers + 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] + $// match the string composed of 26 English letters ^ [A-Z] + $// match the string composed of 26 English letters in uppercase ^ [A-Z] + $// match the string composed of 26 English letters in lowercase ^ [a-za-z0-9] + $// match the string composed of numbers and 26 English letters ^ \ W + $// match the string composed of numbers, 26 English letters or underscores

The above isLiangxu tutorial networkSummary of 40 commands necessary for Linux operation and maintenance shared by all friends.

This article is composed of blog one article multi posting platformOpenWriterelease!

Recommended Today

Supervisor

Supervisor [note] Supervisor – H view supervisor command help Supervisorctl – H view supervisorctl command help Supervisorctl help view the action command of supervisorctl Supervisorctl help any action to view the use of this action 1. Introduction Supervisor is a process control system. Generally speaking, it can monitor your process. If the process exits abnormally, […]