版本1的抓包命令

这两天排查一个小问题,需要在服务器上使用tcpdump24小时不间断抓包,这里简单记录下。

先看下tcpdump的语法:

tcpdump [ -AbdDefhHIJKlLnNOpqStuUvxX# ] [ -B buffer_size ]
[ -c count ]
[ -C file_size ] [ -G rotate_seconds ] [ -F file ]
[ -i interface ] [ -j tstamp_type ] [ -m module ] [ -M secret ]
[ --number ] [ -Q|-P in|out|inout ]
[ -r file ] [ -V file ] [ -s snaplen ] [ -T type ] [ -w file ]
[ -W filecount ]
[ -E spi@ipaddr algo:secret,... ]
[ -y datalinktype ] [ -z postrotate-command ] [ -Z user ]
[ --time-stamp-precision=tstamp_precision ]
[ --immediate-mode ] [ --version ]
[ expression ]

一开始使用的命令基本长下面这样:

nohup tcpdump -i ens192 tcp port 5432 -C 1 -w /root/pg5432.pcap &

这个语句的前后分别是nohup 和 &, 这个是为了断开ssh后也能在后台运行,剩下部分才是主体:

tcpdump -i ens192 tcp port 5432 -C 1  -w /root/pg5432.pcap
  • -i ens192,指定网卡

  • tcp port 5432,指定捕获表达式

  • -C 1,表示当捕获文件的大小超过1M时,就新打开一个文件,我昨天是为了测试,就弄的1M。

    -C file_size

    Before writing a raw packet to a savefile, check whether the file is currently larger

    than file_size and, if so, close the current savefile and open a new one. Savefiles after the first savefile will have the name specified with the -w flag, with a number after it, starting at 1 and continuing upward. The units of file_size are millions of bytes (1,000,000 bytes, not1,048,576 bytes).

  • -w /root/pg5432.pcap,将抓的包存到该路径指定的文件。

    这个选项有一点,就是捕获的内容会被缓存,不会马上写入文件,除非指定-U选项。

    This output will be buffered if written to a file or pipe, so a program reading from the file or pipe may not see packets for an arbitrary amount of time after they are received.

    Use the -U flag to cause packets to be written as soon as they are received.

结果执行这个语句,报错:

tcpdump: /root/pg5432.pcap: Permission denied

我都有点懵,我是尊贵的root,还能没权限吗?

后面再想,是不是变成用tcpdump用户执行了,检查了下这个用户的权限,看着没问题,后面还是在网上找到了答案:

https://serverfault.com/questions/478636/tcpdump-out-pcap-permission-denied

意思就是说,加了-C选项后,会放弃自己root的身份,这段英文有点拗口,自己看吧:

Note that when used with -Z option (enabled by default), privileges are dropped before opening first savefile

不过吧,这句提示,在centos 7上安装的tcpdump版本中,甚至没出现,看来文档还是有点问题。

解决的办法,就是加上-Z选项,我这边就是-Z root。

-Z user

--relinquish-privileges=user

If tcpdump is running as root, after opening the capture device or input savefile, but before opening any savefiles for output,change the user ID to user and the group ID to the primary group of user.

This behavior can also be enabled by default at compile time.

版本二的抓包命令

nohup tcpdump -i ens192 tcp port 5432 -C 1 -Z root -w /root/pg5432.pcap &

这个命令就可以用了。

抓包效果如下:

扩展选项

当然,可以看到抓到的包很多,如果流量大,可能担心磁盘会炸,此时,可以再加上 -W 选项来限制最多生成多少个文件,如-W 100,最多生成100个,超过100后,就会覆盖最早的文件。

-W

Used in conjunction with the -C option, this will limit the number of files created to the specified number, and begin over‐writing files from the beginning, thus creating a 'rotating' buffer. In addition, it will name the files with enough leading 0s to support the maximum number of files, allowing them to sort correctly.

Used in conjunction with the -G option, this will limit the number of rotated dump files that get created, exiting with status 0 when reaching the limit. If used with -C as well, the behavior will result in cyclical files per timeslice.

除了达到一定大小就新建文件,也可以每隔n秒新建文件:

-G rotate_seconds

If specified, rotates the dump file specified with the -w option every rotate_seconds seconds. Savefiles will have the namespecified by -w which should include a time format as defined by strftime(3). If no time format is specified, each new file

will overwrite the previous.

If used in conjunction with the -C option, filenames will take the form of `file'.

在麒麟操作系统如何安装tcpdump

银河麒麟V10

我这次查问题是要在两端同时抓包,一端是centos,另一端结果发现是信创操作系统,麒麟V10。

首先说下,怎么辨别是中标麒麟,还是银河麒麟。

[root@xc-website-db-dev ~]# cat /etc/os-release
NAME="Kylin Linux Advanced Server"
VERSION="V10 (Tercel)"
ID="kylin"
VERSION_ID="V10"
PRETTY_NAME="Kylin Linux Advanced Server V10 (Tercel)"
ANSI_COLOR="0;31"

如果是V10,就是银河麒麟;如果是V7,是中标麒麟。

参考:https://www.cnblogs.com/shuiche/p/16334092.html

我这边也看了下麒麟的官网,服务器端,主要就是这几个版本:

官方文档在这里:

https://www.kylinos.cn/support/document/60.html

银河麒麟高级服务器操作系统 V10系统管理员手册V3.0-20221024.pdf

配置yum源

我看了下上面的文档,已经使用dnf源了,但我们这个系统,不知道谁装的,也还是搞了yum。

但是yum是个本地源,不知道为啥还用不了,配外部yum源吧,内网机器还不能上外网,另外,麒麟的官方的yum源也不知道在哪里,服了,另外,我怀疑是不是必须得用他们的光盘来搞本地源啊。。

我在这台机器上发现个备份的麒麟repo,就记录在这里吧:

###Kylin Linux Advanced Server 10 - os repo###

[ks10-adv-os]
name = Kylin Linux Advanced Server 10 - Os
baseurl = http://update.cs2c.com.cn:8080/NS/V10/V10SP1.1/os/adv/lic/base/$basearch/
gpgcheck = 1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-kylin
enabled = 1 [ks10-adv-updates]
name = Kylin Linux Advanced Server 10 - Updates
baseurl = http://update.cs2c.com.cn:8080/NS/V10/V10SP1.1/os/adv/lic/updates/$basearch/
gpgcheck = 1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-kylin
enabled = 1 [ks10-adv-addons]
name = Kylin Linux Advanced Server 10 - Addons
baseurl = http://update.cs2c.com.cn:8080/NS/V10/V10SP1.1/os/adv/lic/addons/$basearch/
gpgcheck = 1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-kylin
enabled = 0

我想装个tcpdump也很不容易。

这块大家自己百度搜一下吧,我后面是直接rpm安装的:(仅针对x86架构)

https://update.cs2c.com.cn/KY/V10/8U2/os/adv/lic/BaseOS/x86_64/Packages/

里面有tcpdump-4.9.2-6.el8.x86_64.rpm,下载下来rpm -ivh 安装即可。

参考文件

https://www.cnblogs.com/cnhk19/p/16273102.html

https://serverfault.com/questions/478636/tcpdump-out-pcap-permission-denied

https://blog.csdn.net/Xeon_CC/article/details/132142880 (给银河麒麟v10添加yum源)

tcpdump后台不间断抓包的更多相关文章

  1. [na]tcpdump非常实用的抓包实例

    基本语法篇 常用应用: 过滤物理口 过滤某个port/ip/mac 过滤协议 显示ip/mac/port不解析等 过滤mac tcpdump -i eth0 ether host 24:DF:6A:F ...

  2. tcpdump安装配置及抓包分析

    http://blog.csdn.net/e421083458/article/details/23963189 cpdump安装配置及抓包分析 预装软件:[plain] view plain cop ...

  3. 【跟唐老师学习云网络】 - 第7篇 Tcpdump大杀器抓包

    [摘要] 前面章节的网络协议栈相关的信息建议大家多学习一遍,因为这些都是最基础的东西,想玩好云网络必备基本功.. 一.上帝视角 之前提到过定位问题可以开启上帝视角,那么如何开启就要依靠tcpdump这 ...

  4. Tcpdump非常实用的抓包12实例

    1.过滤主机---------------------------------------------------------------- - 抓取所有经过 eth1,目的或源地址是 192.168 ...

  5. tcpdump非常实用的抓包实例

    详细的文档见tcpdump高级过滤技巧 基本语法 ========过滤主机--------- 抓取所有经过 eth1,目的或源地址是 192.168.1.1 的网络数据# tcpdump -i eth ...

  6. 如何利用tcpdump对mysql进行抓包操作

    命令如下: tcpdump -s -l -w - dst -i eno16777736 |strings 其中-i指定监听的网络接口,在RHEL 7下,网络接口名不再是之前的eth0,而是 eno16 ...

  7. 【转】详解使用tcpdump、wireshark对Android应用程序进行抓包并分析

    原文网址:http://blog.csdn.net/gebitan505/article/details/19044857 本文主要介绍如何使用tcpdump和wireshark对Android应用程 ...

  8. Wireshark和TcpDump抓包分析心得

    Wireshark和 TcpDump抓包分析心得  1. Wireshark与tcpdump介绍 Wireshark是一个网络协议检测工具,支持Windows平台和Unix平台,我一般只在Window ...

  9. Android通过tcpdump抓包(wifi, 2g, 3g都可以)

    http://blog.csdn.net/deng529828/article/details/20646197 1. 手机要有root权限 2. 下载tcpdump   http://www.str ...

  10. Android 联网监控抓包工具的制作(tcpdump的使用)

    最近做一个Android联网抓包的工具 自己在网上搜索了好久 发现还是没有头绪 于是考虑在linux层上下功夫 于是采用linux的tcpdump来实现了抓包的功能 用简单的话来定义tcpdump,就 ...

随机推荐

  1. Linux下程序时间消耗监控与统计

    良好的计时器可帮助程序开发人员确定程序的性能瓶颈,或对不同算法进行性能比较.但要精确测量程序的运行时间并不容易,因为进程切换.中断.共享的多用户.网络流量.高速缓存访问及转移预测等因素都会对程序计时产 ...

  2. 常量接口 vs 常量类 vs 枚举区别

    把常量定义在接口里与类里都能通过编译,那2者到底有什么区别呢? 那个更合理? 常量接口 public interface ConstInterfaceA { public static final S ...

  3. 消失的死锁:从 JSF 线程池满到 JVM 初始化原理剖析

    一.问题描述 在一次上线时,按照正常流程上线后,观察了线上报文.接口可用率十分钟以上,未出现异常情况,结果在上线一小时后突然收到jsf线程池耗尽的报警,并且该应用一共有30台机器,只有一台机器出现该问 ...

  4. AI室内设计:提升效率、消除沟通障碍,满足客户需求

    前言 免费AI绘图工具:https://www.topgpt.one 随着人工智能(AI)技术的不断发展,室内设计行业也开始受益于这一技术的应用.其中,AI绘画工具在室内设计中的应用正日益受到关注.这 ...

  5. Self-Instruct 论文解读:利用大模型自己给自己生成指令数据,指令数据自动生成

    总览 大规模"指令调整"的语言模型,即指令微调的LLM,已经表现出非凡的零样本能力,尤其是推广新任务上. 然而,这些模型严重依赖于人类编写的指令数据,而这些数据通常在数量.多样性和 ...

  6. Unparseable date: "2019-03-27"

    Unparseable date: "2 public static void main(String[] args) { try { String dateStr="2019-0 ...

  7. PlayWright(十二)- PO模式

    1.PO模式是什么? PO,即Page Object,直译为页面对象,代表 Web 应用程序的一部分   具体什么意思呢,通俗来讲,一个页面有输入.点击.搜索功能,而且有很多页面,这时候我们就采用每个 ...

  8. 你一定要用这个API管理工具,看完你就知道为什么了

    以下是经常发生在程序员之间的对话: 小张:你知道为什么程序员不喜欢写文档? 小王:因为代码就是最好的文档啊!谁还需要写那些冗长的说明呢? 小张:那你知道为什么程序员也不喜欢别人不写文档吗? 小王:当然 ...

  9. Hexo、Typecho博客添加旅行足迹网页

    本文部署的足迹地图,地址如下: http://www.aomanhao.top/index.php/archives/183/ jVectorMap JVectorMap 是一个优秀的.兼容性强的 j ...

  10. Java_Day17_作业

    1:需求:递归删除带内容的目录 假设删除当前项目下的目录:demo,demo中可以有文件夹自己给出 2:需求:请大家把E:\JavaSE目录下所有的java结尾的文件的绝对路径给输出在控制台. 3:下 ...