常用命令备忘
www.zhaoch.top > 操作系统 > linux
修改ubuntu dns search domain
sudo apt-get install resolvconf
/etc/resolvconf/resolv.conf.d/base
search intra.aaaaa.com intra.bbbb.com
快速删除大量小文件
mkdir /tmp/empty
rsync --delete-before -d /tmp/empty/ /path/to/target/
stat
查看文件的详细信息,mtime,atime等,例如:
File: index.md
Size: 2392 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 2361081 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ zhaoch) Gid: ( 1000/ zhaoch)
Access: 2018-09-18 20:34:48.201967417 +0800
Modify: 2018-09-18 20:34:48.189967904 +0800
Change: 2018-09-18 20:34:48.189967904 +0800
Birth: -
svc
daemontools 的一个工具,全程是supervise 用于管理后台进程。可以通过命令控制进程退出,启动和重启(发送不同的信号)。通过这个程序启动服务可以以前台方式运行,不再需要自己fork两次或daemon()操作。
安装:apt-get install daemontools;sudo apt-get install daemontools-run
使用时在/etc/service/(根据配置可以不同)建立一个目录,目录内放置一个run的shell启动文件。svc会通过这个脚本启动或重启程序(注意这个脚本需要保证可执行权限)。例如:
~ cat /etc/service/mysvr/run
#!/bin/sh
export PYTHONPATH=/home/app/.local/lib64/python2.7/site-packages/
ulimit -c unlimited
ulimit -n 10240
exec setuidgid zhaoch sleep 10s
启动后会建立一些临时文件,如下
➜ ~ sudo tree /etc/service/mysvr/
/etc/service/mysvr/
├── run
└── supervise
├── control
├── lock
├── ok
└── status
1 directory, 5 files
启动示例 svc -u /etc/service/mysvr/
停止svc -d /etc/service/mysvr/
其他参数可以参考man page
mtr
在Linux中有一个更好的网络连通性判断工具,它可以结合ping nslookup tracert 来判断网络的相关特性
https://blog.csdn.net/xuguruogu/article/details/62417755
ip
代替 ifconfig 等命令,更好用。详见man page
Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }
ip [ -force ] -batch filename
where OBJECT := { link | address | addrlabel | route | rule | neigh | ntable |
tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm |
netns | l2tp | fou | macsec | tcp_metrics | token | netconf | ila |
vrf | sr }
OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |
-h[uman-readable] | -iec |
-f[amily] { inet | inet6 | ipx | dnet | mpls | bridge | link } |
-4 | -6 | -I | -D | -B | -0 |
-l[oops] { maximum-addr-flush-attempts } | -br[ief] |
-o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |
-rc[vbuf] [size] | -n[etns] name | -a[ll] | -c[olor]}
zstd
facebook近年新开发的压缩工具,压缩速度和压缩效果上比常用的gzip有较大的改善。
压缩速度
lz4 > zstd > gzip > bz2
压缩效果
bz2 > zstd > gzip > lz4
wine
linux 运行 exe程序
修改本地dns
修改文件 /etc/hosts
127.0.0.1 localhost
127.0.1.1 ubuntu
dig
查询dns
» dig www.zhaoch.top @8.8.8.8
; <<>> DiG 9.11.3-1ubuntu1.1-Ubuntu <<>> www.zhaoch.top @8.8.8.8
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 19650
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;www.zhaoch.top. IN A
;; ANSWER SECTION:
www.zhaoch.top. 299 IN A 202.162.108.57
;; Query time: 154 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Wed Sep 12 14:26:09 +08 2018
;; MSG SIZE rcvd: 59
host
查找ip对应的域名或域名对应的ip
➜ ~ host localhost
localhost.localdomain has address 127.0.0.1
localhost.localdomain has IPv6 address ::1
➜ ~ host 127.0.0.1
1.0.0.127.in-addr.arpa domain name pointer localhost.
strace
跟踪某个程序的系统调用
示例:
» pgrep -f 'nginx: worker' | xargs strace -p
Process 24236 attached - interrupt to quit
epoll_wait(15, {}, 512, 819) = 0
epoll_wait(15, {}, 512, 1000) = 0
epoll_wait(15, {}, 512, 1000) = 0
epoll_wait(15, {}, 512, 1000) = 0
epoll_wait(15, {}, 512, 1000) = 0
dpkg 操作
dpkg -i *.deb
dpkg -l nginx
dpkg -r nginx // 保留配置
dpkg -P nginx // 连同配置一起删除
pgrep, pkill
查找或杀死特定名称的进程,类似ps aux | grep xxx
或 ps aux | grep xxx | xargs kill
示例:
pgrep -f nginx
pkill -9 -f nginx
http 性能测试工具
ab 比较老旧的性能测试工具属于apache2-utils
,只能使用http1.0,只支持单线程
示例:
ab -v4 -n 200000 -c 500 -k http://127.0.0.1:8888/
- -v 指定日志级别
- -n 访问总数
- -c 并发数
- -k 开启http keep alive 功能
wrk 支持lua脚步的性能测试工具
示例:
./wrk -d 30 -c 900 -t 8 http://127.0.0.1:2080/
- -d 持续时间
- -c 连接数
- -t 线程数
https://github.com/wg/wrk
ssh 可以远程执行命令,设计通配符的时候需要使用单引号引起,例如
for i in `cat grp`; do echo $i; ssh root@$i 'zcat /log/error.log.*.gz | grep DIFF| grep "2018/08/29"' > $i.diff; done
stderr重定向到stdio
全部输出到某个文件
curl -vv www.baidu.com > baidu.log 2>&1
stderr不能直接grep,过度下
curl -vv www.baidu.com 2>&1 | grep HTTP
快速生成大文件
fallocate 最好用的
没有的时候可以 dd 使用 seek,skip可以更快的建立一个空洞文件
dd if=/dev/zero of=/tmp/test bs=1G count=6
curl
指定访问ip
curl --resolve www.baidu.com:443:127.0.0.1 https://www.baidu.com
post
mutipart 上传文件
curl -F "1111=@110055.body" https://www.baidu.com
url-encode 上传内容
curl -d "param1=value1¶m2=value2" "http://www.baidu.com"
ssh -A
带上公钥,在登录的机器上可以使用ssh登录其他的机器,更新git库等
删除旧文件
KEEP_DAYS=50
find ${GZ_DIR} -mtime +${KEEP_DAYS} -name "*.log.gz" -exec rm {} \;
- find + xargs 也可以实现类似功能但是不安全,容易误删
- 执行之前可以多用ls代替rm 测试几次再执行
- 尽可能详细指定文件名模式,减少误删可能
find 正则文件名称
- -regex 只能正则
- 写正则要包含路径内容,不是只有文件名
- -regextype可以指定正则兼容性
例如:
find ${TARGET_DIR} -regextype grep -regex '.*access-[0-9]\{10\}\.log\.gz'
参看json文件内容
利用python
cat test.json | python -m json.tool > format.json
cat test.json | python -m json.tool | less
jq
cat test.json | jq . > format.json
cat test.json | jq . | less
jq 支持更高级的用法,部分系统需要安装
apt-get install jq
, 更详细参见man页面
建立软链接
ln -s source target
awk 常用技巧
例如有如下访问日志
- 2018-09-05<SP>14:38:35 0.235 NONE - 85.10.199.185 85.10.199.185 - www.zhaoch.top /%E7%BC%96%E7%A8%8B GET - bypass - 301 886 - Mozilla/5.0<SP>(compatible;<SP>MJ12bot/v1.4.8;<SP>http://mj12bot.com/) - - - - - - - -
- 2018-09-05<SP>14:38:31 0.242 NONE - 85.10.199.185 85.10.199.185 - www.zhaoch.top /%E6%9D%82%E6%96%87 GET - bypass - 301 886 - Mozilla/5.0<SP>(compatible;<SP>MJ12bot/v1.4.8;<SP>http://mj12bot.com/) - - - - - - - -
- 2018-09-05<SP>14:38:33 0.244 NONE - 85.10.199.185 85.10.199.185 - www.zhaoch.top /%C3%A6%C2%9D%C2%82%C3%A6%C2%96%C2%87/ GET - miss - 404 6015 - Mozilla/5.0<SP>(compatible;<SP>MJ12bot/v1.4.8;<SP>http://mj12bot.com/) - - - - - - - -
- 2018-09-05<SP>14:38:39 0.232 NONE - 85.10.199.185 85.10.199.185 - www.zhaoch.top /%E7%BC%96%E7%A8%8B/go/ping-pong%E6%B5%8B%E8%AF%95%E5%A4%87%E5%BF%98.html GET - miss - 200 3574 - Mozilla/5.0<SP>(compatible;<SP>MJ12bot/v1.4.8;<SP>http://mj12bot.com/) - - - - - - - -
- 2018-09-05<SP>14:38:27 0.485 NONE - 85.10.199.185 85.10.199.185 - www.zhaoch.top /robots.txt GET - bypass - 404 10100 - Mozilla/5.0<SP>(compatible;<SP>MJ12bot/v1.4.8;<SP>http://mj12bot.com/) - - - - - - - -
- 2018-09-05<SP>14:38:29 0.267 NONE - 85.10.199.185 85.10.199.185 - www.zhaoch.top /%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/linux/%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4%E5%A4%87%E5%BF%98.html GET - miss - 200 6812 - Mozilla/5.0<SP>(compatible;<SP>MJ12bot/v1.4.8;<SP>http://mj12bot.com/) - - - - - - - -
- 2018-09-05<SP>14:38:37 0.240 NONE - 85.10.199.185 85.10.199.185 - www.zhaoch.top /%C3%A7%C2%BC%C2%96%C3%A7%C2%A8%C2%8B/ GET - miss - 404 6016 - Mozilla/5.0<SP>(compatible;<SP>MJ12bot/v1.4.8;<SP>http://mj12bot.com/) - - - - - - - -
- 2018-09-05<SP>14:57:32 0.005 NONE - 93.158.161.10 93.158.161.10 - www.zhaoch.top /robots.txt GET - bypass - 404 10100 - Mozilla/5.0<SP>(compatible;<SP>YandexBot/3.0;<SP>+http://yandex.com/bots) - - - - - - - -
- 2018-09-05<SP>14:57:36 0.233 NONE - 93.158.161.10 93.158.161.10 - www.zhaoch.top / GET - miss - 200 2692 - Mozilla/5.0<SP>(compatible;<SP>YandexBot/3.0;<SP>+http://yandex.com/bots) - - - - - - - -
打印某些字段
cat log| awk '{print $6" accessed https://"$9""$10}'
85.10.199.185 accessed https://www.zhaoch.top/%E7%BC%96%E7%A8%8B
85.10.199.185 accessed https://www.zhaoch.top/%E6%9D%82%E6%96%87
85.10.199.185 accessed https://www.zhaoch.top/%C3%A6%C2%9D%C2%82%C3%A6%C2%96%C2%87/
85.10.199.185 accessed https://www.zhaoch.top/%E7%BC%96%E7%A8%8B/go/ping-pong%E6%B5%8B%E8%AF%95%E5%A4%87%E5%BF%98.html
85.10.199.185 accessed https://www.zhaoch.top/robots.txt
85.10.199.185 accessed https://www.zhaoch.top/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/linux/%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4%E5%A4%87%E5%BF%98.html
85.10.199.185 accessed https://www.zhaoch.top/%C3%A7%C2%BC%C2%96%C3%A7%C2%A8%C2%8B/
93.158.161.10 accessed https://www.zhaoch.top/robots.txt
93.158.161.10 accessed https://www.zhaoch.top/
条件打印
cat log| awk '{if ($10=="/robots.txt")print $6" accessed https://"$9""$10}'
85.10.199.185 accessed https://www.zhaoch.top/robots.txt
93.158.161.10 accessed https://www.zhaoch.top/robots.txt
分类统计
cat log| awk '{arr[$6] += 1}END{for(i in arr)print i"\t"arr[i]}'
85.10.199.185 7
93.158.161.10 2
某个字段出现的次数
cat log| awk '{print $6}' | sort | uniq -c | sort -n
2 93.158.161.10
7 85.10.199.185
The End
- My github location
- View Source of this website GhostZch.github.io
- Commit issues to discuss with me and others