Linux系统管理常用命令

1.循环遍历当前目录所有的文件名包含 html.php 的文件
[root@ me /]# find . | grep "html.php"

2.循环遍历当前目录所有的文件名包含 html.php 的文件并删除
[root@ me /]# find . | grep "html.php" | xargs rm -rf

3.循环遍历找出当前目录中所有后缀名为 .htm 的文件中 包含 hacker 字符的文件
[root@ me /]# grep 'hacker' find . | grep .htm

4.循环遍历找出当前目录中所有后缀名为 .htm 的文件并文件内容包含 hacker 字符的文件将里面的 the_old_string 用 the_new_string 进行批量替换
[root@ me /]# sed -i "s/10.10.0.191:15605/127.0.0.1:3306/g" grep "10.10.0.191:15605" -rl .

5.找出 /www/webroot/ 目录中文件类型为 html 的文件,并进行批量压缩
[root@ me /]# find /www/webroot/ -name "*.html" -type f -exec gzip {} \;

6.找出网站根目录 /var/webroot 中所有截至到现在被修改过的 php 文件,-name "*.php"为查找所有php文件 -time -10为截止到现在10天
[root@ me /]# find /var/webroot -name "*.php" -mtime -10

7.查找php木马有可能出现的关键字
[root@ me /]# find . -name "*.php" |xargs grep (eval|shell_exec|passthru|popen,system) |more

8.查找当前tcp/ip链接状态中,状态为 EST (一些攻击的特征,当EST状态数很多的时候,服务器的负载会特别大),并降序排列,显示前 100 个链接
[root@ me /]# netstat -an | grep -i ":80" |grep "EST"| awk '{print $5}'|cut -d : -f 1 \
| sort | uniq -c | sort -nr | awk '{if($1 > 100) {print $2}}'

9.关闭不需要的服务
[root@ me /]# for SERVICES in abrtd acpid auditd avahi-daemon cpuspeed haldaemon mdmonitor messagebus udev-post; do chkconfig ${SERVICES} off; done

10.用tcpdump嗅探80端口的访问看看谁最高
[root@ me /]# tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." \
'{print $1"."$2"."$3"."$4}' sort | uniq -c | sort -nr |head -20

11.察看哪个进程占用内存最大
[root@ me /]# ps -aux|sort -k5nr|awk 'BEGIN{print "PID VSZ"}{print $2,$5}'|awk 'NR<3'

12.查看80端口总共有多少个链接
[root@ me /]# netstat -nat|grep -i "80"|wc -l

13.查看80端口连接数最多的20个IP
[root@ me /]# netstat -anlp|grep 80|grep tcp|awk '{print $5}'|awk -F: '{print $1}' \
|sort|uniq -c|sort -nr|head -n20

14.用tcpdump嗅探80端口的访问看看谁最高
[root@ me /]# tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4}' sort | uniq -c | sort -nr |head -20

15.查找较多time_wait连接
[root@ me /]# netstat -n|grep TIME_WAIT|awk '{print $5}'|sort|uniq -c|sort -rn|head -n20

16.查找较多的SYN连接
[root@ me /]# netstat -an | grep SYN | awk '{print $5}' | awk -F: '{print $1}' | \
sort | uniq -c | sort -nr | more

17.对连接的IP按连接数量进行排序
[root@ me /]# netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n

18.查看TCP连接状态
[root@ me /]# netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn
[root@ me /]# netstat -n | awk '/^tcp/ {++S[$NF]};END {for(a in S) print a, S[a]}'
[root@ me /]# netstat -n | awk '/^tcp/ {++state[$NF]}; END {for(key in state) print key,"\t",state[key]}'
[root@ me /]# netstat -n | awk '/^tcp/ {++arr[$NF]};END {for(k in arr) print k,"\t",arr[k]}'
[root@ me /]# netstat -n |awk '/^tcp/ {print $NF}'|sort|uniq -c|sort -rn
[root@ me /]# netstat -ant | awk '{print $NF}' | grep -v '[a-z]' | sort | uniq -c

18. 查找 php 木马(webshell)文件
[root@ me /]# find ./ -name "*.php" |xargs egrep "phpspy|c99sh|milw0rm|eval\(gunerpress|eval\(base64_decoolcode|spider_bc"> /tmp/php.txt
[root@ me /]# grep -r --include=*.php '[^a-z]eval($_POST' . > /tmp/eval.txt
[root@ me /]# grep -r --include=*.php 'file_put_contents(.*$_POST\[.*\]);' . > /tmp/file_put_contents.txt
[root@ me /]# find ./ -name "*.php" -type f -print0 | xargs -0 egrep "(phpspy|c99sh|milw0rm|eval\(gzuncompress\(base64_decoolcode|eval\(base64_decoolcode|spider_bc|gzinflate)" | awk -F: '{print $1}' | sort | uniq

19.查找最近一天被修改的PHP文件
[root@ me /]# find -mtime -1 -type f -name \*.php

20.批量修改网站的权限
[root@ me /]# find -type f -name \*.php -exec chmod 444 {} \;
[root@ me /]# find ./ -type d -exec chmod 555{} \;

21.批量给文件转码
[root@ me /]# for f in ls *.jpg ; do mv $f ls $f|iconv -f GBK -t UTF-8; done

22.找出日志中出现的IP次数最多的那个
[root@ me /]# grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' /var/log/secure | sort | uniq -c | sort -nr

23.过滤日志中从一个时间段开始到另一个时间段,除开蜘蛛爬取页面以外,请求数最高的ip,并降序排列
sed -n '/2013:13/,/2013:15:/p' www.access.log | grep -v 'bot|crawler|slurp|spider|Spider' | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' | sort | uniq -c | sort -nr | head -n 20

1 Comment