Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What practical skills do Linux operators need to master?

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

What this article shares with you is about the practical skills that Linux operators need to master. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article. Let's take a look at it with the editor.

1. Find all files ending in .tar in the current directory and move to the specified directory:

Find. -name "* .tar"-exec mv {}. / backup/

Note: find-name is mainly used to find a file name,-exec and xargs can be used to undertake the previous results, and then the actions to be performed are generally used with find. Find uses us to extend-mtime to find the modification time,-type is the specified object type (including f for file and d for directory), and-size specifies the size. For example, often used: find the current directory 30 days ago greater than 100m LOG files and delete.

Find. -name "* .log"-mtime + 30-typef-size + 100m | xargs rm-rf {}

2. Batch decompress all files ending with .zip in the current directory to the specified directory:

For i in `find. -name "* .zip"-type f `do unzip-d $I / data/www/img/ done

Note: forI in (command); do... Done is a common format of the for loop, where I is a variable and can be specified by yourself.

3. Sed common life collection: test.txt for testing

How to remove the head of the line. Characters:

Sed-i 's / ^. / / g' test.txt

Add an a character at the beginning of the line:

Sed's/ ^ / a Compact g 'test.txt

Add an a character at the end of the line:

Sed's/$/a/' tets.txt

Add a c character after a specific line

Sed'/ wuguangke/ac' test.txt

Add a c character before the line

Sed'/wuguangke/ic' test.txt

Please refer to the documentation for more sed commands.

4, how to judge whether a directory exists, if it does not exist, it will be newly built, and if it exists, the information will be printed.

If [!-d / data/backup/]; then Mkdir-p / data/backup/ else echo "The Directory alreadyexists,please exit" fi

Note: if... ; then... Else.. fi: for if conditional statement,! An exclamation mark means "does not exist", and-d represents a directory.

5. Monitor the root partition of linux disk. If the root partition space is greater than or equal to 90%, send email to Linux SA.

(1) print root partition size

Df-h | sed-n'/ / $/ p' | awk'{print $5}'| awk-F "%"'{print $1}'

Note: awk'{print $5} 'means to print the fifth field,-F means to separate, for example, separated by%, simply means to remove the percent sign, awk-F.' {print $1} 'separation point. No.

(2) the if condition determines whether the size is greater than 90, and sends an email alarm if it is greater than 90.

While sleep 5m do for i in `then echo-h | sed-n'/ / $/ p' | awk'{print $5}'| sed's Linux of disk space% do echo $I if [$I-ge 90]; then echo "More than 90% Linux of disk space, Please LinuxSA Check Linux Disk!" | mail-s "Warn Linux / Parts is $I%" XXX@XXX.XX fi done done

6. Count the Nginx access log, and the top 20 ip addresses in terms of traffic:

Cat access.log | awk'{print $1}'| sort | uniq-c | sort-nr | head-20

Comments: sort sorting, uniq (check and delete repeated rows in text files)

7. Another usage of sed finds the current line, and then modifies the parameters after the line:

Sed-I'/ SELINUX/s/enforcing/disabled/' / etc/selinux/config

The Sed colon sed-I 's:/tmp:/tmp/abc/:g'test.txt means to change / tmp to / tmp/abc/.

8. Print out the maximum and minimum values in a file:

Cat a.txt | sort-nr | awk'{} END {print} NR==1' cat a.txt | sort-nr | awk 'END {print} NR==1'

This is the real maximum and minimum value of printing: sed's / / / g 'a.txt | sort-nr | sed-n' 1p

9. Use snmpd to grab cacti data of version v2:

Snmpwalk-V2C-c public 192.168.0.241

10. Modify the text ending with jk and replace it with yz:

Sed-e's Universe JKG b.txt

11. Network packet capture: tcpdump

Tcpdump-nn host 192.168.56.7 and port 80 crawls packets requested by 56.7 through 80. Tcpdump-nn host 192.168.56.7 or! Host 192.168.0.22 and port 80 excludes port 0.22 80! Tcp/ip layer 7 protocol physical layer-data link layer-network layer-transport layer-session layer-presentation layer-application layer.

12. Display the 20 most commonly used commands:

Cat .bash _ history | grep-v ^ # | awk'{print $1}'| sort | uniq-c | sort-nr | head-20

13. Write a script to find files that were last created 3 days ago with the suffix * .log and delete them.

Find. -mtime + 3-name "* .log" | xargs rm-rf {}

14. Write a script to move files greater than 100k in a directory to / tmp.

Find. -size + 100k-exec mv {} / tmp

15. Write a firewall configuration script that only allows remote hosts to access port 80 of the machine.

Iptables-F iptables-X iptables-An INPUT-p tcp-- dport 80-j accept iptables-An INPUT-p tcp-j REJECT

Or

Iptables-An INPUT-m state-- state NEW-m tcp-p tcp-- dport 80-j ACCEPT

16. Write a script for nginx log statistics, and get the top 10 visits to ip (nginx log path:

/ home/logs/nginx/default/access.log). Cd / home/logs.nginx/default sort-m-k 4-o access.logok access.1 access.2 access.3. Cat access.logok | awk'{print $1}'| sort-n | uniq-c | sort-nr | head-10

17. Replace the directory in the file

Sed's VuGR test.txt UserUserUserUserLog UserVERGULAR

Or

Sed-I's test.txt test.txt and above are the practical skills that Linux operators need to master. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report