常用命令 进入个人的主目录 返回上次所在的目录 查看目录中的文件 显示文件和目录的详细资料 显示隐藏文件 显示包含数字的文件名和目录名 递归创建文件夹 删除一个叫做 'dir1' 的目录 同时删除两个目录及它们的内容 命名/移动 一个目录 复制一个文件 复制一个目录下的所有文件到当前工作目录 复制一个目录到当前工作目录 复制一个目录 复制一个目录及子目录(递归) 创建一个指向文件或目录的软链接(不可以相对路径) 创建一个指向文件或目录的物理链接 从 '/' 开始进入根文件系统搜索文件和目录 显示一个二进制文件、源码或 man 的位置 显示一个二进制文件或可执行文件的完整路径 显示权限 通过 PID 查找端口 查看某个端口是否被占用 1 netstat -nlp | grep 端口号/pid
参数说明:
-n: --numeric 的缩写,即通过数值展示 IP 地址-l: --listening 的缩写,只打印正在监听中的网络连接-p: --program,打印相应端口号对应进程的进程号使用标准的格式显示进程 查找某文件 查找文件夹下包含 "xxx" 的所有文件记录 1 2 grep -rn "xxx" a/* b/* c/app.js
显示进程(BSD 格式) 赋权操作 1 2 3 chmod (u g o a) (+ - =) (r w x) (文件名)
vim 操作 - 全局替换 a 为 b 解压 RPM 包 1 rpm2cpio xxx.rpm | cpio -div
查看当前 PID 下所有线程(默认按 CPU 使用率排序) 选删文件 1 2 find . -mtime +7 -maxdepth 1 ! -name ${val} -type f -print -exec rm -rf {} \;
查找目录下的所有文件中是否含有某个字符串 1 find . | xargs grep -ri "IBM"
查找目录下的所有文件中是否含有某个字符串,并且只打印出文件名 1 find . | xargs grep -ri "IBM" -l
快速 kill PID 1 kill $(ps aux | grep 进程名 | tr -s ' ' | cut -d ' ' -f 2)
批量替换文件内容 例如将 /var/www/test 文件夹下的所有文件内容中的 abc 字符串换成 123:
1 2 sed -i "s/abc/123/g" `grep abc -rl /var/www/test`
查看进程内存占用排行 1 ps aux | awk '{print $6/1024 " MB\t\t" $11"\t"$NF}' | sort -nr | head -10
Linux 系统下查看网卡相关数据 查看网口基本信息 点亮网卡灯 查询网口驱动相关信息 1 2 3 4 5 6 7 8 9 10 $ ethtool -i eth0 driver: igb version: 5.3.0-k firmware-version: 1.67, 0x80000d38, 18.3.6 bus-info: 0000:01:00.0 supports-statistics: yes supports-test: yes supports-eeprom-access: yes supports-register-dump: yes supports-priv-flags: no
查询网口收发包统计信息 1 2 3 4 5 6 7 8 9 10 $ ethtool -S eth0 $ ethtool -S eth0 | grep rx_queue | grep packets rx_queue_0_packets: 117947 rx_queue_1_packets: 3050 rx_queue_2_packets: 1098 rx_queue_3_packets: 1204 rx_queue_4_packets: 9679 rx_queue_5_packets: 1172 rx_queue_6_packets: 7435 rx_queue_7_packets: 2403
显示网卡 offload 参数的状态 1 2 3 4 5 6 7 8 9 10 11 12 13 $ ethtool -k eth0 Features for eth0: rx-checksumming: on tx-checksumming: on tcp-segmentation-offload: on udp-fragmentation-offload: off [fixed] generic-segmentation-offload: on generic-receive-offload: off large-receive-offload: off [fixed] ... ... ntuple-filters: off [fixed] receive-hashing: on ... ...
参数解释如下:
rx-checksumming: 接收包校验和tx-checksumming: 发送包校验和tcp-segmentation-offload: 简称为 TSO,利用网卡对 TCP 数据包分片udp-fragmentation-offload: 简称为 UFO,针对 UDP 的generic-segmentation-offload: 简称 GSO,基本思想就是尽可能的推迟数据分片直至发送到网卡驱动之前,检查网卡是否支持分片功能(如 TSO、UFO),如果支持直接发送到网卡,如果不支持就进行分片后再发往网卡。这样大数据包只需走一次协议栈,而不是被分割成几个数据包分别走,这就提高了效率generic-receive-offload: 简称 GRO,基本思想跟 LRO 类似,克服了 LRO 的一些缺点,更通用。后续的驱动都使用 GRO 的接口,而不是 LROlarge-receive-offload: 简称 LRO,通过将接收到的多个 TCP 数据聚合成一个大的数据包,然后传递给网络协议栈处理,以减少上层协议栈处理开销,提高系统接收 TCP 数据包的能力ntuple-filters: ntuple配置网卡 offload 参数 1 2 3 4 5 6 ethtool -K eth0 rx-checksum on|off ethtool -K eth0 tx-checksum-ip-generic on|off ethtool -K eth0 tso on|off ethtool -K eth0 ufo on | off ethtool -K eth0 gso on | off ethtool -K eth0 ntuple on | off
查看网卡 ntuple 配置规则 1 2 3 4 5 6 7 8 9 10 11 $ ethtool -n eth5 24 RX rings available Total 480 rules Filter: 100 Rule Type: Raw IPv4 Src IP addr: 0.0.0.0 mask: 255.255.255.255 Dest IP addr: 10.79.229.11 mask: 255.255.0.0 TOS: 0x0 mask: 0xff Protocol: 0 mask: 0xff L4 bytes: 0x0 mask: 0xffffffff Action: Direct to queue 11
查看现有 flow-hash 配置:
1 2 3 4 $ ethtool -n eth5 rx-flow-hash tcp4 TCP over IPV4 flows use these fields for computing Hash flow key: IP SA IP DA
配置 flow-hash:
1 2 3 4 ethtool -N eth5 rx-flow-hash udp4 sd ethtool -N eth5 rx-flow-hash tcp4 sd ethtool -N eth5 rx-flow-hash udp4 sdfn ethtool -N eth5 rx-flow-hash tcp4 sdfn
选项含义:
s: Hash on the IP source address of the rx packetd: Hash on the IP destination address of the rx packetf: Hash on bytes 0 and 1 of the Layer 4 header of the rx packetn: Hash on bytes 2 and 3 of the Layer 4 header of the rx packet查看网卡队列数量 通过 -l 选项查看网卡队列数:
1 2 3 4 5 6 7 8 9 10 11 12 $ ethtool -l eth4 Channel parameters for eth4: Pre-set maximums: RX: 16 TX: 16 Other: 1 Combined: 16 Current hardware settings: RX: 0 TX: 0 Other: 1 Combined: 8
查看网卡队列绑定信息 查看 CPU 与网卡多队列绑定信息,执行如下脚本即可:
1 2 3 for i in `cat /proc/interrupts | grep eth0 \ | awk -F: '{print $1}' `; do echo $i && cat /proc/irq/$i /smp_affinity ; done
新浪使用的驱动 igb 系列驱动:千兆网卡。包含:82575, 82576, 82580, I210, I211, I350, I354, DH89xx ixgbe 系列驱动:万兆网卡。包含:82598, 82599, X520, X540, X550 i40e 系列驱动:万兆网卡。包含:X710, XL710, X722, XXV710 关于驱动常用的几个命令 modprobe:安装网卡驱动modinfo:查看网卡驱动具体信息ethtool -i eth0:查看某个网口的驱动信息lspci:查看 PCI 信息depmod:加载驱动 ko 的依赖模块cat /proc/interrupt | grep ethx:查看网卡的队列数执行以下命令,解决网卡驱动依赖问题:
1 2 3 depmod -a 2.6.32-642.15.1.sina11.3.1.el6.alpha1.x86_64 modinfo igb -k 2.6.32-642.15.1.sina11.3.1.el6.alpha1.x86_64 depmod -a
安装官方 igb 驱动 官网下载即可 编译驱动:cd src; make && make installl 编译驱动程序依赖:kernel-devel 的 rpm 包 安装完毕后执行:depmod -a、modinfo igb 新驱动可能需要设置开启网卡队列 安装新驱动后,reboot 后需要查看网卡队列是否开启 cat /proc/interrupt | grep eth4 如果没有开启队列,需要配置 /etc/modprobe.conf 或 /etc/modprobe.d/modprobe.conf 1 2 options igb InterruptThrottleRate=3000,3000,3000,3000 RSS=0,0,0,0 LRO=0,0,0,0 QueuePairs=0,0,0,0
千兆网卡出现 CPU 不均的情况 网卡接收队列包数均匀 网卡发送队列不均匀,基本上都分布在 cpu6 上 查看中断对应的 cpu:cat /proc/irq/71/smp_affinity 驱动是 igb 千兆,网卡型号:82576 现象:cpu6 负载过高,整体 cpu 负载提高 0.8 倍 添加用户 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 groupadd <group_name> groupdel <group_name> groupmod -n <new_group_name> <old_group_name> usermod -a -G <group> <user> useradd -c "Name Surname" -g admin -d /home/user1 -s /bin/bash <user1> useradd <user1> userdel -r <user1> usermod -c "User FTP" -g system -d /ftp/user1 -s /bin/nologin <user1> passwd passwd <user1> adduser <user1> sudo chage -E 2005-12-31 user1 pwck grpck newgrp group_name
其他 查看内存使用率 1 free -m | sed -n '2p' | awk '{print "used mem is "$3"M,total mem is "$2"M,used percent is "$3/$2*100"%"}'
防火墙 1 2 3 4 firewall-cmd --permanent --zone=public --add-port=3306/tcp firewall-cmd --reload
查看权限 剔除限制 Linux 下使用 curl 命令访问 HTTPS CentOS 查看服务器配置信息 envsubst 使用环境变量替换文件内容 一般用法:
1 envsubst < xxxx.yaml > xxxx1.yaml
指定特定待替换的环境变量:
1 envsubst '${VAR_NAME}' < xxx.yaml > xxxx1.yaml
查看 Linux 操作系统版本 通过 /etc/os-release 文件查看 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 $ cat /etc/os-release NAME="CentOS Linux" VERSION="7 (Core)" ID="centos" ID_LIKE="rhel fedora" VERSION_ID="7" PRETTY_NAME="CentOS Linux 7 (Core)" ANSI_COLOR="0;31" CPE_NAME="cpe:/o:centos:centos:7" HOME_URL="https://www.centos.org/" BUG_REPORT_URL="https://bugs.centos.org/" CENTOS_MANTISBT_PROJECT="CentOS-7" CENTOS_MANTISBT_PROJECT_VERSION="7" REDHAT_SUPPORT_PRODUCT="centos" REDHAT_SUPPORT_PRODUCT_VERSION="7"
通过 lsb_release 命令查看 可能初始系统不带此指令
通过 proc 目录中可用版本文件查看 1 2 3 $ cat /proc/version Linux version 3.10.0-1160.59.1.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) )