jackadmi 2016-09-10
一、这篇文章讲了什么?
这篇文章参考性很强,希望以后多分享一些文章啦。所以就把草稿箱的其中一篇很实用性的Linux小技巧分享给运维的基友们。后续该篇文章会一直更新新技巧哟。感谢童鞋们的关注哈。
二、干货开始啦!
1.查看端口监听
[root@linuxidc ~]# netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a,S[a]}' CLOSE_WAIT 4 ESTABLISHED 343
2.查看端口监听状态统计
[root@Master ~]# netstat -aultn | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a,S[a]}' TIME_WAIT 28253 ESTABLISHED 15 LISTEN 13
可以看出当前系统有28253的端口在使用,
3.优化以上内核参数方式:
编辑/etc/sysctl.conf net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_fin_timeout = 30
修改完成后,sysctl -p 立即生效
4.修改流量大的web 服务器上,优化内核参数
net.ipv4.tcp_keepalive_time = 1200 net.ipv4.ip_local_port_range = 10000 65000 net.ipv4.tcp_max_syn_backlog = 8192 net.ipv4.tcp_max_tw_buckets = 5000 #这几个参数,建议只在流量非常大的服务器上开启,会有显著的效果。一般的流量小的服务器上,没有必要去设置这几个参数。
5.查看linux 系统默认网关
ip route show | grep 'default' | awk '{print $3}'
6.过滤文件中的注释以及空格开头
egrep -v '^$|^#' /etc/rsyslog.conf
7.如何清空/删除 last记录
#清空最后登录 echo >/var/log/wtmp #清空历史纪录 echo > ./.bash_history #在空格后输入命令,不记录到历史命令中,先输入到环境变量中 export HISTCONTROL=ignorespace
8.mysql密码忘记了,咋办?
vi /etc/my.cnf 添加下面一句话 [mysqld] skip-grant-tables 重启mysqld。 之后登陆mysql系统,修改root 的密码 USE mysql; UPDATE user SET Password = password ( 'new-password' ) WHERE User = 'root' ; flush privileges 以上就可以了。
9.利用tcpdump 监控某个接口上的端口,如80 端口,-i 后跟网络接口,CentOS6上是eth0。-c 是抓的包数目
[root@web ~]# tcpdump -i eno16777984 'port 80' -c 8 tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on eno16777984, link-type EN10MB (Ethernet), capture size 65535 bytes 23:41:42.142270 IP 192.168.30.65.27342 > web.ichunqiu.com.http: Flags [S], seq 779859144, win 8192, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 0 23:41:42.142360 IP web.ichunqiu.com.http > 192.168.30.65.27342: Flags [S.], seq 1569747590, ack 779859145, win 29200, options [mss 1460,nop,nop,sackOK,nop,wscale 7], length 0 23:41:42.142677 IP 192.168.30.65.27342 > web.ichunqiu.com.http: Flags [.], ack 1, win 256, length 0 23:41:42.142722 IP 192.168.30.65.27343 > web.ichunqiu.com.http: Flags [S], seq 2277286734, win 8192, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 0 23:41:42.142759 IP web.ichunqiu.com.http > 192.168.30.65.27343: Flags [S.], seq 3574925053, ack 2277286735, win 29200, options [mss 1460,nop,nop,sackOK,nop,wscale 7], length 0 23:41:42.143280 IP 192.168.30.65.27344 > web.ichunqiu.com.http: Flags [S], seq 2564439030, win 8192, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 0 23:41:42.143317 IP web.ichunqiu.com.http > 192.168.30.65.27344: Flags [S.], seq 4279749150, ack 2564439031, win 29200, options [mss 1460,nop,nop,sackOK,nop,wscale 7], length 0 23:41:42.143346 IP 192.168.30.65.27343 > web.ichunqiu.com.http: Flags [.], ack 1, win 256, length 0 8 packets captured 23 packets received by filter 0 packets dropped by kernel
10.统计web服务器站点的日志,如apache 的access.log日志
[root@elk ~]# awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head -n 25 5568 124.126.211.201 5247 123.125.2.108 4085 139.59.253.208
安装geoip软件 [root@elk nginx]# yum install GeoIP -y 查询ip归属地 [root@elk nginx]# cat access.log-20160903 | awk '{ print $1 }' | sort | uniq -c | sort -rn | head -n 25 | \awk '{ printf("%5d\t%-15s\t", $1, $2); system("geoiplookup " $2 " | cut -d \\: -f2 ") }' 5568 124.126.211.201 CN, China 5337 123.125.2.108 CN, China 4085 139.59.253.208 AU, Australia 1543 223.223.181.131 CN, China 80 93.174.93.99 NL, Netherlands 42 121.43.61.205 CN, China 26 36.110.44.114 CN, China 26 183.6.143.154 CN, China 25 116.226.39.94 CN, China 24 111.207.81.199 CN, China 24 106.81.231.217 CN, China 18 36.44.56.82 CN, China 18 112.80.61.114 CN, China 11 220.181.132.219 CN, China 5 61.141.94.68 CN, China 5 171.8.167.69 CN, China 4 101.226.33.240 CN, China 3 180.153.212.13 CN, China 3 180.153.206.20 CN, China 3 171.8.167.68 CN, China 3 101.226.89.119 CN, China 3 101.226.33.226 CN, China 3 101.226.102.97 CN, China 2 27.221.19.18 CN, China
[root@elk nginx]# cat /var/log/nginx/access.log | awk '($9 ~ /404/)' | awk '{ print $7 }' | sort | uniq -c | sort -rn | head -n 25
[root@elk nginx]# cat /var/log/nginx/access.log | awk -F\" '{ print $6 }' | sort | uniq -c |sort -frn | head -n 25
[root@elk nginx]# cat /var/log/nginx/access.log | awk '{ print $1 }' | sort | uniq -c | wc -l 122
[root@elk nginx]# cat /var/log/nginx/access.log | grep `date '+%d/%b/%G'` | awk '{ print $1 }' | sort | uniq -c | wc -l 10
[root@elk nginx]# cat access.log | grep `date '+%b/%G'` | awk '{ print $1 }' | sort | uniq -c | wc -l 111
[root@elk nginx]# cat /var/log/nginx/access.log | awk '{ print $9 }' | sort | uniq -c | sort -rn 8973 200 7304 401 720 304 79 404 49 400 10 502 10 "-" 7 201 4 499 4 409 4 173 1 405
[root@elk nginx]# cat /var/log/nginx/access.log | awk '{ print $7 }' | sort | uniq -c | sort -rn | head -n 25 4015 /elasticsearch/packetbeat-*/_field_stats?level=indices 3581 /favicon.ico 2117 /bundles/src/ui/public/images/elk.ico 799 / 340 /elasticsearch/logstash-*/_field_stats?level=indices 225 /elasticsearch/.kibana/_refresh 171 /app/kibana 167 /elasticsearch/_msearch?timeout=0&ignore_unavailable=true&preference=1472786252222 153 /elasticsearch/_msearch?timeout=0&ignore_unavailable=true&preference=1472784429830 144 /elasticsearch/_mget?timeout=0&ignore_unavailable=true&preference=1472746017033 139 /elasticsearch/.kibana/index-pattern/_search?fields= 132 /bundles/src/ui/public/images/kibana.svg 130 /bundles/kibana.bundle.js?v=10000 129 /bundles/node_modules/font-awesome/fonts/fontawesome-webfont.woff2 128 /bundles/commons.style.css?v=10000 128 /bundles/commons.bundle.js?v=10000 121 /bundles/kibana.style.css?v=10000 107 /elasticsearch/topbeat-*/_field_stats?level=indices 97 /elasticsearch/_mget?timeout=0&ignore_unavailable=true&preference=1472744909049 97 /elasticsearch/.kibana/visualization/_search?size=100 75 /elasticsearch/_mget?timeout=0&ignore_unavailable=true&preference=1472749929499 72 /elasticsearch/_mget?timeout=0&ignore_unavailable=true&preference=1472750405461 66 /elasticsearch/_mget?timeout=0&ignore_unavailable=true&preference=1472750160122 66 /elasticsearch/.kibana/dashboard/_search?size=100 62 /elasticsearch/_mget?timeout=0&ignore_unavailable=true&preference=1472743171471
[root@elk nginx]# tailf /var/log/nginx/access.log | awk '{ printf("%-15s\t%s\t%s\t%s\n", $1, $6, $9,$7) }' 60.191.52.254 "HEAD 401 http://115.236.176.134:3434/ 141.212.122.160 "GET 401 / 101.200.215.149 "GET 401 /manager/html 114.44.57.4 "CONNECT 400 126mx01.mxmail.netease.com:25 187.160.7.218 "GET 401 /cgi/common.cgi 187.160.7.218 "GET 401 /stssys.htm 187.160.7.218 "GET 401 / 187.160.7.218 "POST 401 /command.php 218.75.70.3 "GET 401 / 163.172.173.181 "GET 401 http://www.baidu.com/cache/global/img/gs.gif 123.125.2.108 "GET 200 / 123.125.2.108 "GET 200 /app/kibana 123.125.2.108 "GET 304 /bundles/commons.style.css?v=10000
[root@elk nginx]# tailf /var/log/nginx/access.log | awk '{ "geoiplookup " $1 " | cut -d \\: -f2 " | getline geo printf("%-15s\t%s\t%s\t%-20s\t%s\n", $1, $6, $9, geo, $7);}' 123.125.2.108 "GET 304 CN, China /bundles/src/ui/public/images/kibana.svg 123.125.2.108 "POST 200 CN, China /elasticsearch/_mget?timeout=0&ignore_unavailable=true&preference=1472922034187 123.125.2.108 "POST 200 CN, China /elasticsearch/.kibana/index-pattern/_search?fields= 123.125.2.108 "GET 200 CN, China /elasticsearch/.kibana/_mapping/*/field/_source?_=1472922034556 123.125.2.108 "POST 200 CN, China /elasticsearch/_mget?timeout=0&ignore_unavailable=true&preference=1472922034187 123.125.2.108 "GET 304 CN, China /bundles/node_modules/font-awesome/fonts/fontawesome-webfont.woff2
[root@elk nginx]# awk -F\" '($6 ~ /^-?$/)' /var/log/nginx/access.log | awk '{ print $1 }' | sort | uniq 114.44.57.4 183.129.160.229 218.75.70.3 61.231.3.98
11.linux中利用python开启临时的web下载服务,访问端口是88,http://ip:88/,端口可以自定义其它的,避免与系统其它的冲突
[root@web ~]# python -m SimpleHTTPServer 88
Serving HTTP on 0.0.0.0 port 88 ...
12.vim 复制多行内容
:1,10 co 10 意思是复制第1行到10行之间的内容到第10行后面。 :1,-1 co 10 意思是复制第1行到文件最后一行之间的内容到第10行后面。
13.快速删除文件内每行中的的空格
删除行首的空格
[root@hcloud webtest]# sed -i 's/^[ \t]*//g' filename
删除行尾的空格
[root@hcloud webtest]# sed -i 's/[ \t]*$//g' filename
14.windows 编写的脚本到linux 上运行,编码错误解决方法:
:set ff=unix :wq
15.yum 安装软件时,出现以下提示:
Resolving Dependencies There are unfinished transactions remaining. You might consider running yum-complete-transaction first to finish them.
解决方法:
[root@test ]# yum-complete-transaction
16.用grep 过滤文件中的空行已经#注释,并将显示出来的内容标上行号
grep -Ev '^$|^#' hosts >test.txt && cat -n test.txt && rm -f test.txt
17.假如server的某个进程的日志由于访问量或者其它错误bug导致日志大小暴增,且该日志分区空间即将不足,需要在线清空该日志(清理前先备份)
解决方法,有以下几种:
1)
cat /dev/null > logfile
2)
dd if=/dev/null of=logfile
3)
rm logfile
18.利用linux 自带工具实时检测网卡流量
1)ifconfig
[root@zabbix-6 ~]# watch -d ifconfig eth0
2)/proc/net/dev
[root@zabbix-6 ~]# watch -d cat /proc/net/dev
19.模拟cpu升高(可后台运行n次,每个进程的使用率*n 就是总的cpu利用率)
1)
dd if=/dev/zero of=/dev/null
2)
fulload() { dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null & }; fulload; read; killall dd
3)
yes > /dev/null &
4) 哈哈,这个慎用。。。。。!!!!
:(){ :|:& };:
20. 查看浏览器内部的缓存,谷歌为例,浏览器内输入以下内容:
chrome://net-internals/#dns
更新历史:
2016/09/04: 更新web 服务器的访问日志分析