当然,在一行中定义端口和协议是可能的,但是如何在一行中定义TCP和UDP协议,而不是在firewalld中的单独命令中定义?
[root@centos8 /]# firewall-cmd --permanent --add-port=2222/tcp/udp
Error: INVALID_PORT: bad port (most likely missing protocol), correct syntax is portid[-portid]/protocol
[root@centos8 /]# firewall-cmd --permanent --add-port=2222/tcp-udp
Error: INVALID_PROTOCOL: 'tcp-udp' not in {'tcp'|'udp'|'sctp'|'dccp'}
[root@centos8 /]# firewall-cmd --permanent --add-port=2222/tcp|udp
-bash: udp: command not found
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>
BrokenPipeError: [Errno 32] Broken pipe
[root@centos8 /]# firewall-cmd --permanent --add-port=2222/tcp,udp
Error: INVALID_PROTOCOL: 'tcp,udp' not in {'tcp'|'udp'|'sctp'|'dccp'}
[root@centos8 /]# firewall-cmd --permanent --add-port=2222/tcp udp
usage: see firewall-cmd man page
firewall-cmd: error: unrecognized arguments: udp
[root@centos8 /]# firewall-cmd --permanent --add-port=2222/'tcp''udp'
[root@dynatrace /]# firewall-cmd --permanent --add-port=2222/tcp udp
usage: see firewall-cmd man page正如最后的输出所示,手册页被选中了,但是我找不到这样的例子。
发布于 2023-05-31 06:49:33
中完成
我知道这是一个长期存在的问题,但我注意到没有人给出答案。我已经尝试过了,最好的解决方案似乎是使用扩展。可能会变得很棘手。
# simple
firewall-cmd --add-port={80,443}/tcp
# both protocols
firewall-cmd --add-port={80,443}/{tcp,udp}
# can be a bit complex (notice nested brackets)
firewall-cmd --add-port={{80,443}/{tcp,udp},{110,995}/tcp}然后检查一下你做了什么:)
firewall-cmd --list-ports这只是一个演示,展示了如何使用端口和协议创建组合。
我还试验了--add-services', the names of which can be found in the /etc/services的文件。它也能工作,甚至比使用端口更优雅。
名称和端口都在那里,例如
grep -E 'http|imap|pop3|smtp|dns|ftp' /etc/services我们也可以这样做:
# Simple and readable
firewall-cmd --add-service={http,https}
firewall-cmd --list-services希望它展示了如何优雅地做到这一点。
https://unix.stackexchange.com/questions/612189
复制相似问题