nftables를 사용하여 다른 포트를 여는 방법은 무엇입니까?

nftables를 사용하여 다른 포트를 여는 방법은 무엇입니까?

저는 Debian Linux 서버를 실행하고 있습니다.

일부 방화벽 포트를 열어야 합니다. 일반적으로 iptables를 사용하면 간단하지만 이 서버는 nftables를 실행하고 있습니다.

conf 파일을 열면 규칙이 표시되지 않습니다.

cx:/etc# cat nftables.conf 

#!/usr/sbin/nft -f
flush ruleset

table inet filter {
        chain input {
                type filter hook input priority 0;
        }

        chain forward {
                type filter hook forward priority 0;
        }
        chain output {
                type filter hook output priority 0;

하지만 규칙을 나열하면 다음과 같이 볼 수 있습니다.

:/etc# nft list ruleset
table inet filter {
        chain input {
                jump phonesystem
        }

        chain phonesystem {
                ip daddr 224.0.1.75 counter packets 0 bytes 0 accept
                tcp dport { http, https, sip, sip-tls, 5062, 5090 } ct state new counter packets 0 bytes 0 accept
                udp dport { sip, 5090, 7000-10999 } counter packets 0 bytes 0 accept
        }
}
table ip filter {
        chain INPUT {
                meta l4proto tcp tcp dport 22 counter packets 0 bytes 0 accept
                jump phonesystem
        }

        chain phonesystem {
                ip daddr 224.0.1.75 counter packets 0 bytes 0 accept
                tcp dport { http, https, sip, sip-tls, 5062, 5090 } ct state new counter packets 0 bytes 0 accept
                udp dport { sip, 5090, 7000-10999 } counter packets 0 bytes 0 accept
        }

        chain FORWARD {
                type filter hook forward priority 0; policy accept;
        }

        chain OUTPUT {
                type filter hook output priority 0; policy accept;
                meta l4proto tcp tcp dport 22 counter packets 0 bytes 0 accept
        }
}
table ip6 filter {
        chain INPUT {
                jump phonesystem
        }

        chain phonesystem {
                tcp dport { http, https, sip, sip-tls, 5062, 5090 } ct state new counter packets 0 bytes 0 accept
                udp dport { sip, 5090, 7000-10999 } counter packets 0 bytes 0 accept
        }

        chain FORWARD {
                type filter hook forward priority 0; policy accept;
        }

        chain OUTPUT {
                type filter hook output priority 0; policy accept;
        }
}
table ip security {
        chain INPUT {
                type filter hook input priority 150; policy accept;
        }

        chain FORWARD {
                type filter hook forward priority 150; policy accept;
        }

        chain OUTPUT {
                type filter hook output priority 150; policy accept;
        }

이제 포트 4001 등을 열고 싶습니다.

IP 테이블과 연결된 전화 시스템에서 많은 명령을 시도했지만 항상 구문 오류가 발생합니다. 내가 어디서 잘못되었는지 궁금해!

예를 들어

:/etc# nft add rule ip filter input tcp dport 4001 accept
Error: Could not process rule: No such file or directory
add rule ip filter input tcp dport 4001 accept

또는

:/etc# nft add rule ip phonesystem tcp dport 4001 counter accept
Error: syntax error, unexpected tcp, expecting string
add rule ip phonesystem tcp dport 4001 counter accept

더 많은 포트를 열고 싶다면 전화 시스템 체인에 어떻게 추가합니까?

답변1

귀하의 시스템기본적으로iptables-nft대신 사용하십시오 iptables-legacy:

Debian Buster부터 nf_tables는 iptables-nft 계층을 통해(즉, nf_tables 커널 하위 시스템을 통해 iptables 구문을 사용하여) iptables를 사용할 때 기본 백엔드입니다. 이는 ip6tables, arptables 및 ebtables에도 영향을 미칩니다.

도구 구성iptables그래서 사용하고 있습니다iptables-nft대신에iptables-legacy. 그래서 눈에 보이는데nftables또한. 에서도 같은 일이 일어납니다 ip6tables.

따라서 nft list ruleset실제로 표시되는 것은 inet기본적으로 정의된 제품군(이중 IPv4+IPv6) 입니다.nftables통과하다/etc/nftables.conf 그리고iptables다른 구성이나 도구로 완료된 명령의 결과입니다.

ip filter INPUT/OUTPUT/FORWARD체인 등의 규칙을 변경하면 안 됩니다 . 그렇지 않으면 iptables나중에 체인에서 다시 변환할 수 없을 때 실패할 수 있습니다.nftables 호환성 APIiptables. 마찬가지로 nft모든 내용을 디코딩하는 것이 불가능할 수도 있습니다.iptables' 체인은 여전히 ​​번역할 수 없는 것을 사용할 수 있기 때문입니다.iptables일치 또는 대상. 현재 규칙 세트에서는 그렇지 않습니다(또는 #주석( )이 있는 행이 때때로 표시됩니다).

ip filter INPUT기본 체인 ip6 filter INPUT으로 표시 inet filter input되지 않고 phonesystem사용자 / 일반 체인: 먼저 type filter hook input priority 0; policy accept;패킷 경로에서 Netfilter 후크를 사용하여 기본 체인 으로 만들어야 합니다 . 이것이 없으면 여기에서는 입력 필터링이 발생하지 않습니다. 또는 잘라내기/붙여넣기 오류인 경우 기본 입력 정책이 패킷을 삭제하는 것인지 여부(그렇지 않으면 패킷을 수락해야 하는 이유는 무엇입니까?)를 알 수 없게 되어 최종 제안에 영향을 미칩니다.

이 질문에 답하기 위해. 체인 이름이 잘못되었거나(대소문자 구분) 필수 테이블 이름이 누락되어 매번 구문 오류가 발생합니다. 그렇지 않으면 작동하지만 명령을 더 이상 사용하지 못하게 하는 명령은 iptables다음과 같습니다.

nft add rule ip filter INPUT tcp dport 4001 accept
nft add rule ip filter phonesystem tcp dport 4001 counter accept

무슨 일이 있어도 하지 마세요.

iptables-save규칙 세트를 표시하는 데 사용됩니다.iptablesiptables규칙은 다음 명령으로 생성되었으므로 형식을 지정하고 이를 사용하여 평소대로 규칙을 변경합니다 iptables(체계적으로 카운터 규칙 프롬프트 보기).

iptables -A INPUT -p tcp --dport 4001 -j ACCEPT
iptables -A phonesystem -p tcp --dport 4001 -j ACCEPT

사용법 충돌이 없으므로 inet 필터 테이블에 규칙을 자유롭게 추가할 수 있습니다. 또는 이름이 일치하지 않는 한 필요에 맞게 테이블을 직접 만들 수 있습니다.iptables(-nft API를 통해):

예를 들어:

nft add table ip myowntable
nft add chain ip myowntable mycustominput '{ type filter hook input priority 10; policy accept; }'

상호작용이 있다는 점에 유의하세요. 패킷이 체인에서 손실되면 영원히 손실된 상태로 유지됩니다. 패킷이 체인(예: )에서 승인되면 ip filter INPUT동일한 후크/유형(예: 또는 )의 체인 후크에 계속 삭제될 수 있습니다. 그래서 이것은 의미가 있습니다:inet filter inputip myowntable mycustominput

삭제된 패킷은 복구되지 않습니다.

nft add rule ip myowntable mycustominput tcp dport 5555 drop

그러나 다음은 도움이 되지 않습니다(이를 수락해도 ip myowntable mycustominput다른 규칙에 의해 제거되기 전에 다른 곳에서 제거되는 것을 방지할 수는 없습니다 ip filter INPUT).

nft add rule ip myowntable mycustominput tcp dport 4001 accept

결론: 시스템에서 사용 중인 다른 도구가 있는 경우 iptables해당 도구를 계속 사용 iptables하거나 해당 도구로 전환하세요.nftables첫 번째. 두 가지를 동시에 사용하면 더 많은 문제가 발생합니다. 두 가지를 모두 사용해야 하는 경우 이 질문에 답변할 수 있는 몇 가지 추가 관련 Q&A가 있습니다(힌트: 패킷 표시가 필요할 수 있음).

관련 정보