Summary of 40 commands necessary for Linux operation and maintenance

Posted by $Three3 on Mon, 29 Nov 2021 04:53:23 +0100

1. Delete 0 byte file

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_config   Sed 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 from it awk Use of)
killall -TERM mysqld
kill -9 `cat /usr/local/apache2/logs/httpd.pid`   Try the 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 ===                |
+--------------------------------------------------------------+
EOF

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
done

11. Get IP address

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

  perhaps

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

12. Size of memory

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

13. View port 80 to establish a connection

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

14. 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}'

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

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

who | wc -l

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

29. System log

# cat /var/log/rflogview/*errors

Check whether there are abnormal error records   You can also search for some exception keywords, such as:

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

30. Core log  

dmesg

Check whether there are abnormal error records.

31. System time  

date

Check whether the system time is correct.

32. Number of open files   

lsof | wc -l

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

33. 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).

34. Kill the processes related to port 80

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

35. Clear the dead process

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

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

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

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

38. Check how many PHP CGI processes are active

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

39. View the service started by the system

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

40. kudzu check the network card model

kudzu --probe --class=networ

Topics: Linux Operation & Maintenance Container