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

Block only port from being accessed outside except localhost

· 52CE 我爱测

On Centos 6 machine, how can I block port 8888 from being accessed except by localhost/127.0.0.1 only using iptables? Actually, I have another port that I want to listen on 8000 instead of 8888. This command blocks access to loca…

问题原文

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

On Centos 6 machine, how can I block port 8888 from being accessed except by localhost/127.0.0.1 only using iptables? Actually, I have another port that I want to listen on 8000 instead of 8888.

This command blocks access to localhost:8888

iptables -I INPUT -p tcp --dport 8888 -j DROP

I don't want access to 1.2.3.4:8888 but only to localhost:8888

社区高分解答

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

If you mean by 1.2.3.4 any other IP different than localhost, you can use the following (if I understood you well):

iptables -I OUTPUT -p tcp --dport 8888 -j DROP
iptables -I OUTPUT -s 127.0.0.1 -p tcp --dport 8888 -j ACCEPT

The first drop all outgoing traffic to TCP/8888 and the second rule accept it when it originates from localhost.

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

资料来源与适用范围

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

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

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