大网数据广告活动
服务器运维问答

I cannot get iptables to show trace logs

· 52CE 我爱测

uname -a Linux test 5.10.0-17-cloud-amd64 #1 SMP Debian 5.10.136-1 (2022-08-13) x86_64 GNU/Linux # enable logging sudo modprobe ipt_LOG sudo modprobe nf_log_ipv4 sudo sysctl net.netfilter.nf_log.2=nf_log_ipv4 # Try to log traffic…

问题原文

原帖链接 · red888 · CC BY-SA 4.0

uname -a
Linux test 5.10.0-17-cloud-amd64 #1 SMP Debian 5.10.136-1 (2022-08-13) x86_64 GNU/Linux

# enable logging
sudo modprobe ipt_LOG
sudo modprobe nf_log_ipv4
sudo sysctl net.netfilter.nf_log.2=nf_log_ipv4

# Try to log traffic
sudo iptables -t raw -I PREROUTING -s 1.2.3.4 -j LOG
sudo iptables -t raw -I PREROUTING -s 1.2.3.5 -j LOG
# I see logs for these IPs
tail /var/log/kern.log

# Try to trace traffic
sudo iptables -t raw -I PREROUTING -s 1.2.3.4 -j TRACE
sudo iptables -t raw -I PREROUTING -s 1.2.3.5 -j TRACE
# See no trace logs for these IPs
tail /var/log/kern.log

I see LOGs in log file but I see zero trace logs. I can't find info anywhere on why this might be.

已采纳解答

原帖链接 · A.B · CC BY-SA 4.0

Debian switched to iptables-over-nftables with Debian 10, so this includes Debian 11.

$ update-alternatives --list iptables
/usr/sbin/iptables-legacy
/usr/sbin/iptables-nft
$ update-alternatives --display iptables
iptables - auto mode
  link best version is /usr/sbin/iptables-nft
  link currently points to /usr/sbin/iptables-nft
  link iptables is /usr/sbin/iptables
[...]

The TRACE target framework is specifically different with this variant, to benefit from the advantages of the nftables API. It's described in the manuals for the iptables TRACE target and xtables-monitor:

With iptables-nft, the target is translated into nftables' meta nftrace expression. Hence the kernel sends trace events via netlink to userspace where they may be displayed using xtables-monitor --trace command. For details, refer to xtables-monitor(8).

xtables-monitor is used to monitor changes to the ruleset or to show rule evaluation events for packets tagged using the TRACE target. xtables-monitor will run until the user aborts execution, typically by using CTRL-C.

That means a lot of documentation and many blogs are becoming stale and showing only the legacy method.

Now with iptables-nft, one can just run this to display traces:

xtables-monitor -t

Here's at least one advantage with the new API:

Older API (still present when using the command iptables-legacy) sent TRACE results to the kernel ring buffer (eg: using dmesg). This buffer is not network-namespace aware, so there's the choice of logging only the initial network namespace activity (which includes the TRACE results), or all network namespaces activity with a toggle. TRACE being very verbose, this would quickly be a problem.

The new API uses netlink multicast-like socket messages to provide information to possibly multiple listener(s). It is also network-namespace aware. It doesn't pollute logs. Implementers chose to break compabitility for this specific target used only when debugging, to leverage nftables' API instead of keeping the original behavior.

That means when running multiple network namespace, one can trace them separately (as long as they have rules with TRACE targets) for example like this:

ip netns exec othernamespace xtables-monitor -t

with complete isolation from all other network namespaces.

The nftables command nft monitor trace would mostly work as well (maybe not displaying native xtables matches and targets) instead of xtables-monitor -t to monitor these iptables rules since it's the same API.


Actually, using the NFLOG target instead of LOG has a similar behavior (and is identical using the legacy or the nft API) and sends logs using the same kind of facility, for specialized logging tools: usually ulogd but even tcpdump is able to capture it on Linux. Example:

terminal 1:

$ ping 127.0.0.1
[...]

terminal 2 (as root):

# iptables -I INPUT -i lo -p icmp -j NFLOG --nflog-group 123
# tcpdump -ttttt -n -l -i nflog:123
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on nflog:123, link-type NFLOG (Linux netfilter log messages), snapshot length 262144 bytes
 00:00:00.000000 IP 127.0.0.1 > 127.0.0.1: ICMP echo request, id 47883, seq 36, length 64
 00:00:00.000399 IP 127.0.0.1 > 127.0.0.1: ICMP echo reply, id 47883, seq 36, length 64
 00:00:01.023906 IP 127.0.0.1 > 127.0.0.1: ICMP echo request, id 47883, seq 37, length 64
 00:00:01.023976 IP 127.0.0.1 > 127.0.0.1: ICMP echo reply, id 47883, seq 37, length 64
^C
4 packets captured
4 packets received by filter
0 packets dropped by kernel

additional information: NFLOG with wireshark, Nftables and the Netfilter logging framework.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2022-09-02)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。