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

How can I log packets dropped by policy in nftables?

· 52CE 我爱测

I have a policy to drop packets in the input chain chain input { type filter hook input priority 0; policy drop; How can I log just these dropped packets?

问题原文

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

I have a policy to drop packets in the input chain

        chain input {
                type filter hook input priority 0; policy drop;

How can I log just these dropped packets?

社区高分解答

原帖链接 · John Greene · CC BY-SA 4.0

Well, I'm going to guess that you also want to know WHAT kind of packet is being logged at the end of your nftables chain.

Let us assume that you already have a log capture daemon (such as syslogd, rsyslog-ng, or ulogd2) that is already properly configured, daemonized, running, and reading all of your kernel loggings (from /dev/log) that is fed by kernel’s ksyslog() and saving these log messages to a file (such as /var/log/message).

Your default policy is to ACCEPT

If your chain policy is to accept, then append the log keywords to your nftable rule:


table filter {
  ...
  chain input {
    type filter hook input priority 0; policy accept;
    ...
    # All my rules go here
    # Pick one that suits your needs best

    add rule inet filter input tcp dport 22 drop log
    add rule inet filter input tcp dport 21 counter drop log prefix my_input_ftp

  }
  ...
}

This will log any attempt at SSH or FTP to your SSH/FTP server then drop the packet.

Your default policy is to DROP

I would add one line at the very end of that chain, example of filter input chain is given below:

table filter {
  ...
  chain input {
    type filter hook input priority 0; policy drop;
    ...
    # All my rules go here
    
    ...
    # Pick one that suits your needs best
    counter comment "total unfiltered input packets"
    log            # simple detail goes into the log
    log flags all  # extra details go into the log
    log flags all prefix "GOTCHA!: " # parseable keyword
    log flags all counter  # redundant but example
    # drop; # this is redundant policy is drop already
  }
  ...
}

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

资料来源与适用范围

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

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

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