目录

常用命令收集

目录

引入一个更为专业的命令收集站点 :

https://www.commandlinefu.com/commands/browse

统计第一列相同,第二列平均值

1
cat xxx |awk '{a[$1]+=$2;c[$1]++}END{l=asorti(a,b);for(i=1;i<=l;i++)print b[i],a[b[i]]/c[b[i]]}'

时间段统计日志:

1
2
sed -n '/2018:02:30/,/2018:03:00/p' www.log |awk '{a[$1]+=1;} END {for(i in a){print a[i]" "i;}}' |sort -t " " -k 1 -n
sed -n '/2018:01:50/,/2018:02:00/p' www.log |grep "list?" |awk '{a[$1]+=1;} END {for(i in a){print a[i]" "i;}}' |sort -t " " -k 1 -n

按照 ip 排序

1
2
3
4
# 升序
sort -t'.' -k1,1n -k2,2n -k3,3n -k4,4n ip.txt
# 降序
sort -t'.' -k1,1nr -k2,2nr -k3,3nr -k4,4nr ip.txt

shell 中获取脚本绝对路径

1
2
SHELL_DIR=$(dirname $(readlink -f "$0"))
SHELL_DIR=$(cd `dirname $0`; pwd)

tailf 显示高亮

1
2
3
 tail -f www.log | perl -pe 's/(\/pattern1\/pattern2)/\e[1;31m$1\e[0m/g'

 tail -f www.log |grep --color -E 'pattern|$'

openssl 通过证书加密解密大文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
mkdir /etc/encrypt && cd /etc/encrypt

openssl genrsa -out private.pem 2048
openssl rsa -in private.pem -outform PEM -pubout -out public.pem
openssl rand -base64 32 > key.bin   # 远程传输建议每次都新建

# 加密
openssl rsautl -encrypt -pubin -inkey /etc/encrypt/public.pem -in /etc/encrypt/key.bin -out /etc/encrypt/key.bin.enc
openssl aes-256-cfb -a -pbkdf2 -salt -in  filename.gz -out filename.gz.enc -k $(cat /etc/encrypt/key.bin)

# 解密
openssl rsautl -decrypt -inkey private.pem -in key.bin.enc -out key.bin
openssl aes-256-cfb -d -a -pbkdf2 -in filename.gz.enc -out filename.gz -k $(cat key.bin)


# 加密
tar -czf - * |  openssl aes-256-cfb -salt -k "8CASiU6zxAWy9QZ8wj+MgIzqHsBnXjgkHNvWeJ0urHw=" -out ssh.key.pem.enc

openssl aes-256-cfb -d -salt -in ssh.key.pem.enc -out ssh_key.pem.tar.gz

免密登陆

1
2
3
4
5
6
7
8
9
# 172.16.10.11 无密码登陆 172.16.10.12
# 172.16.10.11 生成秘钥
# rsa 默认 2048
ssh-keygen -t rsa -b 2048
#  然后一直回车,可以设置认证密码
# id_rsa 私钥(不要外传)
# id_rsa.pub (公钥导入本地authorized_keys(600)中,将私钥传给客户端,客户端即可通过私钥连接当前服务器)
# 将本地的秘钥复制到服务器上就可以了,或者拷贝追加到服务器的authorized_keys文件中,即可本地登陆远程主机
ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.10.12

手动检测: 每分钟连接次数

1
2
netstat -ntu | awk '{print $5}' |cut -d: -f1|sort|uniq -c |sort -n
netstat -an |grep ^tcp.*:80|egrep -v 'LISTEN|127.0.0.1'|awk -F"[ ]+|[:]" '{print $6}'|sort|uniq -c|sort -rn

linux 用 tcpdump 查看 80 端口访问有哪些 IP

1
tcpdump -i eth0 -tnn dst port 80 -c 1000|awk -F"." '{print $1"."$2"."$3"."$4}'|sort|uniq -c|sort -rn|head -n20

查看 linux 内存占用最高的 10 个进程

1
ps aux|head -1 && ps aux|grep -v PID|sort -rn -k +4|head

linux 查看 cpu 占用最高的 10 个进程

1
ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head

linux 查看命令来源于那个包(yum 也适用)

1
dnf provides htop

linux 查看已安装的命令原来于那个包

1
rpm -qf /usr/bin/htop

linux 查看 rpm 包信息

1
rpm -qpi xxx.rpm

linux 查看 rpm 包内容

1
rpm -qpl xxx.rpm

linux 查看 rpm 包依赖

1
rpm -qpR xxx.rpm

linux 查看 rpm 包带的执行脚本

1
rpm -qp --scripts xxx.rpm

linux 自动安装 rpm 包依赖(dnf 默认已存在该功能)

1
yum -y localinstall xxx.rpm

linux rpm 循环安装包依赖

1
2
# 关于循环安装是指的是主rpm包的所有的依赖包在同一目录下,会自动安装其依赖后在安装主rpm包,(此方法缺陷较大)
rpm -ivh --aid  *.rpm

清除僵死进程

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

LNMP/LAMP 环境查看编译参数

1
2
3
4
5
6
7
8
# nginx
/pathto/nginx/sbin/nginx -V
# apache
/pathto/apache/build/config.nice
# mysql
grep CONFIGURE_LINE /usr/bin/mysqlbug
# php
/pathto/php/bin/php -i|grep configure

让不同的进程使用不同的 cpu

1
2
# taskset -c,--cpu-list command
taskset -c 0,1,2,3 /etc/init.d/mysql start

watch 监测命令运行结果

1
2
3
4
# 类似tailf,但是针对命令
# 查看当前目录内容变化
# watch ls
# watch "netstat -ntu | awk '{print $5}' |cut -d: -f1|sort|uniq -c |sort -n"

创建一个具有特定权限的空文件

1
2
#install -b -m <权限> <来源> <目标>
install -b -m 777 /dev/null file.txt

创建一个具有特定权限的目录

1
2
# install -d -o <用户名> -g <用户组> -m <权限> <目标地址>
install -d -o www -g www -m 755  /run/php-fpm

通过 sshfs 远程挂载目录

1
2
3
4
5
yum install sshfs
# mount
sshfs -o reconnect,_netdev,user,idmap=user,identityfile=/pathto/id_rsa,default_permissions  user@host:/path  /mnt/pathto
# /etc/fstab
user@host:/path /mnt/pathto fuse.sshfs noauto,x-systemd.automount,reconnect,_netdev,user,idmap=user,identityfile=/pathto/id_rsa,allow_other,default_permissions 0 0

tmpfs 一种基于内存的文件系统

1
mount -t tmpfs -o size=1024M tmpfs /mnt/usb02

文件描述符相关

点击展开详细内容
  1. 系统最大打开的文件描述符数量

    1
    2
    3
    4
    5
    
    cat /proc/sys/fs/file-nr
    10848	0	6815744
    # 第一个值: 当前系统已分配使用的打开文件描述符数
    # 第二个值: 为分配后已释放的(目前已不再使用)
    # 第三个值: 等于/proc/sys/fs/file-max(打开的最大fd数量)
    
  2. 获取打开的文件数量

    • 获取整个系统打开的文件数量

      1
      
      lsof | wc -l
      
    • 获取某个用户打开的文件数量

      1
      
      lsof -u test |wc -l
      
    • 获取某个程序打开的文件数量

      1
      2
      3
      
      for i in `pidof dotnet`; do
          lsof -p "$i" | wc -l ;
      done
      
    • 获取某个程序打开的文件描述符数量

      1
      2
      3
      
      for i in `pidof dotnet` ; do
          echo -n "$i : "$(ll /proc/$i/fd|wc -l)
      done
      
  3. 查看系统里占用 fd 最多的进程

1
2
    lsof -n | awk '{print $2}' | sort | uniq -c | sort -nr |head -n 10
    #第一列是占用的fd数量,第二列是进程的pid

字符串拆分

1
2
echo "hello" |awk -F '' '{for(i=1;i<=NF;i++)print $i}'
#echo "hello" |awk '{split($0,a,"''");for(v in a)print a[v]}'

去除文本第一行和最后一行

1
seq 5 |awk'NR>2{print s}{s=$0}'

查看当前主机类型

1
cat /sys/class/dmi/id/product_name

find 查看特定后缀的文件

1
2
# find ./ -regex ".*\.tar.gz\|.*\.7z"
# find ./ -type f -regextype posix-extended -regex ".*\.(tar.gz|7z)"

shell 范围随机数

1
2
3
4
5
# echo $((RANDOM % (max - min) + min))
echo $((RANDOM % (99 - 80) + 80))

# shuf -i min-max -n 1
shuf -i 0-8 -n 1

linux 将时间戳转换为时间

1
date +"%F_%T" -d$timestamp

linux 远程桌面连接

1
2
# freerdp-2.2.0-1.fc32.x86_64
xfreerdp /v:<hostip> /u:<username> /drive:shares,<本地目录>

linux 打包文件夹为 ISO 文件

1
mkisofs -o file.iso -J -R -V 01 file/

Docker 与 iptables 只允许特定 ip 访问 Docker 的服务(DOCKER-USER)

https://blog.csdn.net/Liv2005/article/details/112850208

https://docs.docker.com/network/iptables/

1
2
# 允许172.31.10.0/24网段访问docker网络,eth0 为服务器对外通信网卡 
iptables -I DOCKER-USER -i eth0 ! -s 172.31.10.0/24 -j DROP

openssl 公钥提取

1
2
3
4
# 从证书中提取
openssl x509 -in domain.pem -pubkey  -noout > public.pem
# 从私钥中提取
openssl rsa -in private.key -pubout > public.pem

查看本地监听信息

1
cat /proc/net/tcp | grep " 0A " | sed 's/^[^:]*: \(..\)\(..\)\(..\)\(..\):\(....\).*/echo $((0x\4)).$((0x\3)).$((0x\2)).$((0x\1)):$((0x\5))/g' | bash

获取 ping 域名的 ip

1
ping www.baidu.com -c 1 -w 1 | sed '1{s/[^(]*(//;s/).*//;q}'

检查用户是否有操作 docker 权限

1
sudo -u zabbix curl --unix-socket /var/run/docker.sock --no-buffer -XGET v1.24/_ping

分段压缩

1
2
3
4
5
6
7
8
9
# 压缩
$> tar czf - /pathto/dir01 /pathto/dir02 |split -d -b 2G - file.tgz.
# 解压
$> cat file.tgz* | tar xz 

# 压缩
$> zip -s 100m -r myarchive.zip myfolder/
# 解压
$> unzip myarchive.zip

临时移动工作路径执行命令

1
$> (cd /some/other/dir && other-command)

mount –bind

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 将 olddir 绑定到  newdir
$> mount -o bind olddir newdir

# /etc/fstab
# ro: 只读  rw: 只写
olddir newdir none defaults,ro,bind 0 0

# systemd
# /etc/systemd/system/sftpdir-mnt.mount
[Unit]
SourcePath=/etc/fstab
Documentation=man:fstab(5) man:systemd-fstab-generator(8)
Before=local-fs.target

[Mount]
What=olddir
Where=newdir
Type=none
Options=defaults,rw,bind

[Install]
WantedBy=multi-user.target

systemd 守护桌面程序

1
2
3
4
5
6
# 以最新的QQ Linux版 3.0.0 为例,fedora33 下经常崩溃,用systemd守护其运行,在QQ崩溃时自动重启QQ
# 运行以下命令以启动systemd守护进程 
$> /usr/bin/systemd-run --property Restart=on-failure --user /opt/QQ/qq
# 替换默认 /usr/share/applications/qq.desktop的执行命令 Exec=/usr/bin/systemd-run --property Restart=on-failure --user /opt/QQ/qq
# 日志检查,可以定位当前用户的日志看(或者 systemctl --user list-units run-*|grep qq,查询到systemd-run启动的service,直接定位)
$> journalctl -f -u user@${UID}.service

证书相关操作

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#CA合并
#厂商提供的cer文件,全部合并为后缀为pem的文件,并将域的cer放在文件最前面,crt 在后面
#nginx导入key和pem即可

## 查看远程证书相关信息  
$> echo | openssl s_client -connect tools.example.com:443 2>&- | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > remote-cert.pem \
        && openssl x509 -in remote-cert.pem -text \
        && rm remote-cert.pem
#     查看证书到期时间       
#     && openssl x509 -in remote-cert.pem -enddate -noout      

## 
$> openssl s_client -showcerts -connect tools.example.com:443 </dev/null | openssl x509 -inform PEM -noout -text     

##  CA证书取消密码
$> openssl rsa -in <ca-private-key-file> -out <ca-private-key-file>.enc
##  CA证书添加密码
$> openssl rsa -des3 -in <ca-private-key-file> -out <ca-private-key-file>.enc

# 证书生成 
## 创建ca私钥
$> openssl genrsa -des3 -out ca.key 4096

## 创建ca证书
### /C=CN:证书持有者所在国家的两字母代码
### /ST=CQ:证书持有者所在省/直辖市/自治区的名称或缩写
### /O=example:证书持有者的组织或公司名称
### /CN=example:证书持有者的通用名称(Common Name),一般为服务器的域名或客户端的用户名
### /emailAddress=mail@example.com:证书持有者的电子邮件地址。
$> openssl req -utf8 -x509 -new -nodes -key ca.key -sha512 -days 18250 -out ca.pem -subj "/C=CN/ST=CQ/O=example/CN=example/emailAddress=mail@example.com"

# 创建服务器私钥
$> openssl genrsa -out server.key 4096

# 创建域名csr
$> openssl req -new -key server.key -out server.csr -subj "/C=CN/ST=CQ/O=0x5c0f/CN=example.com/emailAddress=mail@example.com"


# 创建扩展
$> cat > server.ext <<EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth, clientAuth, codeSigning
subjectAltName = @alt_names
[alt_names]
DNS.1 = *.example.com
DNS.2 = *.example.cn
DNS.3 = localhost
IP.1 = 127.0.0.1
EOF

# 生成域名证书
$> openssl x509 -req -in server.csr -CA ca.pem -CAkey ca.key -CAcreateserial -out server.crt -days 1825 -sha512 -extfile server.ext

linux 端口转发

1
2
3
4
5
6
7
# linux 下端口转发
$> echo 1 >/proc/sys/net/ipv4/ip_forward

$> iptables -t nat -A POSTROUTING -j MASQUERADE
$> iptables -A FORWARD -i [内网网卡名称] -j ACCEPT
$> iptables -t nat -A POSTROUTING -s [内网网段] -o [外网网卡名称] -j MASQUERADE
$> iptables -t nat -A PREROUTING -p tcp -m tcp --dport [外网端口] -j DNAT --to-destination [内网地址]:[内网端口]

linux 查询cpu占用过高的php-fpm进程,正在执行的php脚本或者处理的事

1
2
3
4
5
# 1. 通过top找到正在消耗 CPU 的 php-fpm 进程的 PID 
# 2. 使用 strace 命令跟踪该进程:
$> strace -p <PID> -e trace=open,execve,stat
# 3. 在输出中查找正在执行的 PHP 脚本
$> strace -p <PID> -e trace=open,execve,stat 2>&1 | grep '\.php'

查询当前目录下 md5 相同的文件

1
2
$> find . -type f -exec md5sum {} + | sort | uniq -w32 -dD
# uniq -w32 -dD:找到重复的MD5哈希,并只打印重复的行

查询当前目录下 md5 等于某个值并删除/移动

1
2
3
4
# 删除
$> find . -type f -exec md5sum {} + | grep 'your_md5_value' | cut -d ' ' -f 2- | xargs rm
# 移动
$> find . -type f -exec md5sum {} + | grep 'your_md5_value' | cut -d ' ' -f 2- | xargs -I {} mv {} /path/to/destination/