首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >firewalld一行定义TCP和UDP。

firewalld一行定义TCP和UDP。
EN

Unix & Linux用户
提问于 2020-09-30 14:24:25
回答 1查看 1.9K关注 0票数 0

当然,在一行中定义端口和协议是可能的,但是如何在一行中定义TCP和UDP协议,而不是在firewalld中的单独命令中定义?

代码语言:javascript
复制
[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

正如最后的输出所示,手册页被选中了,但是我找不到这样的例子。

EN

回答 1

Unix & Linux用户

发布于 2023-05-31 06:49:33

它可以在一行

中完成

我知道这是一个长期存在的问题,但我注意到没有人给出答案。我已经尝试过了,最好的解决方案似乎是使用扩展。可能会变得很棘手。

代码语言:javascript
复制
# 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}

然后检查一下你做了什么:)

代码语言:javascript
复制
firewall-cmd --list-ports

这只是一个演示,展示了如何使用端口和协议创建组合。

服务是更好的

我还试验了--add-services', the names of which can be found in the /etc/services的文件。它也能工作,甚至比使用端口更优雅。

名称和端口都在那里,例如

代码语言:javascript
复制
grep -E 'http|imap|pop3|smtp|dns|ftp' /etc/services

我们也可以这样做:

代码语言:javascript
复制
# Simple and readable
firewall-cmd --add-service={http,https}
firewall-cmd --list-services

希望它展示了如何优雅地做到这一点。

票数 0
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/612189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档